debootstrap does not verify stuff by default. How broken is that
[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  if [ "$use_boot" != "n" ] ; then
125    chroot . apt-get install grub &&
126    cp -av usr/lib/grub/x86_64-pc boot/grub &&
127    grub << EOF &&
128 device (hd0) $dev_boot
129 root (hd0,0)
130 setup (hd0)
131 quit
132 EOF
133    # install a kernel image
134    cat > etc/kernel-img.conf << EOF &&
135 do_symlinks = yes
136 link_in_boot = yes
137 do_initrd = yes
138 EOF
139    chroot . apt-get install linux-image-2.6-amd64 &&
140    cat >> etc/kernel-img.conf << EOF
141 postinst_hook = /usr/sbin/update-grub
142 postrm_hook   = /usr/sbin/update-grub
143 EOF
144   else
145     cat > etc/kernel-img.conf << EOF &&
146 do_symlinks = no
147 do_initrd = yes
148 EOF
149     chroot . apt-get install linux-image-2.6-amd64 &&
150    cat >> etc/kernel-img.conf << EOF
151 postinst_hook = update-grub
152 postrm_hook   = update-grub
153 EOF
154     chroot . apt-get install grub2 &&
155     cp -av $dev_root dev/ &&
156     echo "(hd0) /dev/`basename $dev_root`" > boot/grub/device.map &&
157     chroot . grub-install /dev/"`basename $dev_root`" &&
158     sed -i -e 's/^#GRUB_TERMINAL=console/GRUB_TERMINAL=console/' etc/default/grub &&
159     chroot . update-grub &&
160     rm -v dev/"`basename $dev_root`" boot/grub/device.map &&
161     sed -i -e "s#dev/`basename $dev_root`#dev/vda#g" boot/grub/grub.cfg
162   fi
163 }}}
164
165 And a fstab and a boot loader config
166
167 {{{
168  # doesn't work: chroot . update-grub
169  rootuuid=`vol_id "$dev_root" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
170  swapuuid=`vol_id "$dev_swap" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
171  if [ "$use_boot" != "n" ] ; then
172    bootuuid=`vol_id "$dev_boot"1  | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
173    cat > boot/grub/menu.lst << EOF
174 default 0
175 timeout 5
176 color cyan/blue white/blue
177
178 ### BEGIN AUTOMAGIC KERNELS LIST
179 # kopt=root=UUID=$rootuuid ro
180
181 ## ## End Default Options ##
182 title Debian
183 root (hd0,0)
184 kernel /vmlinuz root=UUID=$rootuuid ro
185 initrd /initrd.img
186
187 ### END DEBIAN AUTOMAGIC KERNELS LIST
188 EOF
189  fi &&
190  if [ "$fs" = "ext3" ]; then
191    rootopts="errors=remount-ro"
192  else
193    rootopts="defaults"
194  fi
195  cat > etc/fstab << EOF &&
196 UUID=$rootuuid    /               $fs    $rootopts 0       1
197 UUID=$swapuuid    none            swap    sw              0       0
198 EOF
199  if [ "$use_boot" != "n" ] ; then
200     echo "UUID=$bootuuid    /boot           $fs    defaults        0       2" >> etc/fstab
201  fi
202  cat > etc/network/interfaces << EOF
203 auto lo
204 iface lo inet loopback
205
206 auto eth0
207 iface eth0 inet manual
208   pre-up ip link set up dev \$IFACE
209   post-down ip link set down dev \$IFACE
210 EOF
211 }}}
212
213 Maybe fix/setup networking properly:
214
215 {{{
216 vi etc/network/interfaces
217 }}}
218
219 And unmount:
220
221 {{{
222   cd / &&
223  if [ "$use_boot" != "n" ] ; then
224    umount "$target"/boot &&
225    kpartx -v -d "$dev_boot"
226  fi &&
227  umount "$target" &&
228  rmdir "$target"
229 }}}
230
231 === virsh setup ===
232
233 Setup a new kvm domain by creating a new file in /etc/da-virt/`hostname/$guest.xml.
234
235 * Properly configure hostname
236 * Pick a new uuid ({{{uuidgen}}})
237 * Setup block devices properly
238 * 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>}}}. )
239 * virsh commands:
240 ** {{{virsh help}}}
241 ** {{{virsh define foo.xml}}}
242 ** {{{virsh start foo}}}
243 ** {{{virsh destroy foo}}}
244 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host  and  vnc localhost)
245
246 === post processing ===
247
248 Do not forget to set a sane root password before installing ssh in the new kvm domain.
249
250 === when stuff goes wrong ===
251
252 To get to the guest data from the host:
253
254 {{{
255   setup_env
256  kpartx -v -a "$dev_boot" &&
257  mkdir -p "$target" &&
258  mount "$dev_root" "$target" &&
259  mkdir -p "$target/boot" &&
260  mount "$dev_boot"1 "$target/boot"
261 }}}
262
263 and once you're done:
264 {{{
265  cd / &&
266  umount "$target"/boot &&
267  umount "$target" &&
268  kpartx -v -d "$dev_boot" &&
269  rmdir "$target"
270 }}}
271
272 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.