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 "Swap device will be $dev_swap" &&
43 echo "Chosen mirror is $mirror"
55 The way we lay out the filesystems by default is that we have one 4g /
56 filesystem, a swap, and a tiny boot filesystem. On the host we make
57 a new LVM logical volume for each of the three. Only the LV that will
58 take the guest's boot will actually be partitioned - into a single boot
59 partition. That's so we can install grub into the MBR and have the system
60 start just like a real system. Root and swap are put directly onto the
61 logical volume, without partitioning it at all. This makes getting to the
62 data from the host easier - no need to run kpartx - and it makes growing
69 apt-get install debootstrap kpartx &&
70 if [ "$use_lvm" = "y" ]; then
71 lvcreate -L 128m -n "$guest"-boot /dev/"$vg" &&
72 lvcreate -L 4g -n "$guest"-root /dev/"$vg"
74 lvcreate -L 4g -n "$guest"-swap /dev/"$vg" &&
76 ( echo ',,L,*' | sfdisk "$dev_boot" ) &&
77 kpartx -v -a "$dev_boot" &&
78 mkfs."$fs" "$dev_boot"1 &&
79 mkfs."$fs" "$dev_root" &&
83 mount "$dev_root" "$target" &&
84 mkdir -p "$target/boot" &&
85 mount "$dev_boot"1 "$target/boot" &&
88 debootstrap --variant=minbase lenny . "$mirror"
91 And finalize the setup:
96 echo "$guest" > etc/hostname &&
97 cat > etc/hosts << EOF &&
100 # The following lines are desirable for IPv6 capable hosts
101 ::1 localhost ip6-localhost ip6-loopback
103 ff00::0 ip6-mcastprefix
105 ff02::2 ip6-allrouters
108 rm -fv etc/apt/sources.list &&
109 ( ! [ -e /etc/apt/sources.list ] || cp /etc/apt/sources.list etc/apt/sources.list)
110 (cp -v /etc/apt/sources.list.d/* etc/apt/sources.list.d/ || true ) &&
111 cp -v /etc/apt/preferences etc/apt/ &&
112 apt-key exportall | chroot . apt-key add - &&
113 chroot . apt-get update &&
114 echo "Apt::Install-Recommends 0;" > etc/apt/apt.conf.d/local-recommends &&
115 chroot . apt-get install net-tools iproute ifupdown dialog vim netbase xfsprogs &&
116 #chroot . apt-get remove --purge cpp dpkg-dev g++ gcc gcc-4.2-base libatm1 libdevmapper1.02.1 libgpm2 libc6-dev linux-libc-dev libstdc++6-4.3-dev binutils cpp-4.3 gcc-4.3 gettext-base libgmp3c2 libgomp1 libmpfr1ldbl libtimedate-perl
117 # don't - it may start daemons
118 #chroot . apt-get dist-upgrade
119 chroot . apt-get install grub &&
120 cp -av usr/lib/grub/x86_64-pc boot/grub &&
122 device (hd0) $dev_boot
127 # install a kernel image
128 cat > etc/kernel-img.conf << EOF &&
133 chroot . apt-get install linux-image-2.6-amd64 &&
134 cat >> etc/kernel-img.conf << EOF
135 postinst_hook = /usr/sbin/update-grub
136 postrm_hook = /usr/sbin/update-grub
140 And a fstab and a boot loader config
143 # doesn't work: chroot . update-grub
144 rootuuid=`vol_id "$dev_root" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
145 swapuuid=`vol_id "$dev_swap" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
146 bootuuid=`vol_id "$dev_boot"1 | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
147 cat > boot/grub/menu.lst << EOF &&
150 color cyan/blue white/blue
152 ### BEGIN AUTOMAGIC KERNELS LIST
153 # kopt=root=UUID=$rootuuid ro
155 ## ## End Default Options ##
158 kernel /vmlinuz root=UUID=$rootuuid ro
161 ### END DEBIAN AUTOMAGIC KERNELS LIST
163 if [ "$fs" = "ext3" ]; then
164 rootopts="errors=remount-ro"
168 cat > etc/fstab << EOF &&
169 UUID=$rootuuid / $fs $rootopts 0 1
170 UUID=$bootuuid /boot $fs defaults 0 2
171 UUID=$swapuuid none swap sw 0 0
173 cat > etc/network/interfaces << EOF
175 iface lo inet loopback
178 iface eth0 inet manual
179 pre-up ip link set up dev \$IFACE
180 post-down ip link set down dev \$IFACE
184 Maybe fix/setup networking properly:
187 vi etc/network/interfaces
194 umount "$target"/boot &&
196 kpartx -v -d "$dev_boot" &&
202 Setup a new kvm domain by creating a new file in /etc/da-virt/`hostname/$guest.xml.
204 * Properly configure hostname
205 * Pick a new uuid ({{{uuidgen}}})
206 * Setup block devices properly
207 * 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>}}}. )
210 ** {{{virsh define foo.xml}}}
211 ** {{{virsh start foo}}}
212 ** {{{virsh destroy foo}}}
213 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host and vnc localhost)
215 === post processing ===
217 Do not forget to set a sane root password before installing ssh in the new kvm domain.
219 === when stuff goes wrong ===
221 To get to the guest data from the host:
225 kpartx -v -a "$dev_boot" &&
226 mkdir -p "$target" &&
227 mount "$dev_root" "$target" &&
228 mkdir -p "$target/boot" &&
229 mount "$dev_boot"1 "$target/boot"
232 and once you're done:
235 umount "$target"/boot &&
237 kpartx -v -d "$dev_boot" &&
241 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.