Statistics
| Branch: | Revision:

root / example / hooks / linux / grub @ 4fc88242

History | View | Annotate | Download (5.9 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
    swap_part="${SWAP_DEV/*-/}"
54

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

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

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

    
66
    if [ -n "$BOOT_DEV" ]; then
67
        grub1_root=$(($boot_part-1))
68
        grub2_root=$boot_part
69
    else
70
        grub1_root=$(($root_part-1))
71
        grub2_root=$root_part
72
    fi
73

    
74
    # setup minimal proc environment for grub2
75
    $MKDIR_P ${TARGET}/proc/
76
    cat > ${TARGET}/proc/mounts <<EOF
77
/dev/${disk}${boot_part} /boot ${FILESYSTEM} rw,relatime,barrier=1,data=ordered 0 0
78
/dev/${disk}${root_part} / ${FILESYSTEM} rw,relatime,barrier=1,data=ordered 0 0
79
EOF
80
    CLEANUP+=("rm -r ${TARGET}/proc/mounts")
81

    
82
    cat > ${TARGET}/proc/devices <<EOF
83
Block devices:
84
251 virtblk
85
252 device-mapper
86
EOF
87
    CLEANUP+=("rm -r ${TARGET}/proc/devices")
88
    cat > ${TARGET}/proc/misc << EOF
89
 60 device-mapper
90
EOF
91
    CLEANUP+=("rm -r ${TARGET}/proc/misc")
92
}
93

    
94
add_devicemap() {
95
    cat > "${TARGET}/boot/grub/device.map" <<EOF
96
(hd0) /dev/${disk}
97
EOF
98
}
99

    
100
if [ "${IMAGE_TYPE}" != "qemu" ] && \
101
   [ -z "${INSTANCE_HV_kernel_path}" -o "${IMPORT_SCRIPT}" = "1" ] ; then
102
    setup_disk_devs
103
    add_devicemap
104

    
105

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

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

    
172
# execute cleanups
173
cleanup
174
trap - EXIT
175

    
176
exit 0