Statistics
| Branch: | Revision:

root / example / hooks / grub @ c40fd6ae

History | View | Annotate | Download (5.6 kB)

1
#!/bin/bash
2

    
3
# Copyright (C) 2010 Oregon State University
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
# General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
# 02110-1301, USA.
19
#
20
# This is an example script that configures, grub after installation. This
21
# script assumes that grub has been installed onto the image already and a
22
# working grub.conf exists. This is only enabled if using the tarball image
23
# type. 
24

    
25
set -e
26

    
27
. common.sh
28

    
29
debug set -x
30

    
31
CLEANUP=( )
32

    
33
trap cleanup EXIT
34

    
35
if [ -z "${TARGET}" -o ! -d "${TARGET}" ] ; then
36
    log_error "Missing target directory"
37
    exit 1
38
fi
39

    
40
# Set disk based on type of hypervisor
41
disk=""
42
if [ "${HYPERVISOR}" = "kvm" ] ; then
43
  disk="vda"
44
else
45
  disk="xda"
46
fi
47

    
48
boot_dir="${TARGET}/boot/grub"
49

    
50
setup_disk_devs() {
51
    root_part="${ROOT_DEV/*-/}"
52
    boot_part="${BOOT_DEV/*-/}"
53

    
54
    mknod ${TARGET}/dev/${disk} b $(stat -L -c "0x%t 0x%T" $BLOCKDEV)
55
    CLEANUP+=("rm -f ${TARGET}/dev/$disk")
56

    
57
    mknod ${TARGET}/dev/${disk}${root_part} b $(stat -L -c "0x%t 0x%T" $ROOT_DEV)
58
    CLEANUP+=("rm -f ${TARGET}/dev/${disk}${root_part}")
59

    
60
    if [ -n "$BOOT_DEV" ] ; then
61
        mknod ${TARGET}/dev/${disk}${boot_part} b $(stat -L -c "0x%t 0x%T" $BOOT_DEV)
62
        CLEANUP+=("rm -f ${TARGET}/dev/${disk}${boot_part}")
63
    fi
64

    
65
    # setup minimal proc environment for grub2
66
    $MKDIR_P ${TARGET}/proc/
67
    cat > ${TARGET}/proc/mounts <<EOF
68
/dev/${disk}${boot_part} /boot ${FILESYSTEM} rw,relatime,barrier=1,data=ordered 0 0
69
/dev/${disk}${root_part} / ${FILESYSTEM} rw,relatime,barrier=1,data=ordered 0 0
70
EOF
71
    CLEANUP+=("rm -r ${TARGET}/proc/mounts")
72

    
73
    cat > ${TARGET}/proc/devices <<EOF
74
Block devices:
75
251 virtblk
76
252 device-mapper
77
EOF
78
    CLEANUP+=("rm -r ${TARGET}/proc/devices")
79
    cat > ${TARGET}/proc/misc << EOF
80
 60 device-mapper
81
EOF
82
    CLEANUP+=("rm -r ${TARGET}/proc/misc")
83
}
84

    
85
add_devicemap() {
86
    cat > "${TARGET}/boot/grub/device.map" <<EOF
87
(hd0) /dev/${disk}
88
EOF
89
}
90

    
91
if [ "${IMAGE_TYPE}" != "qemu" ] && \
92
   [ -z "${INSTANCE_HV_kernel_path}" -o "${IMPORT_SCRIPT}" = "1" ] ; then
93
    setup_disk_devs
94
    add_devicemap
95

    
96
    if [ -e "${boot_dir}/menu.lst" ] ; then
97
        # install grub to the block device
98
        chroot ${TARGET} grub --batch --no-floppy \
99
            --device-map=/boot/grub/device.map <<EOF
100
root (hd0,0)
101
setup (hd0)
102
quit
103
EOF
104
        # check to see if grub is using UUID's and replace if so
105
        if [ -n "$(grep 'root=UUID' ${boot_dir}/menu.lst)" ] ; then
106
            root_uuid="$($VOL_ID $ROOT_DEV)"
107
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
108
                ${boot_dir}/menu.lst
109
        fi
110
        # additional kernel arguments for the instance
111
        if [ -n "${KERNEL_ARGS}" ] ; then
112
            sed --follow-symlinks -ie "s/\(.*kernel.*\)/\1 ${KERNEL_ARGS}/g" \
113
                ${boot_dir}/menu.lst
114
        fi
115
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
116
        echo "grub2 support is partially supported"
117
        echo "please run update-grub after the instance is online"
118
        # check to see if grub is using UUID's and replace if so
119
        if [ -n "$(grep 'root=UUID' ${boot_dir}/grub.cfg)" ] ; then
120
            root_uuid="$($VOL_ID $ROOT_DEV)"
121
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
122
                ${boot_dir}/grub.cfg
123
        fi
124
        # additional kernel arguments for the instance
125
        if [ -n "${KERNEL_ARGS}" ] ; then
126
            sed -ie "s/.*GRUB_CMDLINE_LINUX=\"\(.*\)\"/GRUB_CMDLINE_LINUX=\"\1 ${KERNEL_ARGS}\"/" \
127
                ${TARGET}/etc/default/grub
128
        fi
129
        # show the menu countdown in case we want to boot to a different kernel
130
        sed -ie 's/.*GRUB_HIDDEN_TIMEOUT=.*/GRUB_HIDDEN_TIMEOUT=5/' ${TARGET}/etc/default/grub
131
        sed -ie 's/.*GRUB_HIDDEN_TIMEOUT_QUIET=.*/GRUB_HIDDEN_TIMEOUT_QUIET=false/' ${TARGET}/etc/default/grub
132
        # install grub2 MBR
133
        chroot ${TARGET} grub-setup --force --device-map=/boot/grub/device.map \
134
            --directory=/boot/grub --root-device="(hd0,1)" "(hd0)"
135
    fi
136
fi
137

    
138
# setup serial console
139
if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
140
    if [ ! -e ${TARGET}/dev/${disk} ] ; then
141
        setup_disk_devs
142
    fi
143
    if [ -e "${boot_dir}/menu.lst" ] ; then
144
        # grub 0.x
145
        sed --follow-symlinks -ie 's/^default.*/default 0\n\nserial --unit=0\nterminal --timeout=3 console serial/' \
146
            ${boot_dir}/menu.lst
147
        sed --follow-symlinks -ie 's/\(.*kernel.*\)/\1 console=ttyS0,115200n8/g' \
148
            ${boot_dir}/menu.lst
149
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
150
        # grub 2.x
151
        add_devicemap
152
        sed -ie 's/.*GRUB_TERMINAL.*/GRUB_TERMINAL=serial/' ${TARGET}/etc/default/grub
153
        sed -ie 's/.*GRUB_CMDLINE_LINUX=\"\(.*\)\"/GRUB_CMDLINE_LINUX=\"\1 console=ttyS0,115200n8\"/' \
154
            ${TARGET}/etc/default/grub
155
        echo "GRUB_SERIAL_COMMAND=\"serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1\"" \
156
            >> ${TARGET}/etc/default/grub
157
    else
158
        echo "No grub bootloader found, skipping..."
159
    fi
160
fi
161

    
162
# execute cleanups
163
cleanup
164
trap - EXIT
165

    
166
exit 0