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