Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (5.9 kB)

1 79224631 Lance Albertson
#!/bin/bash
2 f1afb4b1 Lance Albertson
3 f1afb4b1 Lance Albertson
# Copyright (C) 2010 Oregon State University
4 f1afb4b1 Lance Albertson
#
5 f1afb4b1 Lance Albertson
# This program is free software; you can redistribute it and/or modify
6 f1afb4b1 Lance Albertson
# it under the terms of the GNU General Public License as published by
7 f1afb4b1 Lance Albertson
# the Free Software Foundation; either version 2 of the License, or
8 f1afb4b1 Lance Albertson
# (at your option) any later version.
9 f1afb4b1 Lance Albertson
#
10 f1afb4b1 Lance Albertson
# This program is distributed in the hope that it will be useful, but
11 f1afb4b1 Lance Albertson
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 f1afb4b1 Lance Albertson
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 f1afb4b1 Lance Albertson
# General Public License for more details.
14 f1afb4b1 Lance Albertson
#
15 f1afb4b1 Lance Albertson
# You should have received a copy of the GNU General Public License
16 f1afb4b1 Lance Albertson
# along with this program; if not, write to the Free Software
17 f1afb4b1 Lance Albertson
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 f1afb4b1 Lance Albertson
# 02110-1301, USA.
19 f1afb4b1 Lance Albertson
#
20 f7959feb Lance Albertson
# This is an example script that configures, grub after installation. This
21 f7959feb Lance Albertson
# script assumes that grub has been installed onto the image already and a
22 c3e07762 Lance Albertson
# working grub.conf exists. This is only enabled if using the tarball image
23 c3e07762 Lance Albertson
# type. 
24 79224631 Lance Albertson
25 79224631 Lance Albertson
set -e
26 79224631 Lance Albertson
27 79224631 Lance Albertson
. common.sh
28 79224631 Lance Albertson
29 e7a2d1ad Lance Albertson
debug set -x
30 e84e87cd Lance Albertson
31 79224631 Lance Albertson
CLEANUP=( )
32 79224631 Lance Albertson
33 79224631 Lance Albertson
trap cleanup EXIT
34 79224631 Lance Albertson
35 ad81a23e Lance Albertson
if [ -z "${TARGET}" -o ! -d "${TARGET}" ] ; then
36 e0f1ce56 Lance Albertson
    log_error "Missing target directory"
37 f7959feb Lance Albertson
    exit 1
38 79224631 Lance Albertson
fi
39 79224631 Lance Albertson
40 f7959feb Lance Albertson
# Set disk based on type of hypervisor
41 f7959feb Lance Albertson
disk=""
42 f7959feb Lance Albertson
if [ "${HYPERVISOR}" = "kvm" ] ; then
43 f7959feb Lance Albertson
  disk="vda"
44 f7959feb Lance Albertson
else
45 f7959feb Lance Albertson
  disk="xda"
46 f7959feb Lance Albertson
fi
47 79224631 Lance Albertson
48 13eb5d4c Lance Albertson
boot_dir="${TARGET}/boot/grub"
49 13eb5d4c Lance Albertson
50 07d32070 Lance Albertson
setup_disk_devs() {
51 07d32070 Lance Albertson
    root_part="${ROOT_DEV/*-/}"
52 07d32070 Lance Albertson
    boot_part="${BOOT_DEV/*-/}"
53 4fc88242 Nikos Skalkotos
    swap_part="${SWAP_DEV/*-/}"
54 07d32070 Lance Albertson
55 5edb4a94 Lance Albertson
    mknod ${TARGET}/dev/${disk} b $(stat -L -c "0x%t 0x%T" $BLOCKDEV)
56 5edb4a94 Lance Albertson
    CLEANUP+=("rm -f ${TARGET}/dev/$disk")
57 5edb4a94 Lance Albertson
58 07d32070 Lance Albertson
    mknod ${TARGET}/dev/${disk}${root_part} b $(stat -L -c "0x%t 0x%T" $ROOT_DEV)
59 07d32070 Lance Albertson
    CLEANUP+=("rm -f ${TARGET}/dev/${disk}${root_part}")
60 79224631 Lance Albertson
61 07d32070 Lance Albertson
    if [ -n "$BOOT_DEV" ] ; then
62 07d32070 Lance Albertson
        mknod ${TARGET}/dev/${disk}${boot_part} b $(stat -L -c "0x%t 0x%T" $BOOT_DEV)
63 07d32070 Lance Albertson
        CLEANUP+=("rm -f ${TARGET}/dev/${disk}${boot_part}")
64 07d32070 Lance Albertson
    fi
65 c838e846 Lance Albertson
66 4fc88242 Nikos Skalkotos
    if [ -n "$BOOT_DEV" ]; then
67 4fc88242 Nikos Skalkotos
        grub1_root=$(($boot_part-1))
68 4fc88242 Nikos Skalkotos
        grub2_root=$boot_part
69 4fc88242 Nikos Skalkotos
    else
70 4fc88242 Nikos Skalkotos
        grub1_root=$(($root_part-1))
71 4fc88242 Nikos Skalkotos
        grub2_root=$root_part
72 4fc88242 Nikos Skalkotos
    fi
73 4fc88242 Nikos Skalkotos
74 c838e846 Lance Albertson
    # setup minimal proc environment for grub2
75 c838e846 Lance Albertson
    $MKDIR_P ${TARGET}/proc/
76 c838e846 Lance Albertson
    cat > ${TARGET}/proc/mounts <<EOF
77 c838e846 Lance Albertson
/dev/${disk}${boot_part} /boot ${FILESYSTEM} rw,relatime,barrier=1,data=ordered 0 0
78 c838e846 Lance Albertson
/dev/${disk}${root_part} / ${FILESYSTEM} rw,relatime,barrier=1,data=ordered 0 0
79 c838e846 Lance Albertson
EOF
80 c838e846 Lance Albertson
    CLEANUP+=("rm -r ${TARGET}/proc/mounts")
81 c838e846 Lance Albertson
82 c838e846 Lance Albertson
    cat > ${TARGET}/proc/devices <<EOF
83 c838e846 Lance Albertson
Block devices:
84 c838e846 Lance Albertson
251 virtblk
85 c838e846 Lance Albertson
252 device-mapper
86 c838e846 Lance Albertson
EOF
87 c838e846 Lance Albertson
    CLEANUP+=("rm -r ${TARGET}/proc/devices")
88 c838e846 Lance Albertson
    cat > ${TARGET}/proc/misc << EOF
89 c838e846 Lance Albertson
 60 device-mapper
90 c838e846 Lance Albertson
EOF
91 c838e846 Lance Albertson
    CLEANUP+=("rm -r ${TARGET}/proc/misc")
92 07d32070 Lance Albertson
}
93 79224631 Lance Albertson
94 07d32070 Lance Albertson
add_devicemap() {
95 07d32070 Lance Albertson
    cat > "${TARGET}/boot/grub/device.map" <<EOF
96 f7959feb Lance Albertson
(hd0) /dev/${disk}
97 79224631 Lance Albertson
EOF
98 07d32070 Lance Albertson
}
99 07d32070 Lance Albertson
100 c2fca018 Lance Albertson
if [ "${IMAGE_TYPE}" != "qemu" ] && \
101 c2fca018 Lance Albertson
   [ -z "${INSTANCE_HV_kernel_path}" -o "${IMPORT_SCRIPT}" = "1" ] ; then
102 07d32070 Lance Albertson
    setup_disk_devs
103 c40fd6ae Lance Albertson
    add_devicemap
104 79224631 Lance Albertson
105 4fc88242 Nikos Skalkotos
106 07d32070 Lance Albertson
    if [ -e "${boot_dir}/menu.lst" ] ; then
107 5edb4a94 Lance Albertson
        # install grub to the block device
108 5edb4a94 Lance Albertson
        chroot ${TARGET} grub --batch --no-floppy \
109 78422bb3 Lance Albertson
            --device-map=/boot/grub/device.map > /dev/null <<EOF
110 4fc88242 Nikos Skalkotos
root (hd0,${grub1_root})
111 f7959feb Lance Albertson
setup (hd0)
112 f7959feb Lance Albertson
quit
113 f7959feb Lance Albertson
EOF
114 5edb4a94 Lance Albertson
        # check to see if grub is using UUID's and replace if so
115 5edb4a94 Lance Albertson
        if [ -n "$(grep 'root=UUID' ${boot_dir}/menu.lst)" ] ; then
116 5edb4a94 Lance Albertson
            root_uuid="$($VOL_ID $ROOT_DEV)"
117 5edb4a94 Lance Albertson
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
118 5edb4a94 Lance Albertson
                ${boot_dir}/menu.lst
119 5edb4a94 Lance Albertson
        fi
120 c40fd6ae Lance Albertson
        # additional kernel arguments for the instance
121 c40fd6ae Lance Albertson
        if [ -n "${KERNEL_ARGS}" ] ; then
122 c40fd6ae Lance Albertson
            sed --follow-symlinks -ie "s/\(.*kernel.*\)/\1 ${KERNEL_ARGS}/g" \
123 c40fd6ae Lance Albertson
                ${boot_dir}/menu.lst
124 c40fd6ae Lance Albertson
        fi
125 13eb5d4c Lance Albertson
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
126 c838e846 Lance Albertson
        echo "grub2 support is partially supported"
127 c838e846 Lance Albertson
        echo "please run update-grub after the instance is online"
128 5edb4a94 Lance Albertson
        # check to see if grub is using UUID's and replace if so
129 c838e846 Lance Albertson
        if [ -n "$(grep 'root=UUID' ${boot_dir}/grub.cfg)" ] ; then
130 c838e846 Lance Albertson
            root_uuid="$($VOL_ID $ROOT_DEV)"
131 c838e846 Lance Albertson
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
132 c838e846 Lance Albertson
                ${boot_dir}/grub.cfg
133 c838e846 Lance Albertson
        fi
134 c40fd6ae Lance Albertson
        # additional kernel arguments for the instance
135 c40fd6ae Lance Albertson
        if [ -n "${KERNEL_ARGS}" ] ; then
136 c40fd6ae Lance Albertson
            sed -ie "s/.*GRUB_CMDLINE_LINUX=\"\(.*\)\"/GRUB_CMDLINE_LINUX=\"\1 ${KERNEL_ARGS}\"/" \
137 c40fd6ae Lance Albertson
                ${TARGET}/etc/default/grub
138 c40fd6ae Lance Albertson
        fi
139 c40fd6ae Lance Albertson
        # show the menu countdown in case we want to boot to a different kernel
140 c40fd6ae Lance Albertson
        sed -ie 's/.*GRUB_HIDDEN_TIMEOUT=.*/GRUB_HIDDEN_TIMEOUT=5/' ${TARGET}/etc/default/grub
141 c40fd6ae Lance Albertson
        sed -ie 's/.*GRUB_HIDDEN_TIMEOUT_QUIET=.*/GRUB_HIDDEN_TIMEOUT_QUIET=false/' ${TARGET}/etc/default/grub
142 c838e846 Lance Albertson
        # install grub2 MBR
143 c838e846 Lance Albertson
        chroot ${TARGET} grub-setup --force --device-map=/boot/grub/device.map \
144 4fc88242 Nikos Skalkotos
            --directory=/boot/grub --root-device="(hd0,${grub2_root})" "(hd0)" > /dev/null
145 5edb4a94 Lance Albertson
    fi
146 5edb4a94 Lance Albertson
fi
147 5edb4a94 Lance Albertson
148 91767b54 Lance Albertson
# setup serial console
149 91767b54 Lance Albertson
if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
150 07d32070 Lance Albertson
    if [ ! -e ${TARGET}/dev/${disk} ] ; then
151 07d32070 Lance Albertson
        setup_disk_devs
152 07d32070 Lance Albertson
    fi
153 91767b54 Lance Albertson
    if [ -e "${boot_dir}/menu.lst" ] ; then
154 07d32070 Lance Albertson
        # grub 0.x
155 c3d1b43e Lance Albertson
        sed --follow-symlinks -ie 's/^default.*/default 0\n\nserial --unit=0\nterminal --timeout=3 console serial/' \
156 91767b54 Lance Albertson
            ${boot_dir}/menu.lst
157 c3d1b43e Lance Albertson
        sed --follow-symlinks -ie 's/\(.*kernel.*\)/\1 console=ttyS0,115200n8/g' \
158 9404f8cd Lance Albertson
            ${boot_dir}/menu.lst
159 13eb5d4c Lance Albertson
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
160 07d32070 Lance Albertson
        # grub 2.x
161 07d32070 Lance Albertson
        add_devicemap
162 13eb5d4c Lance Albertson
        sed -ie 's/.*GRUB_TERMINAL.*/GRUB_TERMINAL=serial/' ${TARGET}/etc/default/grub
163 c40fd6ae Lance Albertson
        sed -ie 's/.*GRUB_CMDLINE_LINUX=\"\(.*\)\"/GRUB_CMDLINE_LINUX=\"\1 console=ttyS0,115200n8\"/' \
164 13eb5d4c Lance Albertson
            ${TARGET}/etc/default/grub
165 91767b54 Lance Albertson
        echo "GRUB_SERIAL_COMMAND=\"serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1\"" \
166 13eb5d4c Lance Albertson
            >> ${TARGET}/etc/default/grub
167 91767b54 Lance Albertson
    else
168 91767b54 Lance Albertson
        echo "No grub bootloader found, skipping..."
169 91767b54 Lance Albertson
    fi
170 91767b54 Lance Albertson
fi
171 91767b54 Lance Albertson
172 79224631 Lance Albertson
# execute cleanups
173 79224631 Lance Albertson
cleanup
174 79224631 Lance Albertson
trap - EXIT
175 79224631 Lance Albertson
176 79224631 Lance Albertson
exit 0