handle new grub brokenness
[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  vgdefault=`vgdisplay -c | awk -F: '{print $1;exit}' | sed 's/ *//g'` &&
18  echo -n "Volume group? [$vgdefault]: " &&
19  read vg &&
20  if [ "$vg" = "" ]; then vg="$vgdefault"; fi &&
21  echo -n "Use lvm for non-swap partitions? [Y/n] " &&
22  read use_lvm &&
23  if [ "$use_lvm" = "n" ]; then
24    : SAN, as in the msa2012i at ubcece &&
25    : requires that it is already setup &&
26    dev_root="/dev/mapper/$guest-root" &&
27    dev_boot="/dev/mapper/$guest-boot" &&
28    echo "Root device will be $dev_root" &&
29    echo "Boot device will be $dev_boot" &&
30    echo "Make sure they exist already." &&
31    fs=xfs
32  else
33    use_lvm="y" &&
34    dev_root="/dev/mapper/$vg-$guest--root" &&
35    dev_boot="/dev/mapper/$vg-$guest--boot" &&
36    echo "Root device will be $dev_root" &&
37    echo "Boot device will be $dev_boot" &&
38    fs=ext3
39  fi &&
40  dev_swap="/dev/mapper/$vg-$guest--swap" &&
41  echo -n "Use a /boot filesystem? [Y/n] " &&
42  read use_boot &&
43  echo "Swap device will be $dev_swap" &&
44  echo "fs is $fs"
45  echo "Chosen mirror is $mirror"
46 }
47 }}}
48
49 And run it
50
51 {{{
52 setup_env
53 }}}
54
55 Then install base:
56
57 The way we lay out the filesystems by default is that we have one 4g /
58 filesystem, a swap, and a tiny boot filesystem.  On the host we make
59 a new LVM logical volume for each of the three.  Only the LV that will
60 take the guest's boot will actually be partitioned - into a single boot
61 partition.  That's so we can install grub into the MBR and have the system
62 start just like a real system.  Root and swap are put directly onto the
63 logical volume, without partitioning it at all.  This makes getting to the
64 data from the host easier - no need to run kpartx - and it makes growing
65 trivial.
66
67 {{{
68 #######
69 # install base
70
71  apt-get install debootstrap debian-archive-keyring kpartx &&
72  if [ "$use_lvm" = "y" ]; then
73    if [ "$use_boot" != "n" ] ; then
74      lvcreate -L 128m -n "$guest"-boot /dev/"$vg"
75    fi &&
76    lvcreate -L 4g -n "$guest"-root /dev/"$vg"
77  fi &&
78  lvcreate -L 4g -n "$guest"-swap /dev/"$vg" &&
79  : &&
80  if [ "$use_boot" != "n" ] ; then
81    ( echo ',,L,*' | sfdisk "$dev_boot" ) &&
82    kpartx -v -a "$dev_boot" &&
83    mkfs."$fs" "$dev_boot"1
84  fi &&
85  mkfs."$fs" "$dev_root" &&
86  mkswap "$dev_swap" &&
87  : &&
88  mkdir -p "$target" &&
89  mount "$dev_root" "$target" &&
90  if [ "$use_boot" != "n" ] ; then
91    mkdir -p "$target/boot" &&
92    mount "$dev_boot"1 "$target/boot"
93  fi &&
94
95  cd "$target" &&
96  debootstrap --variant=minbase --keyring=/usr/share/keyrings/debian-archive-keyring.gpg lenny . "$mirror"
97 }}}
98
99 And finalize the setup:
100
101 {{{
102 #######
103 # finish setup
104  echo "$guest" > etc/hostname &&
105  cat > etc/hosts << EOF &&
106 127.0.0.1       localhost
107
108 # The following lines are desirable for IPv6 capable hosts
109 ::1     localhost ip6-localhost ip6-loopback
110 fe00::0 ip6-localnet
111 ff00::0 ip6-mcastprefix
112 ff02::1 ip6-allnodes
113 ff02::2 ip6-allrouters
114 ff02::3 ip6-allhosts
115 EOF
116  rm -fv etc/apt/sources.list &&
117  ( ! [ -e /etc/apt/sources.list ] || cp /etc/apt/sources.list etc/apt/sources.list)
118  (cp -v /etc/apt/sources.list.d/* etc/apt/sources.list.d/ || true ) &&
119  cp -v /etc/apt/preferences etc/apt/ &&
120  apt-key exportall | chroot . apt-key add - &&
121  chroot . apt-get update &&
122  echo "Apt::Install-Recommends 0;" > etc/apt/apt.conf.d/local-recommends &&
123  chroot . apt-get install net-tools iproute ifupdown dialog vim netbase xfsprogs &&
124  cp -av $dev_root dev/ &&
125  if [ "$use_boot" != "n" ] ; then
126    DEBIAN_FRONTEND=noninteractive chroot . apt-get install grub &&
127    cp -av $dev_boot $dev_boot""1 dev/ &&
128    echo "(hd0) /dev/`basename $dev_boot`" > boot/grub/device.map &&
129    if [ -e usr/lib/grub/x86_64-pc ] ; then cp -av usr/lib/grub/x86_64-pc boot/grub ;
130                                       else cp -av usr/lib/grub/i386-pc boot/grub ; fi &&
131    grub << EOF &&
132 device (hd0) $dev_boot
133 root (hd0,0)
134 setup (hd0)
135 quit
136 EOF
137    # install a kernel image
138    cat > etc/kernel-img.conf << EOF &&
139 do_symlinks = yes
140 link_in_boot = yes
141 do_initrd = yes
142 EOF
143    chroot . apt-get install linux-image-2.6-amd64 &&
144    cat >> etc/kernel-img.conf << EOF
145 postinst_hook = /usr/sbin/update-grub
146 postrm_hook   = /usr/sbin/update-grub
147 EOF
148     rm -v dev/"`basename $dev_boot`" dev/"`basename $dev_boot`1" boot/grub/device.map &&
149   else
150     cat > etc/kernel-img.conf << EOF &&
151 do_symlinks = no
152 do_initrd = yes
153 EOF
154     chroot . apt-get install linux-image-2.6-amd64 &&
155    cat >> etc/kernel-img.conf << EOF
156 postinst_hook = update-grub
157 postrm_hook   = update-grub
158 EOF
159     DEBIAN_FRONTEND=noninteractive chroot . apt-get install grub2 &&
160     echo "(hd0) /dev/`basename $dev_root`" > boot/grub/device.map &&
161     chroot . grub-install /dev/"`basename $dev_root`" &&
162     sed -i -e 's/^#GRUB_TERMINAL=console/GRUB_TERMINAL=console/' etc/default/grub &&
163     chroot . update-grub &&
164     sed -i -e "s#dev/`basename $dev_root`#dev/vda#g" boot/grub/grub.cfg
165     rm -v boot/grub/device.map
166   fi &&
167   rm -v dev/"`basename $dev_root`"
168 }}}
169
170 And a fstab and a boot loader config
171
172 {{{
173  # doesn't work: chroot . update-grub
174  rootuuid=`vol_id "$dev_root" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
175  swapuuid=`vol_id "$dev_swap" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
176  if [ "$use_boot" != "n" ] ; then
177    bootuuid=`vol_id "$dev_boot"1  | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
178    cat > boot/grub/menu.lst << EOF
179 default 0
180 timeout 5
181 color cyan/blue white/blue
182
183 ### BEGIN AUTOMAGIC KERNELS LIST
184 # kopt=root=UUID=$rootuuid ro
185
186 ## ## End Default Options ##
187 title Debian
188 root (hd0,0)
189 kernel /vmlinuz root=UUID=$rootuuid ro
190 initrd /initrd.img
191
192 ### END DEBIAN AUTOMAGIC KERNELS LIST
193 EOF
194  fi &&
195  if [ "$fs" = "ext3" ]; then
196    rootopts="errors=remount-ro"
197  else
198    rootopts="defaults"
199  fi
200  cat > etc/fstab << EOF &&
201 UUID=$rootuuid    /               $fs    $rootopts 0       1
202 UUID=$swapuuid    none            swap    sw              0       0
203 EOF
204  if [ "$use_boot" != "n" ] ; then
205     echo "UUID=$bootuuid    /boot           $fs    defaults        0       2" >> etc/fstab
206  fi
207  cat > etc/network/interfaces << EOF
208 auto lo
209 iface lo inet loopback
210
211 auto eth0
212 iface eth0 inet manual
213   pre-up ip link set up dev \$IFACE
214   post-down ip link set down dev \$IFACE
215 EOF
216 }}}
217
218 Maybe fix/setup networking properly:
219
220 {{{
221 vi etc/network/interfaces
222 }}}
223
224 And unmount:
225
226 {{{
227   cd / &&
228  if [ "$use_boot" != "n" ] ; then
229    umount "$target"/boot &&
230    kpartx -v -d "$dev_boot"
231  fi &&
232  umount "$target" &&
233  rmdir "$target"
234 }}}
235
236 === virsh setup ===
237
238 Setup a new kvm domain by creating a new file in /etc/da-virt/`hostname/$guest.xml.
239
240 * Properly configure hostname
241 * Pick a new uuid ({{{uuidgen}}})
242 * Setup block devices properly
243 * 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>}}}. )
244 * virsh commands:
245 ** {{{virsh help}}}
246 ** {{{virsh define foo.xml}}}
247 ** {{{virsh start foo}}}
248 ** {{{virsh destroy foo}}}
249 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host  and  vnc localhost)
250
251 === post processing ===
252
253 Do not forget to set a sane root password before installing ssh in the new kvm domain.
254
255 === when stuff goes wrong ===
256
257 To get to the guest data from the host:
258
259 {{{
260   setup_env
261  kpartx -v -a "$dev_boot" &&
262  mkdir -p "$target" &&
263  mount "$dev_root" "$target" &&
264  mkdir -p "$target/boot" &&
265  mount "$dev_boot"1 "$target/boot"
266 }}}
267
268 and once you're done:
269 {{{
270  cd / &&
271  umount "$target"/boot &&
272  umount "$target" &&
273  kpartx -v -d "$dev_boot" &&
274  rmdir "$target"
275 }}}
276
277 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.