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 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 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 if [ "$use_boot" != "n" ] ; then
125 chroot . apt-get install grub &&
126 cp -av usr/lib/grub/x86_64-pc boot/grub &&
128 device (hd0) $dev_boot
133 # install a kernel image
134 cat > etc/kernel-img.conf << EOF &&
139 chroot . apt-get install linux-image-2.6-amd64 &&
140 cat >> etc/kernel-img.conf << EOF
141 postinst_hook = /usr/sbin/update-grub
142 postrm_hook = /usr/sbin/update-grub
145 cat > etc/kernel-img.conf << EOF &&
149 chroot . apt-get install linux-image-2.6-amd64 &&
150 cat >> etc/kernel-img.conf << EOF
151 postinst_hook = update-grub
152 postrm_hook = update-grub
154 chroot . apt-get install grub2 &&
155 cp -av $dev_root dev/ &&
156 echo "(hd0) /dev/`basename $dev_root`" > boot/grub/device.map &&
157 chroot . grub-install /dev/"`basename $dev_root`" &&
158 sed -i -e 's/^#GRUB_TERMINAL=console/GRUB_TERMINAL=console/' etc/default/grub &&
159 chroot . update-grub &&
160 rm -v dev/"`basename $dev_root`" boot/grub/device.map &&
161 sed -i -e "s#dev/`basename $dev_root`#dev/vda#g" boot/grub/grub.cfg
165 And a fstab and a boot loader config
168 # doesn't work: chroot . update-grub
169 rootuuid=`vol_id "$dev_root" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
170 swapuuid=`vol_id "$dev_swap" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
171 if [ "$use_boot" != "n" ] ; then
172 bootuuid=`vol_id "$dev_boot"1 | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
173 cat > boot/grub/menu.lst << EOF
176 color cyan/blue white/blue
178 ### BEGIN AUTOMAGIC KERNELS LIST
179 # kopt=root=UUID=$rootuuid ro
181 ## ## End Default Options ##
184 kernel /vmlinuz root=UUID=$rootuuid ro
187 ### END DEBIAN AUTOMAGIC KERNELS LIST
190 if [ "$fs" = "ext3" ]; then
191 rootopts="errors=remount-ro"
195 cat > etc/fstab << EOF &&
196 UUID=$rootuuid / $fs $rootopts 0 1
197 UUID=$swapuuid none swap sw 0 0
199 if [ "$use_boot" != "n" ] ; then
200 echo "UUID=$bootuuid /boot $fs defaults 0 2" >> etc/fstab
202 cat > etc/network/interfaces << EOF
204 iface lo inet loopback
207 iface eth0 inet manual
208 pre-up ip link set up dev \$IFACE
209 post-down ip link set down dev \$IFACE
213 Maybe fix/setup networking properly:
216 vi etc/network/interfaces
223 if [ "$use_boot" != "n" ] ; then
224 umount "$target"/boot &&
225 kpartx -v -d "$dev_boot"
233 Setup a new kvm domain by creating a new file in /etc/da-virt/`hostname/$guest.xml.
235 * Properly configure hostname
236 * Pick a new uuid ({{{uuidgen}}})
237 * Setup block devices properly
238 * 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>}}}. )
241 ** {{{virsh define foo.xml}}}
242 ** {{{virsh start foo}}}
243 ** {{{virsh destroy foo}}}
244 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host and vnc localhost)
246 === post processing ===
248 Do not forget to set a sane root password before installing ssh in the new kvm domain.
250 === when stuff goes wrong ===
252 To get to the guest data from the host:
256 kpartx -v -a "$dev_boot" &&
257 mkdir -p "$target" &&
258 mount "$dev_root" "$target" &&
259 mkdir -p "$target/boot" &&
260 mount "$dev_boot"1 "$target/boot"
263 and once you're done:
266 umount "$target"/boot &&
268 kpartx -v -d "$dev_boot" &&
272 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.