Why we do partitions the way we do,
[mirror/dsa-wiki.git] / input / howto / install-kvm.creole
1 == Setup a new kvm domain fast ==
2
3 === guest setup ===
4
5 or: how to install Debian.
6
7 Define a shell function
8
9 {{{
10 # set up environment
11 setup_env() {
12  echo -n "New hostname: " &&
13  export LC_ALL=C &&
14  read guest &&
15  target=/mnt/target &&
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"
20 }
21 }}}
22
23 And run it
24
25 {{{
26 setup_env
27 }}}
28
29 Then install base:
30
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
39 trivial.
40
41 {{{
42 #######
43 # install base
44
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" &&
49  : &&
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 &&
55  : &&
56  mkdir -p "$target" &&
57  mount /dev/"$vg"/"$guest"-root "$target" &&
58  mkdir -p "$target/boot" &&
59  mount /dev/mapper/"$vg"-"$guest"--boot1 "$target/boot" &&
60
61  cd "$target" &&
62  debootstrap --variant=minbase lenny . "$mirror"
63 }}}
64
65 And finalize the setup:
66
67 {{{
68 #######
69 # finish setup
70  echo "$guest" > etc/hostname &&
71  cat > etc/hosts << EOF &&
72 127.0.0.1       localhost
73
74 # The following lines are desirable for IPv6 capable hosts
75 ::1     localhost ip6-localhost ip6-loopback
76 fe00::0 ip6-localnet
77 ff00::0 ip6-mcastprefix
78 ff02::1 ip6-allnodes
79 ff02::2 ip6-allrouters
80 ff02::3 ip6-allhosts
81 EOF
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 &&
95  grub << EOF &&
96 device (hd0) /dev/mapper/$vg-$guest--boot
97 root (hd0,0)
98 setup (hd0)
99 quit
100 EOF
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
104
105 }}}
106
107 And a fstab and a boot loader config
108
109 {{{
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 &&
115 default 0
116 timeout 5
117 color cyan/blue white/blue
118
119 ### BEGIN AUTOMAGIC KERNELS LIST
120 # kopt=root=UUID=$rootuuid ro
121
122 ## ## End Default Options ##
123 title Debian
124 root (hd0,0)
125 kernel /vmlinuz root=UUID=$rootuuid ro
126 initrd /initrd.img
127
128 ### END DEBIAN AUTOMAGIC KERNELS LIST
129 EOF
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
134 EOF
135  cat > etc/network/interfaces << EOF
136 auto lo
137 iface lo inet loopback
138
139 auto eth0
140 iface eth0 inet manual
141   pre-up ip link set up dev \$IFACE
142   post-down ip link set down dev \$IFACE
143 EOF
144 }}}
145
146 Maybe fix/setup networking properly:
147
148 {{{
149 vi etc/network/interfaces
150 }}}
151
152 And unmount:
153
154 {{{
155   cd / &&
156  umount "$target"/boot &&
157  umount "$target" &&
158  kpartx -v -d /dev/"$vg"/"$guest"-boot &&
159  rmdir "$target"
160 }}}
161
162 === virsh setup ===
163
164 Setup a new kvm domain by creating a new file in /etc/dm-virt/`hostname/$guest.xml.
165
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>}}}. )
170 * virsh commands:
171 ** {{{virsh help}}}
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)
176
177
178 === when stuff goes wrong ===
179
180 To get to the guest data from the host:
181
182 {{{
183   setup_env
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"
189 }}}
190
191 and once you're done:
192 {{{
193  cd / &&
194  umount "$target"/boot &&
195  umount "$target" &&
196  kpartx -v -d /dev/"$vg"/"$guest"-boot &&
197  rmdir "$target"
198 }}}
199
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.