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 "Chosen mirror is $mirror"
31 The way we lay out the filesystems by default is that we have one 4g /
32 filesystem, a swap, and a tiny boot filesystem. On the host we make
33 a new LVM logical volume for each of the three. Only the LV that will
34 take the guest's boot will actually be partitioned - into a single boot
35 partition. That's so we can install grub into the MBR and have the system
36 start just like a real system. Root and swap are put directly onto the
37 logical volume, without partitioning it at all. This makes getting to the
38 data from the host easier - no need to run kpartx - and it makes growing
45 apt-get install debootstrap kpartx &&
46 lvcreate -L 128m -n "$guest"-boot /dev/"$vg" &&
47 lvcreate -L 4g -n "$guest"-root /dev/"$vg" &&
48 lvcreate -L 4g -n "$guest"-swap /dev/"$vg" &&
50 ( echo ',,L,*' | sfdisk /dev/"$vg"/"$guest"-boot ) &&
51 kpartx -v -a /dev/"$vg"/"$guest"-boot &&
52 mkfs.ext3 /dev/mapper/"$vg"-"$guest"--boot1 &&
53 mkfs.ext3 /dev/"$vg"/"$guest"-root &&
54 mkswap /dev/"$vg"/"$guest"-swap &&
57 mount /dev/"$vg"/"$guest"-root "$target" &&
58 mkdir -p "$target/boot" &&
59 mount /dev/mapper/"$vg"-"$guest"--boot1 "$target/boot" &&
62 debootstrap --variant=minbase lenny . "$mirror"
65 And finalize the setup:
70 echo "$guest" > etc/hostname &&
71 cat > etc/hosts << EOF &&
74 # The following lines are desirable for IPv6 capable hosts
75 ::1 localhost ip6-localhost ip6-loopback
77 ff00::0 ip6-mcastprefix
79 ff02::2 ip6-allrouters
82 rm -fv etc/apt/sources.list &&
83 ( ! [ -e /etc/apt/sources.list ] || cp /etc/apt/sources.list etc/apt/sources.list)
84 (cp -v /etc/apt/sources.list.d/* etc/apt/sources.list.d/ || true ) &&
85 cp -v /etc/apt/preferences etc/apt/ &&
86 apt-key exportall | chroot . apt-key add - &&
87 chroot . apt-get update &&
88 echo "Apt::Install-Recommends 0;" > etc/apt/apt.conf.d/local-recommends &&
89 chroot . apt-get install net-tools iproute ifupdown dialog vim netbase &&
90 #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
91 # don't - it may start daemons
92 #chroot . apt-get dist-upgrade
93 chroot . apt-get install grub &&
94 cp -av usr/lib/grub/x86_64-pc boot/grub &&
96 device (hd0) /dev/mapper/$vg-$guest--boot
101 # install a kernel image
102 echo -e "do_symlinks = yes\nlink_in_boot = yes\ndo_initrd = yes" > etc/kernel-img.conf &&
103 chroot . apt-get install linux-image-2.6-amd64
107 And a fstab and a boot loader config
110 # doesn't work: chroot . update-grub
111 rootuuid=`vol_id /dev/"$vg"/"$guest"-root "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
112 swapuuid=`vol_id /dev/"$vg"/"$guest"-swap "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
113 bootuuid=`vol_id /dev/mapper/"$vg"-"$guest"--boot1 | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
114 cat > boot/grub/menu.lst << EOF &&
117 color cyan/blue white/blue
119 ### BEGIN AUTOMAGIC KERNELS LIST
120 # kopt=root=UUID=$rootuuid ro
122 ## ## End Default Options ##
125 kernel /vmlinuz root=UUID=$rootuuid ro
128 ### END DEBIAN AUTOMAGIC KERNELS LIST
130 cat > etc/fstab << EOF &&
131 UUID=$rootuuid / ext3 errors=remount-ro 0 1
132 UUID=$bootuuid /boot ext3 defaults 0 2
133 UUID=$swapuuid none swap sw 0 0
135 cat > etc/network/interfaces << EOF
137 iface lo inet loopback
140 iface eth0 inet manual
141 pre-up ip link set up dev \$IFACE
142 post-down ip link set down dev \$IFACE
146 Maybe fix/setup networking properly:
149 vi etc/network/interfaces
156 umount "$target"/boot &&
158 kpartx -v -d /dev/"$vg"/"$guest"-boot &&
164 Setup a new kvm domain by creating a new file in /etc/dm-virt/`hostname/$guest.xml.
166 * Properly configure hostname
167 * Pick a new uuid ({{{uuidgen}}})
168 * Setup block devices properly
169 * 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>}}}. )
172 ** {{{virsh define foo.xml}}}
173 ** {{{virsh start foo}}}
174 ** {{{virsh destroy foo}}}
175 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host and vnc localhost)
178 === when stuff goes wrong ===
180 To get to the guest data from the host:
184 kpartx -v -a /dev/"$vg"/"$guest"-boot &&
185 mkdir -p "$target" &&
186 mount /dev/"$vg"/"$guest"-root "$target" &&
187 mkdir -p "$target/boot" &&
188 mount /dev/mapper/"$vg"-"$guest"--boot1 "$target/boot"
191 and once you're done:
194 umount "$target"/boot &&
196 kpartx -v -d /dev/"$vg"/"$guest"-boot &&
200 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.