Statistics
| Branch: | Revision:

root / example / instance-image.d / grub @ 07d32070

History | View | Annotate | Download (4.1 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 07d32070 Lance Albertson
}
65 79224631 Lance Albertson
66 07d32070 Lance Albertson
add_devicemap() {
67 07d32070 Lance Albertson
    cat > "${TARGET}/boot/grub/device.map" <<EOF
68 f7959feb Lance Albertson
(hd0) /dev/${disk}
69 79224631 Lance Albertson
EOF
70 07d32070 Lance Albertson
}
71 07d32070 Lance Albertson
72 07d32070 Lance Albertson
if [ -n "${INSTANCE_HV_kernel_path}" -o "${IMPORT_SCRIPT}" = "1" ] ; then
73 07d32070 Lance Albertson
    setup_disk_devs
74 79224631 Lance Albertson
75 07d32070 Lance Albertson
    if [ -e "${boot_dir}/menu.lst" ] ; then
76 07d32070 Lance Albertson
        add_devicemap
77 5edb4a94 Lance Albertson
        # install grub to the block device
78 5edb4a94 Lance Albertson
        chroot ${TARGET} grub --batch --no-floppy \
79 5edb4a94 Lance Albertson
            --device-map=/boot/grub/device.map <<EOF
80 f7959feb Lance Albertson
root (hd0,0)
81 f7959feb Lance Albertson
setup (hd0)
82 f7959feb Lance Albertson
quit
83 f7959feb Lance Albertson
EOF
84 5edb4a94 Lance Albertson
        # check to see if grub is using UUID's and replace if so
85 5edb4a94 Lance Albertson
        if [ -n "$(grep 'root=UUID' ${boot_dir}/menu.lst)" ] ; then
86 5edb4a94 Lance Albertson
            root_uuid="$($VOL_ID $ROOT_DEV)"
87 5edb4a94 Lance Albertson
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
88 5edb4a94 Lance Albertson
                ${boot_dir}/menu.lst
89 5edb4a94 Lance Albertson
        fi
90 13eb5d4c Lance Albertson
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
91 5edb4a94 Lance Albertson
        chroot ${TARGET} grub2-install /dev/${disk}
92 5edb4a94 Lance Albertson
        # check to see if grub is using UUID's and replace if so
93 5edb4a94 Lance Albertson
        if [ -n "$(grep 'root=UUID' ${boot_dir}/grub.cfg)" ] ; then
94 5edb4a94 Lance Albertson
            root_uuid="$($VOL_ID $ROOT_DEV)"
95 5edb4a94 Lance Albertson
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
96 5edb4a94 Lance Albertson
                ${boot_dir}/grub.cfg
97 5edb4a94 Lance Albertson
        fi
98 5edb4a94 Lance Albertson
    fi
99 5edb4a94 Lance Albertson
fi
100 5edb4a94 Lance Albertson
101 91767b54 Lance Albertson
# setup serial console
102 91767b54 Lance Albertson
if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
103 07d32070 Lance Albertson
    if [ ! -e ${TARGET}/dev/${disk} ] ; then
104 07d32070 Lance Albertson
        setup_disk_devs
105 07d32070 Lance Albertson
    fi
106 91767b54 Lance Albertson
    if [ -e "${boot_dir}/menu.lst" ] ; then
107 07d32070 Lance Albertson
        # grub 0.x
108 c3d1b43e Lance Albertson
        sed --follow-symlinks -ie 's/^default.*/default 0\n\nserial --unit=0\nterminal --timeout=3 console serial/' \
109 91767b54 Lance Albertson
            ${boot_dir}/menu.lst
110 c3d1b43e Lance Albertson
        sed --follow-symlinks -ie 's/\(.*kernel.*\)/\1 console=ttyS0,115200n8/g' \
111 9404f8cd Lance Albertson
            ${boot_dir}/menu.lst
112 13eb5d4c Lance Albertson
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
113 07d32070 Lance Albertson
        # grub 2.x
114 07d32070 Lance Albertson
        add_devicemap
115 13eb5d4c Lance Albertson
        sed -ie 's/.*GRUB_TERMINAL.*/GRUB_TERMINAL=serial/' ${TARGET}/etc/default/grub
116 9404f8cd Lance Albertson
        sed -ie 's/.*GRUB_CMDLINE_LINUX.*/GRUB_CMDLINE_LINUX=\"console=ttyS0,115200n8\"/' \
117 13eb5d4c Lance Albertson
            ${TARGET}/etc/default/grub
118 91767b54 Lance Albertson
        echo "GRUB_SERIAL_COMMAND=\"serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1\"" \
119 13eb5d4c Lance Albertson
            >> ${TARGET}/etc/default/grub
120 07d32070 Lance Albertson
        chroot ${TARGET} update-grub
121 91767b54 Lance Albertson
    else
122 91767b54 Lance Albertson
        echo "No grub bootloader found, skipping..."
123 91767b54 Lance Albertson
    fi
124 91767b54 Lance Albertson
fi
125 91767b54 Lance Albertson
126 79224631 Lance Albertson
# execute cleanups
127 79224631 Lance Albertson
cleanup
128 79224631 Lance Albertson
trap - EXIT
129 79224631 Lance Albertson
130 79224631 Lance Albertson
exit 0