1 # Copyright 2013-2014 Intel Corporation
2 # Author: Artem Bityutskiy
5 # This file contains common functions for setup-scripts-* programs
8 __PROG="${PROG:-installerfw-sh-functions}"
10 # Installer framework variables are saved in this file
11 __installerfw_file="/etc/installerfw-environment"
12 # The OS release information file
13 __osrelease_file="/etc/os-release"
14 # EFI System Partition PARTUUID
15 __esp_ptypeid="C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
19 IFS= printf "%s\n" "$__PROG: error: $*" 1>&2
25 if [ -n "$verbose" ]; then
26 IFS= printf "%s\n" "$__PROG (verbose): $*" >&2
30 # Verify that an environment variable is defined
31 installerfw_verify_defined()
35 printenv "$variable" > /dev/null ||
36 __fatal "cannot find required environment variable" \
40 # Add the INSTALLERFW_MOUNT_PREFIX prefix to a path. This does not really
41 # require a separate function unless we want to be fancy and avoid double or
42 # tripple "/" in the resulting path.
43 installerfw_mnt_prefix()
45 local path="${INSTALLERFW_MOUNT_PREFIX:-}/$1"
47 printf "%s" "$path" | LC_ALL=C sed -e 's/\/\+/\//g'
50 # Return full path to the file which contains the installer framework
51 # environment variables.
52 installerfw_get_env_file_name()
54 printf "%s" "$(installerfw_mnt_prefix "$__installerfw_file")"
57 # Save installer framework environment variables. Note, all the variables can
58 # be split on 2 classes - those which make sense only inside the particular
59 # installer and those which make sense in the OS environment. We only save the
61 installerfw_save_env()
63 local file="$(installerfw_get_env_file_name)"
65 -e '^INSTALLERFW_KERNEL_OPTS=' \
66 -e '^INSTALLERFW_PART[[:digit:]]\+_ALIGN=' \
67 -e '^INSTALLERFW_PART[[:digit:]]\+_BOOTFLAG=' \
68 -e '^INSTALLERFW_PART[[:digit:]]\+_FSOPTS=' \
69 -e '^INSTALLERFW_PART[[:digit:]]\+_FSTYPE=' \
70 -e '^INSTALLERFW_PART[[:digit:]]\+_LABEL=' \
71 -e '^INSTALLERFW_PART[[:digit:]]\+_MOUNTPOINT=' \
72 -e '^INSTALLERFW_PART[[:digit:]]\+_PARTUUID=' \
73 -e '^INSTALLERFW_PART[[:digit:]]\+_SIZE=' \
74 -e '^INSTALLERFW_PART[[:digit:]]\+_TYPE_ID=' \
75 -e '^INSTALLERFW_PART[[:digit:]]\+_UUID=' \
76 -e '^INSTALLERFW_PART_COUNT=' \
77 -e '^INSTALLERFW_PTABLE_FORMAT=' \
78 -e '^INSTALLERFW_INSTALLER_NAME=' \
81 local variables="$(printenv | eval "LC_ALL=C grep $opts")"
83 if [ "$(printf "%s\n" "$variables" | wc -l)" -eq "0" ]; then
84 __fatal "no installer framework environment variables" \
85 "found, nothing to save"
88 printf "%s\n" "$variables" | LC_ALL=C sed -n -e \
89 "s/\(^INSTALLERFW_[^=]\+\)=\(.*\)/\1=\"\2\"/p" > "$file"
90 __verbose "installerfw_save_env(): saved installer framework" \
91 "environment in \"$file\""
94 # Restore installer framework environment variables.
95 installerfw_restore_env()
97 local file="$(installerfw_get_env_file_name)"
100 __fatal "installerfw_restore_env(): can't restore the" \
101 "installer framework environment: can't find" \
104 while IFS= read -r line || [ -n "$line" ]; do
108 __verbose "installerfw_restore_env(): restored installer" \
109 "framework environment from \"$file\""
112 # Check whether installer framework variables are defined
113 installerfw_available()
115 if printenv | LC_ALL=C grep -q "^INSTALLERFW_[^[:blank:]]\+"; then
122 # Check if the system is an EFI boot system by checking whether the boot
123 # partition is a FAT 32 partition with the magic EFI type GUID.
124 installerfw_is_efi_boot_system()
126 installerfw_get_part_info "/boot" "TYPE_ID" "__ptypeid"
128 # Make sure the UUID uses capital letters
129 __ptypeid="$(printf "%s" "$__ptypeid" | tr "[:lower:]" "[:upper:]")"
131 installerfw_verify_defined "INSTALLERFW_PTABLE_FORMAT"
133 if [ "${INSTALLERFW_PTABLE_FORMAT:-}" = "gpt" ] && \
134 [ "$__ptypeid" = "$__esp_ptypeid" ]; then
135 __verbose "installerfw_is_efi_boot_system(): /boot is" \
136 "the EFI system partition"
139 __verbose "installerfw_is_efi_boot_system(): no EFI" \
140 "system partition found"
145 # Get a piece of installer framework data for a partition. At the moment the
146 # partition is specified by it's mount point (in $1), but this can be extended
147 # to also accept the partition number, if needed.
149 # The second parameter ($2) is the a partial installer framework variable name
150 # which should be returned. For example, "PARTUUID" would correspond to
151 # "INSTALLERFW_PARTx_PARTUUID", and so on.
153 # The third parameter ($3) is name of the variable to store the result at. If
154 # the requested installer framework variable is undefined or null, the shell
155 # variable with name stored in $3 will have null value upon exit.
156 installerfw_get_part_info()
158 local __mntpoint="$1"; shift
159 local __var="$1"; shift
160 local __res_var="$1"; shift
163 installerfw_verify_defined "INSTALLERFW_PART_COUNT"
165 while [ "$__pnum" -lt "$INSTALLERFW_PART_COUNT" ]; do
166 local __mp="INSTALLERFW_PART${__pnum}_MOUNTPOINT"
167 installerfw_verify_defined "$__mp"
169 __mp="$(eval printf "%s" "\"\$$__mp\"")"
171 [ "$__mp" != "$__mntpoint" ] || break
173 __pnum="$((__pnum+1))"
176 local installerfw_var="INSTALLERFW_PART${__pnum}_${__var}"
179 if printenv "$installerfw_var" > /dev/null; then
180 __value="$(eval printf "%s" "\"\$$installerfw_var\"")"
183 __verbose "installerfw_get_part_info(): $__res_var=$__value"
184 eval "$__res_var"="\"\$__value\""