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 vg=`vgdisplay -c | awk -F: '{print $1;exit}' | sed 's/ *//g'` &&
18 echo "Chosen vg is $vg" &&
19 echo -n "Use lvm for non-swap partitions? [Y/n] " &&
21 if [ "$use_lvm" = "n" ]; then
22 : SAN, as in the msa2012i at ubcece &&
23 : requires that it is already setup &&
24 dev_root="/dev/mapper/$guest-root" &&
25 dev_boot="/dev/mapper/$guest-boot" &&
26 echo "Root device will be $dev_root" &&
27 echo "Boot device will be $dev_boot" &&
28 echo "Make sure they exist already." &&
32 dev_root="/dev/mapper/$vg-$guest--root" &&
33 dev_boot="/dev/mapper/$vg-$guest--boot" &&
34 echo "Root device will be $dev_root" &&
35 echo "Boot device will be $dev_boot" &&
38 dev_swap="/dev/mapper/$vg-$guest--swap" &&
39 echo "Swap device will be $dev_swap" &&
41 echo "Chosen mirror is $mirror"
53 The way we lay out the filesystems by default is that we have one 4g /
54 filesystem, a swap, and a tiny boot filesystem. On the host we make
55 a new LVM logical volume for each of the three. Only the LV that will
56 take the guest's boot will actually be partitioned - into a single boot
57 partition. That's so we can install grub into the MBR and have the system
58 start just like a real system. Root and swap are put directly onto the
59 logical volume, without partitioning it at all. This makes getting to the
60 data from the host easier - no need to run kpartx - and it makes growing
67 apt-get install debootstrap kpartx &&
68 if [ "$use_lvm" = "y" ]; then
69 lvcreate -L 128m -n "$guest"-boot /dev/"$vg" &&
70 lvcreate -L 4g -n "$guest"-root /dev/"$vg"
72 lvcreate -L 4g -n "$guest"-swap /dev/"$vg" &&
74 ( echo ',,L,*' | sfdisk "$dev_boot" ) &&
75 kpartx -v -a "$dev_boot" &&
76 mkfs."$fs" "$dev_boot"1 &&
77 mkfs."$fs" "$dev_root" &&
81 mount "$dev_root" "$target" &&
82 mkdir -p "$target/boot" &&
83 mount "$dev_boot"1 "$target/boot" &&
86 debootstrap --variant=minbase lenny . "$mirror"
89 And finalize the setup:
94 echo "$guest" > etc/hostname &&
95 cat > etc/hosts << EOF &&
98 # The following lines are desirable for IPv6 capable hosts
99 ::1 localhost ip6-localhost ip6-loopback
101 ff00::0 ip6-mcastprefix
103 ff02::2 ip6-allrouters
106 rm -fv etc/apt/sources.list &&
107 ( ! [ -e /etc/apt/sources.list ] || cp /etc/apt/sources.list etc/apt/sources.list)
108 (cp -v /etc/apt/sources.list.d/* etc/apt/sources.list.d/ || true ) &&
109 cp -v /etc/apt/preferences etc/apt/ &&
110 apt-key exportall | chroot . apt-key add - &&
111 chroot . apt-get update &&
112 echo "Apt::Install-Recommends 0;" > etc/apt/apt.conf.d/local-recommends &&
113 chroot . apt-get install net-tools iproute ifupdown dialog vim netbase xfsprogs &&
114 #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
115 # don't - it may start daemons
116 #chroot . apt-get dist-upgrade
117 chroot . apt-get install grub &&
118 cp -av usr/lib/grub/x86_64-pc boot/grub &&
120 device (hd0) $dev_boot
125 # install a kernel image
126 cat > etc/kernel-img.conf << EOF &&
131 chroot . apt-get install linux-image-2.6-amd64 &&
132 cat >> etc/kernel-img.conf << EOF
133 postinst_hook = /usr/sbin/update-grub
134 postrm_hook = /usr/sbin/update-grub
139 And a fstab and a boot loader config
142 # doesn't work: chroot . update-grub
143 rootuuid=`vol_id "$dev_root" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
144 swapuuid=`vol_id "$dev_swap" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
145 bootuuid=`vol_id "$dev_boot"1 | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
146 cat > boot/grub/menu.lst << EOF &&
149 color cyan/blue white/blue
151 ### BEGIN AUTOMAGIC KERNELS LIST
152 # kopt=root=UUID=$rootuuid ro
154 ## ## End Default Options ##
157 kernel /vmlinuz root=UUID=$rootuuid ro
160 ### END DEBIAN AUTOMAGIC KERNELS LIST
162 if [ "$fs" = "ext3" ]; then
163 rootopts="errors=remount-ro"
167 cat > etc/fstab << EOF &&
168 UUID=$rootuuid / $fs $rootopts 0 1
169 UUID=$bootuuid /boot $fs defaults 0 2
170 UUID=$swapuuid none swap sw 0 0
172 cat > etc/network/interfaces << EOF
174 iface lo inet loopback
177 iface eth0 inet manual
178 pre-up ip link set up dev \$IFACE
179 post-down ip link set down dev \$IFACE
183 Maybe fix/setup networking properly:
186 vi etc/network/interfaces
193 umount "$target"/boot &&
195 kpartx -v -d "$dev_boot" &&
201 Setup a new kvm domain by creating a new file in /etc/da-virt/`hostname/$guest.xml.
203 * Properly configure hostname
204 * Pick a new uuid ({{{uuidgen}}})
205 * Setup block devices properly
206 * 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>}}}. )
209 ** {{{virsh define foo.xml}}}
210 ** {{{virsh start foo}}}
211 ** {{{virsh destroy foo}}}
212 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host and vnc localhost)
214 === post processing ===
216 Do not forget to set a sane root password before installing ssh in the new kvm domain.
218 === when stuff goes wrong ===
220 To get to the guest data from the host:
224 kpartx -v -a "$dev_boot" &&
225 mkdir -p "$target" &&
226 mount "$dev_root" "$target" &&
227 mkdir -p "$target/boot" &&
228 mount "$dev_boot"1 "$target/boot"
231 and once you're done:
234 umount "$target"/boot &&
236 kpartx -v -d "$dev_boot" &&
240 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.