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