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