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" &&
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 &&
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 echo -e "do_symlinks = yes\nlink_in_boot = yes\ndo_initrd = yes" > etc/kernel-img.conf &&
127 chroot . apt-get install linux-image-2.6-amd64
131 And a fstab and a boot loader config
134 # doesn't work: chroot . update-grub
135 rootuuid=`vol_id "$dev_root" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
136 swapuuid=`vol_id "$dev_swap" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
137 bootuuid=`vol_id "$dev_boot"1 | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
138 cat > boot/grub/menu.lst << EOF &&
141 color cyan/blue white/blue
143 ### BEGIN AUTOMAGIC KERNELS LIST
144 # kopt=root=UUID=$rootuuid ro
146 ## ## End Default Options ##
149 kernel /vmlinuz root=UUID=$rootuuid ro
152 ### END DEBIAN AUTOMAGIC KERNELS LIST
154 cat > etc/fstab << EOF &&
155 UUID=$rootuuid / ext3 errors=remount-ro 0 1
156 UUID=$bootuuid /boot ext3 defaults 0 2
157 UUID=$swapuuid none swap sw 0 0
159 cat > etc/network/interfaces << EOF
161 iface lo inet loopback
164 iface eth0 inet manual
165 pre-up ip link set up dev \$IFACE
166 post-down ip link set down dev \$IFACE
170 Maybe fix/setup networking properly:
173 vi etc/network/interfaces
180 umount "$target"/boot &&
182 kpartx -v -d "$dev_boot" &&
188 Setup a new kvm domain by creating a new file in /etc/da-virt/`hostname/$guest.xml.
190 * Properly configure hostname
191 * Pick a new uuid ({{{uuidgen}}})
192 * Setup block devices properly
193 * 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>}}}. )
196 ** {{{virsh define foo.xml}}}
197 ** {{{virsh start foo}}}
198 ** {{{virsh destroy foo}}}
199 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host and vnc localhost)
201 === post processing ===
203 Do not forget to set a sane root password before installing ssh in the new kvm domain.
205 === when stuff goes wrong ===
207 To get to the guest data from the host:
211 kpartx -v -a "$dev_boot" &&
212 mkdir -p "$target" &&
213 mount "$dev_root" "$target" &&
214 mkdir -p "$target/boot" &&
215 mount "$dev_boot"1 "$target/boot"
218 and once you're done:
221 umount "$target"/boot &&
223 kpartx -v -d "$dev_boot" &&
227 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.