Fix installerfw environment save/restore
[platform/adaptation/setup-scripts.git] / installerfw-sh-functions
1 # Copyright 2013 Intel Corporation
2 # Author: Artem Bityutskiy
3 # License: GPLv2
4
5 # This file contains common functions for setup-ivi-* programs
6
7 # Own name
8 __PROG="${PROG:-installerfw-sh-functions}"
9
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"
16
17 # Mount prefix is assumed to be "/" if it is not defined
18 [ -n "${INSTALLERFW_MOUNT_PREFIX:-}" ] || export INSTALLERFW_MOUNT_PREFIX="/"
19
20 __fatal()
21 {
22         IFS= printf "%s\n" "$__PROG: error: $*" 1>&2
23         exit 1
24 }
25
26 __verbose()
27 {
28         if [ -n "$verbose" ]; then
29                 IFS= printf "%s\n" "$__PROG (verbose): $*" >&2
30         fi
31 }
32
33 # Verify that an environment variable is defined
34 installerfw_verify_defined()
35 {
36         local variable="$1"
37
38         printenv "$variable" > /dev/null ||
39                 __fatal "cannot find required environment variable" \
40                         "\"$variable\""
41 }
42
43 # Add the INSTALLERFW_MOUNT_PREFIX prefix to a path. This does not really
44 # require a separate function unless we want to be fancy and avoid double or
45 # tripple "/" in the resulting path.
46 installerfw_mnt_prefix()
47 {
48         installerfw_verify_defined "INSTALLERFW_MOUNT_PREFIX"
49
50         local path="$INSTALLERFW_MOUNT_PREFIX/$1"
51
52         printf "%s" "$path" | LC_ALL=C sed -e 's/\/\+/\//g'
53 }
54
55 # Return full path to the file which contains the installer framework
56 # environment variables.
57 installerfw_get_env_file_name()
58 {
59         printf "%s" "$(installerfw_mnt_prefix "$__installerfw_file")"
60 }
61
62 # Save installer framework environment variables. Note, all the variables can
63 # be split on 2 classes - those which make sense only inside the particular
64 # installer and those which make sense in the OS environment. We only save the
65 # latter.
66 installerfw_save_env()
67 {
68         local file="$(installerfw_get_env_file_name)"
69         local opts="\
70 -e '^INSTALLERFW_KERNEL_OPTS=' \
71 -e '^INSTALLERFW_PART[[:digit:]]\+_ALIGN=' \
72 -e '^INSTALLERFW_PART[[:digit:]]\+_BOOTFLAG=' \
73 -e '^INSTALLERFW_PART[[:digit:]]\+_FSOPTS=' \
74 -e '^INSTALLERFW_PART[[:digit:]]\+_FSTYPE=' \
75 -e '^INSTALLERFW_PART[[:digit:]]\+_LABEL=' \
76 -e '^INSTALLERFW_PART[[:digit:]]\+_MOUNTPOINT=' \
77 -e '^INSTALLERFW_PART[[:digit:]]\+_PARTUUID=' \
78 -e '^INSTALLERFW_PART[[:digit:]]\+_SIZE=' \
79 -e '^INSTALLERFW_PART[[:digit:]]\+_TYPE_ID=' \
80 -e '^INSTALLERFW_PART[[:digit:]]\+_UUID=' \
81 -e '^INSTALLERFW_PART_COUNT=' \
82 -e '^INSTALLERFW_PTABLE_FORMAT=' \
83 "
84
85         local variables="$(printenv | eval "LC_ALL=C grep $opts")"
86
87         if [ "$(printf "%s" "$variables" | wc -l)" -eq "0" ]; then
88                 __fatal "no installer framework environment variables" \
89                         "found, nothing to save"
90         fi
91
92         printf "%s" "$variables" | LC_ALL=C sed -n -e \
93                 "s/\(^INSTALLERFW_[^=]\+\)=\(.*\)/\1=\"\2\"/p" > "$file"
94         __verbose "installerfw_save_env(): saved installer framework" \
95                   "environment in \"$file\""
96 }
97
98 # Restore installer framework environment variables.
99 installerfw_restore_env()
100 {
101         local file="$(installerfw_get_env_file_name)"
102
103         [ -f "$file" ] || \
104                 __fatal "installerfw_restore_env(): can't restore the" \
105                         "installer framework environment: can't find" \
106                         "\"$file\""
107
108         while IFS= read -r line; do
109                 eval "export $line"
110         done < "$file"
111
112         __verbose "installerfw_restore_env(): restored installer" \
113                   "framework environment from \"$file\""
114 }
115
116 # Check whether installer framework variables are defined
117 installerfw_available()
118 {
119         if printenv | LC_ALL=C grep -q "^INSTALLERFW_[^[:blank:]]\+"; then
120                 return 0;
121         else
122                 return 1;
123         fi
124 }
125
126 # Check if the system is an EFI boot system by checking whether the boot
127 # partition is a FAT 32 partition with the magic EFI type GUID.
128 installerfw_is_efi_boot_system()
129 {
130         installerfw_get_part_info "/boot" "TYPE_ID" "__ptypeid"
131
132         # Make sure the UUID uses capital letters
133         __ptypeid="$(printf "%s" "$__ptypeid" | tr "[:lower:]" "[:upper:]")"
134
135         installerfw_verify_defined "INSTALLERFW_PTABLE_FORMAT"
136
137         if [ "${INSTALLERFW_PTABLE_FORMAT:-}" = "gpt" ] && \
138            [ "$__ptypeid" = "$__esp_ptypeid" ]; then
139                 __verbose "installerfw_is_efi_boot_system(): /boot is" \
140                           "the EFI system partition"
141                 return 0
142         else
143                 __verbose "installerfw_is_efi_boot_system(): no EFI" \
144                           "system partition found"
145                 return 1
146         fi
147 }
148
149 # Get a piece of installer framework data for a partition. At the moment the
150 # partition is specified by it's mount point (in $1), but this can be extended
151 # to also accept the partition number, if needed.
152 #
153 # The second parameter ($2) is the a partial installer framework variable name
154 # which should be returned. For example, "PARTUUID" would correspond to
155 # "INSTALLERFW_PARTx_PARTUUID", and so on.
156 #
157 # The third parameter ($3) is name of the variable to store the result at. If
158 # the requested installer framework variable is undefined or null, the shell
159 # variable with name stored in $3 will have null value upon exit.
160 installerfw_get_part_info()
161 {
162         local __mntpoint="$1"; shift
163         local __var="$1"; shift
164         local __res_var="$1"; shift
165         local __pnum="0"
166
167         installerfw_verify_defined "INSTALLERFW_PART_COUNT"
168
169         while [ "$__pnum" -lt "$INSTALLERFW_PART_COUNT" ]; do
170                 local __mp="INSTALLERFW_PART${__pnum}_MOUNTPOINT"
171                 installerfw_verify_defined "$__mp"
172
173                 __mp="$(eval printf "%s" "\"\$$__mp\"")"
174
175                 [ "$__mp" != "$__mntpoint" ] || break
176
177                 __pnum="$((__pnum+1))"
178         done
179
180         local installerfw_var="INSTALLERFW_PART${__pnum}_${__var}"
181
182         local __value=
183         if printenv "$installerfw_var" > /dev/null; then
184                 __value="$(eval printf "%s" "\"\$$installerfw_var\"")"
185         fi
186
187         __verbose "installerfw_get_part_info(): $__res_var=$__value"
188         eval "$__res_var"="\"\$__value\""
189 }