From 5aa2dee5eaffc1d7064f9a76d07947d0a19128b9 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Thu, 2 Apr 2009 12:39:24 +0200 Subject: [PATCH] Why we do partitions the way we do, how to get at the data when stuff blows up and call the guest a guest, not host. --- input/howto/install-kvm.creole | 78 ++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/input/howto/install-kvm.creole b/input/howto/install-kvm.creole index e648ec7..e4c760d 100644 --- a/input/howto/install-kvm.creole +++ b/input/howto/install-kvm.creole @@ -1,5 +1,9 @@ == Setup a new kvm domain fast == +=== guest setup === + +or: how to install Debian. + Define a shell function {{{ @@ -7,7 +11,7 @@ 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'` && @@ -24,25 +28,35 @@ setup_env 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" @@ -53,7 +67,7 @@ And finalize the setup: {{{ ####### # finish setup - echo "$host" > etc/hostname && + echo "$guest" > etc/hostname && cat > etc/hosts << EOF && 127.0.0.1 localhost @@ -79,7 +93,7 @@ EOF 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 @@ -94,9 +108,9 @@ And a fstab and a boot loader config {{{ # 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 @@ -141,18 +155,46 @@ And unmount: 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 {{{..:..:..:..::}}}. ) * 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. -- 2.20.1