Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.1 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

    
66
add_devicemap() {
67
    cat > "${TARGET}/boot/grub/device.map" <<EOF
68
(hd0) /dev/${disk}
69
EOF
70
}
71

    
72
if [ -n "${INSTANCE_HV_kernel_path}" -o "${IMPORT_SCRIPT}" = "1" ] ; then
73
    setup_disk_devs
74

    
75
    if [ -e "${boot_dir}/menu.lst" ] ; then
76
        add_devicemap
77
        # install grub to the block device
78
        chroot ${TARGET} grub --batch --no-floppy \
79
            --device-map=/boot/grub/device.map <<EOF
80
root (hd0,0)
81
setup (hd0)
82
quit
83
EOF
84
        # check to see if grub is using UUID's and replace if so
85
        if [ -n "$(grep 'root=UUID' ${boot_dir}/menu.lst)" ] ; then
86
            root_uuid="$($VOL_ID $ROOT_DEV)"
87
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
88
                ${boot_dir}/menu.lst
89
        fi
90
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
91
        chroot ${TARGET} grub2-install /dev/${disk}
92
        # check to see if grub is using UUID's and replace if so
93
        if [ -n "$(grep 'root=UUID' ${boot_dir}/grub.cfg)" ] ; then
94
            root_uuid="$($VOL_ID $ROOT_DEV)"
95
            sed --follow-symlinks -ie "s/\(root=UUID=\)\([a-z0-9-]*\)/\1${root_uuid}/g" \
96
                ${boot_dir}/grub.cfg
97
        fi
98
    fi
99
fi
100

    
101
# setup serial console
102
if [ "${INSTANCE_HV_serial_console}" = "True" ] ; then
103
    if [ ! -e ${TARGET}/dev/${disk} ] ; then
104
        setup_disk_devs
105
    fi
106
    if [ -e "${boot_dir}/menu.lst" ] ; then
107
        # grub 0.x
108
        sed --follow-symlinks -ie 's/^default.*/default 0\n\nserial --unit=0\nterminal --timeout=3 console serial/' \
109
            ${boot_dir}/menu.lst
110
        sed --follow-symlinks -ie 's/\(.*kernel.*\)/\1 console=ttyS0,115200n8/g' \
111
            ${boot_dir}/menu.lst
112
    elif [ -e "${boot_dir}/grub.cfg" -a -e "${TARGET}/etc/default/grub" ] ; then
113
        # grub 2.x
114
        add_devicemap
115
        sed -ie 's/.*GRUB_TERMINAL.*/GRUB_TERMINAL=serial/' ${TARGET}/etc/default/grub
116
        sed -ie 's/.*GRUB_CMDLINE_LINUX.*/GRUB_CMDLINE_LINUX=\"console=ttyS0,115200n8\"/' \
117
            ${TARGET}/etc/default/grub
118
        echo "GRUB_SERIAL_COMMAND=\"serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1\"" \
119
            >> ${TARGET}/etc/default/grub
120
        chroot ${TARGET} update-grub
121
    else
122
        echo "No grub bootloader found, skipping..."
123
    fi
124
fi
125

    
126
# execute cleanups
127
cleanup
128
trap - EXIT
129

    
130
exit 0