Statistics
| Branch: | Revision:

root / example / hooks / grub @ c40fd6ae

History | View | Annotate | Download (5.6 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 07d32070 Lance Albertson
54 5edb4a94 Lance Albertson
    mknod ${TARGET}/dev/${disk} b $(stat -L -c "0x%t 0x%T" $BLOCKDEV)
55 5edb4a94 Lance Albertson
    CLEANUP+=("rm -f ${TARGET}/dev/$disk")
56 5edb4a94 Lance Albertson
57 07d32070 Lance Albertson
    mknod ${TARGET}/dev/${disk}${root_part} b $(stat -L -c "0x%t 0x%T" $ROOT_DEV)
58 07d32070 Lance Albertson
    CLEANUP+=("rm -f ${TARGET}/dev/${disk}${root_part}")
59 79224631 Lance Albertson
60 07d32070 Lance Albertson
    if [ -n "$BOOT_DEV" ] ; then
61 07d32070 Lance Albertson
        mknod ${TARGET}/dev/${disk}${boot_part} b $(stat -L -c "0x%t 0x%T" $BOOT_DEV)
62 07d32070 Lance Albertson
        CLEANUP+=("rm -f ${TARGET}/dev/${disk}${boot_part}")
63 07d32070 Lance Albertson
    fi
64 c838e846 Lance Albertson
65 c838e846 Lance Albertson
    # setup minimal proc environment for grub2
66 c838e846 Lance Albertson
    $MKDIR_P ${TARGET}/proc/
67 c838e846 Lance Albertson
    cat > ${TARGET}/proc/mounts <<EOF
68 c838e846 Lance Albertson
/dev/${disk}${boot_part} /boot ${FILESYSTEM} rw,relatime,barrier=1,data=ordered 0 0
69 c838e846 Lance Albertson
/dev/${disk}${root_part} / ${FILESYSTEM} rw,relatime,barrier=1,data=ordered 0 0
70 c838e846 Lance Albertson
EOF
71 c838e846 Lance Albertson
    CLEANUP+=("rm -r ${TARGET}/proc/mounts")
72 c838e846 Lance Albertson
73 c838e846 Lance Albertson
    cat > ${TARGET}/proc/devices <<EOF
74 c838e846 Lance Albertson
Block devices:
75 c838e846 Lance Albertson
251 virtblk
76 c838e846 Lance Albertson
252 device-mapper
77 c838e846 Lance Albertson
EOF
78 c838e846 Lance Albertson
    CLEANUP+=("rm -r ${TARGET}/proc/devices")
79 c838e846 Lance Albertson
    cat > ${TARGET}/proc/misc << EOF
80 c838e846 Lance Albertson
 60 device-mapper
81 c838e846 Lance Albertson
EOF
82 c838e846 Lance Albertson
    CLEANUP+=("rm -r ${TARGET}/proc/misc")
83 07d32070 Lance Albertson
}
84 79224631 Lance Albertson
85 07d32070 Lance Albertson
add_devicemap() {
86 07d32070 Lance Albertson
    cat > "${TARGET}/boot/grub/device.map" <<EOF
87 f7959feb Lance Albertson
(hd0) /dev/${disk}
88 79224631 Lance Albertson
EOF
89 07d32070 Lance Albertson
}
90 07d32070 Lance Albertson
91 c2fca018 Lance Albertson
if [ "${IMAGE_TYPE}" != "qemu" ] && \
92 c2fca018 Lance Albertson
   [ -z "${INSTANCE_HV_kernel_path}" -o "${IMPORT_SCRIPT}" = "1" ] ; then
93 07d32070 Lance Albertson
    setup_disk_devs
94 c40fd6ae Lance Albertson
    add_devicemap
95 79224631 Lance Albertson
96 07d32070 Lance Albertson
    if [ -e "${boot_dir}/menu.lst" ] ; then
97 5edb4a94 Lance Albertson
        # install grub to the block device
98 5edb4a94 Lance Albertson
        chroot ${TARGET} grub --batch --no-floppy \
99 5edb4a94 Lance Albertson
            --device-map=/boot/grub/device.map <<EOF
100 f7959feb Lance Albertson
root (hd0,0)
101 f7959feb Lance Albertson
setup (hd0)
102 f7959feb Lance Albertson
quit
103 f7959feb Lance Albertson
EOF
104 5edb4a94 Lance Albertson
        # check to see if grub is using UUID's and replace if so
105 5edb4a94 Lance Albertson
        if [ -n "$(grep 'root=UUID' ${boot_dir}/menu.lst)" ] ; then
106 5edb4a94 Lance Albertson
            root_uuid="$($VOL_ID $ROOT_DEV)"
107 5edb4a94 Lance Albertson
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
108 5edb4a94 Lance Albertson
                ${boot_dir}/menu.lst
109 5edb4a94 Lance Albertson
        fi
110 c40fd6ae Lance Albertson
        # additional kernel arguments for the instance
111 c40fd6ae Lance Albertson
        if [ -n "${KERNEL_ARGS}" ] ; then
112 c40fd6ae Lance Albertson
            sed --follow-symlinks -ie "s/\(.*kernel.*\)/\1 ${KERNEL_ARGS}/g" \
113 c40fd6ae Lance Albertson
                ${boot_dir}/menu.lst
114 c40fd6ae Lance Albertson
        fi
115 13eb5d4c Lance Albertson
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
116 c838e846 Lance Albertson
        echo "grub2 support is partially supported"
117 c838e846 Lance Albertson
        echo "please run update-grub after the instance is online"
118 5edb4a94 Lance Albertson
        # check to see if grub is using UUID's and replace if so
119 c838e846 Lance Albertson
        if [ -n "$(grep 'root=UUID' ${boot_dir}/grub.cfg)" ] ; then
120 c838e846 Lance Albertson
            root_uuid="$($VOL_ID $ROOT_DEV)"
121 c838e846 Lance Albertson
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
122 c838e846 Lance Albertson
                ${boot_dir}/grub.cfg
123 c838e846 Lance Albertson
        fi
124 c40fd6ae Lance Albertson
        # additional kernel arguments for the instance
125 c40fd6ae Lance Albertson
        if [ -n "${KERNEL_ARGS}" ] ; then
126 c40fd6ae Lance Albertson
            sed -ie "s/.*GRUB_CMDLINE_LINUX=\"\(.*\)\"/GRUB_CMDLINE_LINUX=\"\1 ${KERNEL_ARGS}\"/" \
127 c40fd6ae Lance Albertson
                ${TARGET}/etc/default/grub
128 c40fd6ae Lance Albertson
        fi
129 c40fd6ae Lance Albertson
        # show the menu countdown in case we want to boot to a different kernel
130 c40fd6ae Lance Albertson
        sed -ie 's/.*GRUB_HIDDEN_TIMEOUT=.*/GRUB_HIDDEN_TIMEOUT=5/' ${TARGET}/etc/default/grub
131 c40fd6ae Lance Albertson
        sed -ie 's/.*GRUB_HIDDEN_TIMEOUT_QUIET=.*/GRUB_HIDDEN_TIMEOUT_QUIET=false/' ${TARGET}/etc/default/grub
132 c838e846 Lance Albertson
        # install grub2 MBR
133 c838e846 Lance Albertson
        chroot ${TARGET} grub-setup --force --device-map=/boot/grub/device.map \
134 c838e846 Lance Albertson
            --directory=/boot/grub --root-device="(hd0,1)" "(hd0)"
135 5edb4a94 Lance Albertson
    fi
136 5edb4a94 Lance Albertson
fi
137 5edb4a94 Lance Albertson
138 91767b54 Lance Albertson
# setup serial console
139 91767b54 Lance Albertson
if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
140 07d32070 Lance Albertson
    if [ ! -e ${TARGET}/dev/${disk} ] ; then
141 07d32070 Lance Albertson
        setup_disk_devs
142 07d32070 Lance Albertson
    fi
143 91767b54 Lance Albertson
    if [ -e "${boot_dir}/menu.lst" ] ; then
144 07d32070 Lance Albertson
        # grub 0.x
145 c3d1b43e Lance Albertson
        sed --follow-symlinks -ie 's/^default.*/default 0\n\nserial --unit=0\nterminal --timeout=3 console serial/' \
146 91767b54 Lance Albertson
            ${boot_dir}/menu.lst
147 c3d1b43e Lance Albertson
        sed --follow-symlinks -ie 's/\(.*kernel.*\)/\1 console=ttyS0,115200n8/g' \
148 9404f8cd Lance Albertson
            ${boot_dir}/menu.lst
149 13eb5d4c Lance Albertson
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
150 07d32070 Lance Albertson
        # grub 2.x
151 07d32070 Lance Albertson
        add_devicemap
152 13eb5d4c Lance Albertson
        sed -ie 's/.*GRUB_TERMINAL.*/GRUB_TERMINAL=serial/' ${TARGET}/etc/default/grub
153 c40fd6ae Lance Albertson
        sed -ie 's/.*GRUB_CMDLINE_LINUX=\"\(.*\)\"/GRUB_CMDLINE_LINUX=\"\1 console=ttyS0,115200n8\"/' \
154 13eb5d4c Lance Albertson
            ${TARGET}/etc/default/grub
155 91767b54 Lance Albertson
        echo "GRUB_SERIAL_COMMAND=\"serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1\"" \
156 13eb5d4c Lance Albertson
            >> ${TARGET}/etc/default/grub
157 91767b54 Lance Albertson
    else
158 91767b54 Lance Albertson
        echo "No grub bootloader found, skipping..."
159 91767b54 Lance Albertson
    fi
160 91767b54 Lance Albertson
fi
161 91767b54 Lance Albertson
162 79224631 Lance Albertson
# execute cleanups
163 79224631 Lance Albertson
cleanup
164 79224631 Lance Albertson
trap - EXIT
165 79224631 Lance Albertson
166 79224631 Lance Albertson
exit 0