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