grub hook from ganeti list
[gnt-info] / hooks / grub
1 #!/bin/bash -x
2 #
3 # This is an example script that install and configure grub after installation.
4 # To use it put it in your CUSTOMIZE_DIR and make it executable.
5 #
6 # Do not include grub in EXTRA_PKGS of
7 # $sysconfdir/default/ganeti-instance-debootstrap because it will
8 # cause error of debootstrap.
9
10 set -e
11
12 # On wheezy we debootstrap with extlinux instead of grub
13 if [ $SUITE = "wheezy" ]; then
14     exit 0
15 fi
16
17 . common.sh
18
19 CLEANUP=( )
20
21 trap cleanup EXIT
22
23 if [ -z "$TARGET" -o ! -d "$TARGET" ]; then
24   echo "Missing target directory"
25   exit 1
26 fi
27
28 if [[ $BLOCKDEV == /dev/drbd* ]]; then
29     DISKTYPE=drbd
30 elif dmsetup info $BLOCKDEV > /dev/null 2>&1; then
31     DISKTYPE=lvm
32 else
33     echo "Unknown disk type"
34     exit 1
35 fi
36
37 mount -o bind /dev $TARGET/dev
38 CLEANUP+=("umount $TARGET/dev")
39
40 mount -t proc proc $TARGET/proc
41 CLEANUP+=("umount $TARGET/proc")
42
43 # For some reason, /dev/disk/by-uuid/<UUID> is not created. This results in
44 # grub refusing to specify the root fs uuid on the commandline, instead it
45 # will use $ROOTDEV which is not available in the VM...
46 ROOTUUID=$(blkid -o value -s UUID $FSYSDEV)
47 #ln -s $FSYSDEV /dev/disk/by-uuid/$ROOTUUID
48 CLEANUP+=("rm /dev/disk/by-uuid/$ROOTUUID")
49
50 chroot "$TARGET" apt-get update
51 chroot "$TARGET" /usr/bin/env DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install grub2
52
53 # install grub to make all the modules available ... serial.mod is not available otherwise
54 chroot "$TARGET" update-grub
55
56 case $DISKTYPE in
57     lvm)
58
59         # When using LVM, grub thinks we're installing to a partition. Using a loop dev fools it
60         LODEV=$(losetup --show -f $BLOCKDEV)
61         CLEANUP+=("losetup -d $LODEV")
62
63
64         # BLOCKDEV has the format /dev/<vgname>/<lvname>, but Grub will be looking
65         # for /dev/mapper/<vgname>-<mangled-lvname> ...
66         # This adds the mapper device to device.map, which is used to translate
67         # the root device node into grub-syntax (i.e., (hd0))
68
69         MAPPERDEV=/dev/mapper/$(dmsetup info -C --noheadings -o name $BLOCKDEV)
70         echo "(hd0) $MAPPERDEV" > $TARGET/boot/grub/device.map
71         chroot "$TARGET" grub-install $LODEV
72         ;;
73     drbd)
74         chroot "$TARGET" grub-install $BLOCKDEV
75         ;;
76 esac
77
78 cat >> $TARGET/etc/default/grub <<EOF
79 GRUB_CMDLINE_LINUX="text console=ttyS0,115200n8"
80 GRUB_TERMINAL=serial
81 GRUB_SERIAL_COMMAND="serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1"
82 EOF
83
84 # and do it again, serial.mod should now be available
85 chroot "$TARGET" update-grub
86
87 case $DISKTYPE in
88     lvm)
89         chroot "$TARGET" grub-install $LODEV
90         ;;
91     drbd)
92         chroot "$TARGET" grub-install $BLOCKDEV
93         ;;
94 esac
95
96 # For some reason, the loopback device is still busy/open if this sleep is removed ...
97 sleep 2
98
99 # execute cleanups
100 cleanup
101 trap - EXIT
102