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