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