1 == Setup a new kvm domain fast ==
5 or: how to install Debian.
7 Define a shell function
12 echo -n "New hostname: " &&
15 target="/mnt/target-$guest" &&
16 mirror=`cat /etc/apt/sources.list /etc/apt/sources.list.d/debian.list 2>/dev/null | awk '/^deb.*debian/ {print $2; exit}'` &&
17 vgdefault=`vgdisplay -c | awk -F: '{print $1;exit}' | sed 's/ *//g'` &&
18 echo -n "Volume group? [$vgdefault]: " &&
20 if [ "$vg" = "" ]; then vg="$vgdefault"; fi &&
21 echo -n "Use lvm for non-swap partitions? [Y/n] " &&
23 if [ "$use_lvm" = "n" ]; then
24 : SAN, as in the msa2012i at ubcece &&
25 : requires that it is already setup &&
26 dev_root="/dev/mapper/$guest-root" &&
27 dev_boot="/dev/mapper/$guest-boot" &&
28 echo "Root device will be $dev_root" &&
29 echo "Boot device will be $dev_boot" &&
30 echo "Make sure they exist already." &&
34 dev_root="/dev/mapper/$vg-$guest--root" &&
35 dev_boot="/dev/mapper/$vg-$guest--boot" &&
36 echo "Root device will be $dev_root" &&
37 echo "Boot device will be $dev_boot" &&
40 if [ "$vg" != "" ]; then
41 dev_swap="/dev/mapper/$vg-$guest--swap"
42 elif [ -d /SWAPFILES ]; then
43 dev_swap=/SWAPFILES/$guest-swap
45 echo "No idea how to do swap" && false
47 echo -n "Use a /boot filesystem (strongly recommended)? [Y/n] " &&
49 echo "Swap device will be $dev_swap" &&
51 echo "Chosen mirror is $mirror"
63 The way we lay out the filesystems by default is that we have one 4g /
64 filesystem, a swap, and a tiny boot filesystem. On the host we make
65 a new LVM logical volume for each of the three. Only the LV that will
66 take the guest's boot will actually be partitioned - into a single boot
67 partition. That's so we can install grub into the MBR and have the system
68 start just like a real system. Root and swap are put directly onto the
69 logical volume, without partitioning it at all. This makes getting to the
70 data from the host easier - no need to run kpartx - and it makes growing
77 apt-get install debootstrap debian-archive-keyring kpartx &&
78 if [ "$use_lvm" = "y" ]; then
79 if [ "$use_boot" != "n" ] ; then
80 lvcreate -L 128m -n "$guest"-boot /dev/"$vg"
82 lvcreate -L 4g -n "$guest"-root /dev/"$vg"
84 lvcreate -L 4g -n "$guest"-swap /dev/"$vg" &&
86 if [ "$use_boot" != "n" ] ; then
87 ( echo ',,L,*' | sfdisk -D "$dev_boot" ) &&
88 kpartx -v -a "$dev_boot" &&
89 mkfs."$fs" "$dev_boot"1
91 mkfs."$fs" "$dev_root" &&
95 mount "$dev_root" "$target" &&
96 if [ "$use_boot" != "n" ] ; then
97 mkdir -p "$target/boot" &&
98 mount "$dev_boot"1 "$target/boot"
102 debootstrap --variant=minbase --keyring=/usr/share/keyrings/debian-archive-keyring.gpg stable . "$mirror"
105 And finalize the setup:
110 echo "$guest" > etc/hostname &&
111 cat > etc/hosts << EOF &&
114 # The following lines are desirable for IPv6 capable hosts
115 ::1 localhost ip6-localhost ip6-loopback
117 ff00::0 ip6-mcastprefix
119 ff02::2 ip6-allrouters
122 rm -fv etc/apt/sources.list &&
123 ( ! [ -e /etc/apt/sources.list ] || cp /etc/apt/sources.list etc/apt/sources.list)
124 (cp -v /etc/apt/sources.list.d/* etc/apt/sources.list.d/ || true ) &&
125 ( ! [ -e /etc/apt/preferences ] || cp -v /etc/apt/preferences etc/apt/ ) &&
126 apt-key exportall | chroot . apt-key add - &&
127 chroot . apt-get update &&
128 echo "Apt::Install-Recommends 0;" > etc/apt/apt.conf.d/local-recommends &&
129 chroot . apt-get install -y net-tools iproute ifupdown dialog vim netbase xfsprogs &&
130 cp -av `readlink -f $dev_root` dev/`basename $dev_root` &&
131 DEBIAN_FRONTEND=noninteractive chroot . apt-get install -y grub2 &&
132 cat > etc/kernel-img.conf << EOF &&
136 if [ "$use_boot" != "n" ] ; then
137 cp -av `readlink -f $dev_boot` dev/`basename $dev_boot` &&
138 cp -av `readlink -f $dev_boot""1` dev/`basename $dev_boot`1 &&
139 chroot . grub-install --modules=part_msdos /dev/`basename $dev_boot` &&
140 # install a kernel image
141 chroot . apt-get install -y linux-image-2.6-amd64 &&
142 sed -i -e 's/^#GRUB_TERMINAL=console/GRUB_TERMINAL=console/' etc/default/grub &&
143 echo "(hd0) /dev/`basename $dev_boot`" > boot/grub/device.map &&
144 chroot . update-grub &&
145 sed -i -e "s#dev/`basename $dev_boot`1#dev/hda1#g" boot/grub/grub.cfg &&
146 rm -v dev/"`basename $dev_boot`" dev/"`basename $dev_boot`1"
148 echo && echo && echo && echo "Hardly tested, expect this to fail." && echo && echo && echo &&
149 echo "(hd0) /dev/`basename $dev_root`" > boot/grub/device.map &&
150 chroot . grub-install /dev/"`basename $dev_root`" &&
151 # install a kernel image
152 chroot . apt-get install -y linux-image-2.6-amd64 &&
153 sed -i -e 's/^#GRUB_TERMINAL=console/GRUB_TERMINAL=console/' etc/default/grub &&
156 sed -i -e "s#dev/`basename $dev_root`#dev/vda#g" boot/grub/grub.cfg &&
157 rm -v boot/grub/device.map &&
158 rm -v dev/"`basename $dev_root`"
161 And a fstab and a boot loader config
164 # doesn't work: chroot . update-grub
165 rootuuid=`blkid -s UUID -o value "$dev_root"` &&
166 swapuuid=`blkid -s UUID -o value "$dev_swap"` &&
167 if [ "$fs" = "ext4" ]; then
168 rootopts="errors=remount-ro"
172 cat > etc/fstab << EOF &&
173 UUID=$rootuuid / $fs $rootopts 0 1
174 UUID=$swapuuid none swap sw 0 0
176 if [ "$use_boot" != "n" ] ; then
177 bootuuid=`blkid -s UUID -o value "$dev_boot"1` &&
178 echo "UUID=$bootuuid /boot $fs defaults 0 2" >> etc/fstab
180 cat > etc/network/interfaces << EOF
182 iface lo inet loopback
185 iface eth0 inet manual
186 pre-up ip link set up dev \$IFACE
187 post-down ip link set down dev \$IFACE
191 Maybe fix/setup networking properly:
194 vi etc/network/interfaces
202 Set a nameserver config that works once the VM has booted. Later in the process we will install unbound anyway.
204 cat > etc/resolv.conf << EOF
214 if [ "$use_boot" != "n" ] ; then
215 umount "$target"/boot &&
216 kpartx -v -d "$dev_boot"
224 Setup a new kvm domain by creating a new file in /etc/dsa-kvm/`hostname/$guest.xml.
226 * Properly configure hostname
227 * Pick a new uuid ({{{uuidgen}}})
228 * Setup block devices properly
229 * pick a new and unique mac address (on d.o every kvm host has their own mac address space and the last block is changed for the guests, as in {{{..:..:..:..:<host byte>:<guest byte>}}}. )
232 ** {{{virsh define foo.xml}}}
233 ** {{{virsh start foo}}}
234 ** {{{virsh destroy foo}}}
235 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host and vnc localhost)
237 === post processing ===
239 Do not forget to set a sane root password before installing ssh in the new kvm domain.
241 === when stuff goes wrong ===
243 To get to the guest data from the host:
247 kpartx -v -a "$dev_boot" &&
248 mkdir -p "$target" &&
249 mount "$dev_root" "$target" &&
250 mkdir -p "$target/boot" &&
251 mount "$dev_boot"1 "$target/boot"
254 and once you're done:
257 umount "$target"/boot &&
259 kpartx -v -d "$dev_boot" &&
263 Make sure that the filesystem isn't being mounted twice - i.e. never start the guest while the filesystems are mounted, and never mount them while the guest is running.