== Setup a new kvm domain fast ==
+=== guest setup ===
+
+or: how to install Debian.
+
Define a shell function
{{{
setup_env() {
echo -n "New hostname: " &&
export LC_ALL=C &&
- read host &&
+ read guest &&
target=/mnt/target &&
mirror=`cat /etc/apt/sources.list /etc/apt/sources.list.d/debian.list 2>/dev/null | awk '/^deb.*debian/ {print $2; exit}'` &&
vg=`vgdisplay -c | awk -F: '{print $1;exit}' | sed 's/ *//g'` &&
Then install base:
+The way we lay out the filesystems by default is that we have one 4g /
+filesystem, a swap, and a tiny boot filesystem. On the host we make
+a new LVM logical volume for each of the three. Only the LV that will
+take the guest's boot will actually be partitioned - into a single boot
+partition. That's so we can install grub into the MBR and have the system
+start just like a real system. Root and swap are put directly onto the
+logical volume, without partitioning it at all. This makes getting to the
+data from the host easier - no need to run kpartx - and it makes growing
+trivial.
+
{{{
#######
# install base
apt-get install debootstrap kpartx &&
- lvcreate -L 128m -n "$host"-boot /dev/"$vg" &&
- lvcreate -L 4g -n "$host"-root /dev/"$vg" &&
- lvcreate -L 4g -n "$host"-swap /dev/"$vg" &&
+ lvcreate -L 128m -n "$guest"-boot /dev/"$vg" &&
+ lvcreate -L 4g -n "$guest"-root /dev/"$vg" &&
+ lvcreate -L 4g -n "$guest"-swap /dev/"$vg" &&
: &&
- ( echo ',,L,*' | sfdisk /dev/"$vg"/"$host"-boot ) &&
- kpartx -v -a /dev/"$vg"/"$host"-boot &&
- mkfs.ext3 /dev/mapper/"$vg"-"$host"--boot1 &&
- mkfs.ext3 /dev/"$vg"/"$host"-root &&
- mkswap /dev/"$vg"/"$host"-swap &&
+ ( echo ',,L,*' | sfdisk /dev/"$vg"/"$guest"-boot ) &&
+ kpartx -v -a /dev/"$vg"/"$guest"-boot &&
+ mkfs.ext3 /dev/mapper/"$vg"-"$guest"--boot1 &&
+ mkfs.ext3 /dev/"$vg"/"$guest"-root &&
+ mkswap /dev/"$vg"/"$guest"-swap &&
: &&
mkdir -p "$target" &&
- mount /dev/"$vg"/"$host"-root "$target" &&
+ mount /dev/"$vg"/"$guest"-root "$target" &&
mkdir -p "$target/boot" &&
- mount /dev/mapper/"$vg"-"$host"--boot1 "$target/boot" &&
+ mount /dev/mapper/"$vg"-"$guest"--boot1 "$target/boot" &&
cd "$target" &&
debootstrap --variant=minbase lenny . "$mirror"
{{{
#######
# finish setup
- echo "$host" > etc/hostname &&
+ echo "$guest" > etc/hostname &&
cat > etc/hosts << EOF &&
127.0.0.1 localhost
chroot . apt-get install grub &&
cp -av usr/lib/grub/x86_64-pc boot/grub &&
grub << EOF &&
-device (hd0) /dev/mapper/$vg-$host--boot
+device (hd0) /dev/mapper/$vg-$guest--boot
root (hd0,0)
setup (hd0)
quit
{{{
# doesn't work: chroot . update-grub
- rootuuid=`vol_id /dev/"$vg"/"$host"-root "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
- swapuuid=`vol_id /dev/"$vg"/"$host"-swap "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
- bootuuid=`vol_id /dev/mapper/"$vg"-"$host"--boot1 | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
+ rootuuid=`vol_id /dev/"$vg"/"$guest"-root "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
+ swapuuid=`vol_id /dev/"$vg"/"$guest"-swap "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
+ bootuuid=`vol_id /dev/mapper/"$vg"-"$guest"--boot1 | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
cat > boot/grub/menu.lst << EOF &&
default 0
timeout 5
cd / &&
umount "$target"/boot &&
umount "$target" &&
- kpartx -v -d /dev/"$vg"/"$host"-boot &&
+ kpartx -v -d /dev/"$vg"/"$guest"-boot &&
rmdir "$target"
}}}
-Setup a new kvm domain by creating a new file in /etc/dm-virt/`hostname/$host.xml.
+=== virsh setup ===
+
+Setup a new kvm domain by creating a new file in /etc/dm-virt/`hostname/$guest.xml.
* Properly configure hostname
* Pick a new uuid ({{{uuidgen}}})
* Setup block devices properly
* 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>}}}. )
* virsh commands:
+** {{{virsh help}}}
** {{{virsh define foo.xml}}}
** {{{virsh start foo}}}
** {{{virsh destroy foo}}}
** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host and vnc localhost)
+
+
+=== when stuff goes wrong ===
+
+To get to the guest data from the host:
+
+{{{
+ setup_env
+ kpartx -v -a /dev/"$vg"/"$guest"-boot &&
+ mkdir -p "$target" &&
+ mount /dev/"$vg"/"$guest"-root "$target" &&
+ mkdir -p "$target/boot" &&
+ mount /dev/mapper/"$vg"-"$guest"--boot1 "$target/boot"
+}}}
+
+and once you're done:
+{{{
+ cd / &&
+ umount "$target"/boot &&
+ umount "$target" &&
+ kpartx -v -d /dev/"$vg"/"$guest"-boot &&
+ rmdir "$target"
+}}}
+
+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.