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