e050dd9ddda4139e4a3657d048466d7e61dcdcda
[platform/adaptation/setup-scripts.git] / setup-scripts-boot
1 #!/bin/sh -euf
2
3 # Copyright 2013-2014 Intel Corporation
4 # Author: Artem Bityutskiy
5 # License: GPLv2
6
7 PROG="setup-scripts-boot"
8 VER="1.0"
9
10 srcdir="$(readlink -ev -- ${0%/*})"
11 PATH="/usr/share/setup-scripts:$srcdir:$PATH"
12
13 if [ -f "$srcdir/setup-scripts-sh-functions" ]; then
14         . "$srcdir/setup-scripts-sh-functions"
15         . "$srcdir/installerfw-sh-functions"
16 else
17         .  /usr/share/setup-scripts/setup-scripts-sh-functions
18         .  /usr/share/setup-scripts/installerfw-sh-functions
19 fi
20
21 # This is a small trick which I use to make sure my scripts are portable -
22 # check if 'dash' is present, and if yes - use it.
23 if can_switch_to_dash; then
24         exec dash -euf -- "$srcdir/$PROG" "$@"
25         exit $?
26 fi
27
28 install_gummiboot()
29 {
30         verbose "installing gummiboot to $bootdir"
31
32         local installdir="$bootdir/EFI/boot"
33         local gummiboot_path="$(installerfw_mnt_prefix "/usr/lib/gummiboot")"
34         local src32="$gummiboot_path/gummibootia32.efi"
35         local src64="$gummiboot_path/gummibootx64.efi"
36         local src=""
37         local dst=""
38
39         # Autoboot from fs0 by default
40         local efi_bootdir="fs0:"
41
42         # Make sure gummiboot is installed in the system
43         if ! ([ -f "$src32" ] || [ -f "$src64" ]); then
44                 fatal "\"$gummiboot_path/gummiboot*.efi\" files not found!"
45         fi
46
47         # Install gummiboot
48         mkdir -p $verbose -- "$installdir" >&2
49
50         if [ -f "$src64" ] ; then
51             src="$src64"
52             dst="$installdir/bootx64.efi"
53         elif [ -f "$src32" ] ; then
54             src="$src32"
55             dst="$installdir/bootia32.efi"
56         fi
57
58         if [ -f "$src" ] ; then
59             local basename=$(basename -- "$dst")
60             cp $verbose "$src" "$dst" 2>&2
61             echo "${efi_bootdir}\\EFI\\boot\\$basename" > "${bootdir}/startup.nsh"
62         fi
63
64         verbose "installed gummiboot to $bootdir"
65 }
66
67 install_extlinux()
68 {
69         local installdir="$bootdir/extlinux"
70         local extlinux="extlinux"
71         local output
72
73         # Check if extlinux is available
74         if ! command -v "extlinux" >/dev/null 2>&1; then
75                 extlinux="$(installerfw_mnt_prefix "/sbin/extlinux")"
76                 [ -f "$extlinux" ] ||
77                         fatal "cannot find \"$extlinux\""
78         fi
79
80         # Get device node name for the boot partition
81         local boot_devnode
82         installerfw_get_part_info "/boot" "DEVNODE_NOW" "boot_devnode"
83         [ -n "$boot_devnode" ] || \
84                 fatal "cannot find device node of the boot disk, probably" \
85                       "INSTALLERFW_PARTx_DEVNODE_NOW environment" \
86                       "variable is not defined"
87
88         # Install extlinux
89         verbose "installing extlinux to $bootdir, boot device node is" \
90                 "\"$boot_devnode\""
91         mkdir -p $verbose -- "$installdir" >&2
92         output="$("$extlinux" --device "$boot_devnode" -i "$installdir" 2>&1)" \
93                 || fatal "cannot install extlinux to \"$installdir\" (note," \
94                          "extlinux version 5 or greater is required)" \
95                          "${br}${output}"
96
97         # Get device node name for the boot disk
98         local mbr_devnode
99         installerfw_get_part_info "/boot" "DISK_DEVNODE_NOW" "mbr_devnode"
100         [ -n "$mbr_devnode" ] || \
101                 fatal "cannot find device node of the boot disk, probably" \
102                       "INSTALLERFW_PARTx_DISK_DEVNODE_NOW environment" \
103                       "variable is not defined"
104
105         # Install the MBR part of extlinux
106         local mbr_bin="$(installerfw_mnt_prefix \
107                          "/usr/share/syslinux/gptmbr.bin")"
108         verbose "setting up MBR, writing \"$mbr_bin\" to \"$mbr_devnode\""
109         output="$(dd if="$mbr_bin" of="$mbr_devnode" count=1 2>&1)" || \
110                 fatal "cannot install MBR, dd if=$mbr_bin of=$mbr_devnode" \
111                       "failed${br}${output}"
112
113         verbose "installed extlinux to $bootdir"
114 }
115
116 show_usage()
117 {
118         cat <<-EOF
119 Usage: $PROG [options]
120
121 Install the EFI bootloader (gummiboot) and create the initial configuration
122 for all the currently installed kernels. This program depends on various
123 "installer framework" variables.
124
125 Options:
126   -v, --verbose  be verbose
127   --version      show the program version and exit
128   -h, --help     show this text and exit
129 EOF
130 }
131
132 show_usage_fail()
133 {
134         IFS= printf "%s\n\n" "$PROG: error: $*" >&2
135         show_usage >&2
136         exit 1
137 }
138
139 # Parse the options
140 tmp=`getopt -n $PROG -o v,h --long verbose,version,help -- "$@"` ||
141         show_usage_fail "cannot parse command-line options"
142 eval set -- "$tmp"
143
144 verbose=
145 while true; do
146         case "$1" in
147         -v|--verbose)
148                 verbose="-v"
149                 ;;
150         --version)
151                 printf "%s\n" "$VER"
152                 exit 0
153                 ;;
154         -h|--help)
155                 show_usage
156                 exit 0
157                 ;;
158         --) shift; break
159                 ;;
160         *) show_usage_fail "unrecognized option \"$1\""
161                 ;;
162         esac
163         shift
164 done
165
166 bootdir="$(installerfw_mnt_prefix "/boot")"
167
168 if installerfw_available; then
169         installerfw_save_env
170 else
171         installerfw_restore_env
172 fi
173
174 # Get OS name
175 get_os_name "os_name"
176
177 if installerfw_is_efi_boot_system; then
178         install_gummiboot
179 else
180         install_extlinux
181 fi
182
183 # Create bootloader entries for each kernel
184 kernels="$(ls -1 "$bootdir" | LC_ALL=C grep -- "^vmlinuz-" | sort -r)"
185
186 [ -n "$kernels" ] || \
187         fatal "no kernels (vmlinuz-*) found in \"$bootdir\""
188
189 printf "%s\n" "$kernels" | while IFS= read -r kernel; do
190         setup-scripts-bootloader-conf $verbose add --force "$kernel"
191 done
192
193 # Set the default kernel to the kernel with highest version
194 newest_kernel="$(get_newest_kernel "$bootdir")"
195 setup-scripts-bootloader-conf $verbose default "$newest_kernel"
196
197 # And finally, create the /etc/fstab file
198 setup-scripts-fstab $verbose --force