EFI support for installmedia dracut module
[ta/image-provision.git] / dracut / modules / 00installmedia / installmedia-lib.sh
1 #!/bin/bash
2
3 # Copyright 2019 Nokia
4
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 export SYS_BLOCK="/sys/class/block"
18 export CONSOLE_DEV="/dev/tty0"
19 export BOOTCD_LOCATION="/run/boot.iso"
20 export CLOUD_CONFIGS="/tmp/cloud_configs"
21 if (! declare -f warn );then
22     echo "warn function not defined assume running outside of dracut" > $CONSOLE_DEV
23     alias warn=echo
24 fi
25
26 function is_using_boot_cd(){
27     if [ -a $BOOTCD_LOCATION ];then
28         return 0
29     fi
30     return 1
31 }
32
33 function logmsg(){
34     message="$@"
35     echo $message > $CONSOLE_DEV
36     warn $message
37 }
38
39 function run_crit(){
40     OUTPUT=$(echo $@ | /bin/bash)
41     if [ $? -ne 0 ]; then
42         logmsg "Failed to execute $@::$OUTPUT"
43         exit 1
44     fi
45     echo $OUTPUT
46 }
47
48 function read_devices(){
49     # Get list of block devices on the system
50     device_list=$(ls $SYS_BLOCK)
51     read -r -a hd_devices <<< $device_list
52     export hd_devices
53 }
54
55 function check_params()
56 {
57     if [ $1 -ne $(($#-1)) ];then
58         echo "Not enough params for ${FUNCNAME[ 1 ]}" > $CONSOLE_DEV
59         exit 1
60     fi
61 }
62
63 function try_mount(){
64     check_params 3 "$@"
65     dev=$1
66     device_mount=$2
67     umount_on_found=$3
68     if [ -e $dev ] && [ -b $dev ];then
69
70         mount -o ro -t iso9660 $dev $device_mount
71         if [ $? -ne 0 ];then
72             return 1
73         else
74             if [ -e "$device_mount/guest-image.img" ];then
75                 if ( $umount_on_found ); then
76                     umount $device_mount
77                 fi
78                 return 0
79             else
80                 umount $device_mount
81                 return 1
82             fi
83         fi
84     else
85         return 1
86     fi
87 }
88
89 function is_loop(){
90     check_params 1 "$@"
91     device=$1
92     if [ -e $SYS_BLOCK/$device/loop ]; then
93         return 0
94     fi
95     return 1
96 }
97
98 function is_partition(){
99     check_params 1 "$@"
100     device=$1
101     if [ -e $SYS_BLOCK/$device/partition ];then
102         return 0
103     fi
104     return 1
105 }
106
107 function is_removable(){
108     check_params 1 "$@"
109     device=$1
110     sysdev=$SYS_BLOCK/$device
111     if ( is_partition $device );then
112         removable=$(readlink -f $sysdev/..)/removable
113     else
114         removable=$sysdev/removable
115     fi
116     if [ -e $removable ] && [ $(cat $removable) -eq 1 ];then
117         return 0
118     else
119         return 1
120     fi
121
122 }
123
124 function is_esp_partition_present(){
125     check_params 1 "$@"
126     device=$1
127     if [ 'vfat' = "$(blkid -o value -s TYPE ${device}1)" ]; then
128         return 0
129     fi
130     return 1
131 }
132
133 function get_config_from_device_end(){
134     check_params 1 "$@"
135     local BOOTDEVICE=$1
136
137     if [ -b $BOOTDEVICE ]; then
138         SKIP=$(($(blockdev --getsize64 $BOOTDEVICE)/2048-32))
139         dd if=$BOOTDEVICE of=/tmp/cloudconf.tgz bs=2k skip=$SKIP
140         if gzip -t /tmp/cloudconf.tgz 2>/dev/null ; then
141             logmsg "Copying cloud configurations to $CLOUD_CONFIGS"
142             mkdir -p $CLOUD_CONFIGS
143             tar xzf /tmp/cloudconf.tgz -C $CLOUD_CONFIGS
144             return $?
145         else
146             return 1
147         fi
148     else
149         return 1
150     fi
151
152 }
153
154 function find_iso(){
155     check_params 2 "$@"
156     device_mount=$1
157     umount_on_found=$2
158     if [ ! -d $device_mount  ];then
159        mkdir -p $device_mount
160     fi
161     read_devices
162     for device in ${hd_devices[@]}; do
163         if ( is_removable $device );then
164             if ( try_mount /dev/$device $device_mount $umount_on_found );then
165                 logmsg "installmedia: found image from device $device."
166                 export BOOTDEVICE=/dev/${device}
167                 return 0
168             fi
169         fi
170     done
171     return 1
172 }