1 == Setup a new kvm domain fast ==
5 or: how to install Debian.
7 Define a shell function
12 echo -n "New hostname: " &&
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 dev_swap="/dev/mapper/$vg-$guest--swap" &&
41 echo -n "Use a /boot filesystem? [Y/n] " &&
43 echo "Swap device will be $dev_swap" &&
45 echo "Chosen mirror is $mirror"
57 The way we lay out the filesystems by default is that we have one 4g /
58 filesystem, a swap, and a tiny boot filesystem. On the host we make
59 a new LVM logical volume for each of the three. Only the LV that will
60 take the guest's boot will actually be partitioned - into a single boot
61 partition. That's so we can install grub into the MBR and have the system
62 start just like a real system. Root and swap are put directly onto the
63 logical volume, without partitioning it at all. This makes getting to the
64 data from the host easier - no need to run kpartx - and it makes growing
71 apt-get install debootstrap debian-archive-keyring kpartx &&
72 if [ "$use_lvm" = "y" ]; then
73 if [ "$use_boot" != "n" ] ; then
74 lvcreate -L 128m -n "$guest"-boot /dev/"$vg"
76 lvcreate -L 4g -n "$guest"-root /dev/"$vg"
78 lvcreate -L 4g -n "$guest"-swap /dev/"$vg" &&
80 if [ "$use_boot" != "n" ] ; then
81 ( echo ',,L,*' | sfdisk "$dev_boot" ) &&
82 kpartx -v -a "$dev_boot" &&
83 mkfs."$fs" "$dev_boot"1
85 mkfs."$fs" "$dev_root" &&
89 mount "$dev_root" "$target" &&
90 if [ "$use_boot" != "n" ] ; then
91 mkdir -p "$target/boot" &&
92 mount "$dev_boot"1 "$target/boot"
96 debootstrap --variant=minbase --keyring=/usr/share/keyrings/debian-archive-keyring.gpg lenny . "$mirror"
99 And finalize the setup:
104 echo "$guest" > etc/hostname &&
105 cat > etc/hosts << EOF &&
108 # The following lines are desirable for IPv6 capable hosts
109 ::1 localhost ip6-localhost ip6-loopback
111 ff00::0 ip6-mcastprefix
113 ff02::2 ip6-allrouters
116 rm -fv etc/apt/sources.list &&
117 ( ! [ -e /etc/apt/sources.list ] || cp /etc/apt/sources.list etc/apt/sources.list)
118 (cp -v /etc/apt/sources.list.d/* etc/apt/sources.list.d/ || true ) &&
119 cp -v /etc/apt/preferences etc/apt/ &&
120 apt-key exportall | chroot . apt-key add - &&
121 chroot . apt-get update &&
122 echo "Apt::Install-Recommends 0;" > etc/apt/apt.conf.d/local-recommends &&
123 chroot . apt-get install net-tools iproute ifupdown dialog vim netbase xfsprogs &&
124 cp -av $dev_root dev/ &&
125 if [ "$use_boot" != "n" ] ; then
126 DEBIAN_FRONTEND=noninteractive chroot . apt-get install grub &&
127 cp -av $dev_boot $dev_boot""1 dev/ &&
128 echo "(hd0) /dev/`basename $dev_boot`" > boot/grub/device.map &&
129 if [ -e usr/lib/grub/x86_64-pc ] ; then cp -av usr/lib/grub/x86_64-pc boot/grub ;
130 else cp -av usr/lib/grub/i386-pc boot/grub ; fi &&
132 device (hd0) $dev_boot
137 # install a kernel image
138 cat > etc/kernel-img.conf << EOF &&
143 chroot . apt-get install linux-image-2.6-amd64 &&
144 cat >> etc/kernel-img.conf << EOF
145 postinst_hook = /usr/sbin/update-grub
146 postrm_hook = /usr/sbin/update-grub
148 rm -v dev/"`basename $dev_boot`" dev/"`basename $dev_boot`1" boot/grub/device.map &&
150 cat > etc/kernel-img.conf << EOF &&
154 chroot . apt-get install linux-image-2.6-amd64 &&
155 cat >> etc/kernel-img.conf << EOF
156 postinst_hook = update-grub
157 postrm_hook = update-grub
159 DEBIAN_FRONTEND=noninteractive chroot . apt-get install grub2 &&
160 echo "(hd0) /dev/`basename $dev_root`" > boot/grub/device.map &&
161 chroot . grub-install /dev/"`basename $dev_root`" &&
162 sed -i -e 's/^#GRUB_TERMINAL=console/GRUB_TERMINAL=console/' etc/default/grub &&
163 chroot . update-grub &&
164 sed -i -e "s#dev/`basename $dev_root`#dev/vda#g" boot/grub/grub.cfg
165 rm -v boot/grub/device.map
167 rm -v dev/"`basename $dev_root`"
170 And a fstab and a boot loader config
173 # doesn't work: chroot . update-grub
174 rootuuid=`vol_id "$dev_root" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
175 swapuuid=`vol_id "$dev_swap" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
176 if [ "$use_boot" != "n" ] ; then
177 bootuuid=`vol_id "$dev_boot"1 | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
178 cat > boot/grub/menu.lst << EOF
181 color cyan/blue white/blue
183 ### BEGIN AUTOMAGIC KERNELS LIST
184 # kopt=root=UUID=$rootuuid ro
186 ## ## End Default Options ##
189 kernel /vmlinuz root=UUID=$rootuuid ro
192 ### END DEBIAN AUTOMAGIC KERNELS LIST
195 if [ "$fs" = "ext3" ]; then
196 rootopts="errors=remount-ro"
200 cat > etc/fstab << EOF &&
201 UUID=$rootuuid / $fs $rootopts 0 1
202 UUID=$swapuuid none swap sw 0 0
204 if [ "$use_boot" != "n" ] ; then
205 echo "UUID=$bootuuid /boot $fs defaults 0 2" >> etc/fstab
207 cat > etc/network/interfaces << EOF
209 iface lo inet loopback
212 iface eth0 inet manual
213 pre-up ip link set up dev \$IFACE
214 post-down ip link set down dev \$IFACE
218 Maybe fix/setup networking properly:
221 vi etc/network/interfaces
228 if [ "$use_boot" != "n" ] ; then
229 umount "$target"/boot &&
230 kpartx -v -d "$dev_boot"
238 Setup a new kvm domain by creating a new file in /etc/da-virt/`hostname/$guest.xml.
240 * Properly configure hostname
241 * Pick a new uuid ({{{uuidgen}}})
242 * Setup block devices properly
243 * 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>}}}. )
246 ** {{{virsh define foo.xml}}}
247 ** {{{virsh start foo}}}
248 ** {{{virsh destroy foo}}}
249 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host and vnc localhost)
251 === post processing ===
253 Do not forget to set a sane root password before installing ssh in the new kvm domain.
255 === when stuff goes wrong ===
257 To get to the guest data from the host:
261 kpartx -v -a "$dev_boot" &&
262 mkdir -p "$target" &&
263 mount "$dev_root" "$target" &&
264 mkdir -p "$target/boot" &&
265 mount "$dev_boot"1 "$target/boot"
268 and once you're done:
271 umount "$target"/boot &&
273 kpartx -v -d "$dev_boot" &&
277 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.