fix a syntax error
[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 postinst_hook = /usr/sbin/update-grub
131 postrm_hook   = /usr/sbin/update-grub
132 EOF
133  chroot . apt-get install linux-image-2.6-amd64
134
135 }}}
136
137 And a fstab and a boot loader config
138
139 {{{
140  # doesn't work: chroot . update-grub
141  rootuuid=`vol_id "$dev_root" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
142  swapuuid=`vol_id "$dev_swap" "$target" | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
143  bootuuid=`vol_id "$dev_boot"1  | awk -F= '$1=="ID_FS_UUID" {print $2}'` &&
144  cat > boot/grub/menu.lst << EOF &&
145 default 0
146 timeout 5
147 color cyan/blue white/blue
148
149 ### BEGIN AUTOMAGIC KERNELS LIST
150 # kopt=root=UUID=$rootuuid ro
151
152 ## ## End Default Options ##
153 title Debian
154 root (hd0,0)
155 kernel /vmlinuz root=UUID=$rootuuid ro
156 initrd /initrd.img
157
158 ### END DEBIAN AUTOMAGIC KERNELS LIST
159 EOF
160  if [ "$fs" = "ext3" ]; then
161    rootopts="errors=remount-ro"
162  else
163    rootopts="defaults"
164  fi
165  cat > etc/fstab << EOF &&
166 UUID=$rootuuid    /               $fs    $rootopts 0       1
167 UUID=$bootuuid    /boot           $fs    defaults        0       2
168 UUID=$swapuuid    none            swap    sw              0       0
169 EOF
170  cat > etc/network/interfaces << EOF
171 auto lo
172 iface lo inet loopback
173
174 auto eth0
175 iface eth0 inet manual
176   pre-up ip link set up dev \$IFACE
177   post-down ip link set down dev \$IFACE
178 EOF
179 }}}
180
181 Maybe fix/setup networking properly:
182
183 {{{
184 vi etc/network/interfaces
185 }}}
186
187 And unmount:
188
189 {{{
190   cd / &&
191  umount "$target"/boot &&
192  umount "$target" &&
193  kpartx -v -d "$dev_boot" &&
194  rmdir "$target"
195 }}}
196
197 === virsh setup ===
198
199 Setup a new kvm domain by creating a new file in /etc/da-virt/`hostname/$guest.xml.
200
201 * Properly configure hostname
202 * Pick a new uuid ({{{uuidgen}}})
203 * Setup block devices properly
204 * 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>}}}. )
205 * virsh commands:
206 ** {{{virsh help}}}
207 ** {{{virsh define foo.xml}}}
208 ** {{{virsh start foo}}}
209 ** {{{virsh destroy foo}}}
210 ** {{{virsh vncdisplay foo}}} (and ssh -L 5900:localhost:<5900+x> $host  and  vnc localhost)
211
212 === post processing ===
213
214 Do not forget to set a sane root password before installing ssh in the new kvm domain.
215
216 === when stuff goes wrong ===
217
218 To get to the guest data from the host:
219
220 {{{
221   setup_env
222  kpartx -v -a "$dev_boot" &&
223  mkdir -p "$target" &&
224  mount "$dev_root" "$target" &&
225  mkdir -p "$target/boot" &&
226  mount "$dev_boot"1 "$target/boot"
227 }}}
228
229 and once you're done:
230 {{{
231  cd / &&
232  umount "$target"/boot &&
233  umount "$target" &&
234  kpartx -v -d "$dev_boot" &&
235  rmdir "$target"
236 }}}
237
238 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.