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