Seed code for image-provision
[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 get_config_from_device_end(){
125     check_params 1 "$@"
126     local BOOTDEVICE=$1
127
128     if [ -b $BOOTDEVICE ]; then
129         SKIP=$(($(blockdev --getsize64 $BOOTDEVICE)/2048-32))
130         dd if=$BOOTDEVICE of=/tmp/cloudconf.tgz bs=2k skip=$SKIP
131         if gzip -t /tmp/cloudconf.tgz 2>/dev/null ; then
132             logmsg "Copying cloud configurations to $CLOUD_CONFIGS"
133             mkdir -p $CLOUD_CONFIGS
134             tar xzf /tmp/cloudconf.tgz -C $CLOUD_CONFIGS
135             return $?
136         else
137             return 1
138         fi
139     else
140         return 1
141     fi
142
143 }
144
145 function find_iso(){
146     check_params 2 "$@"
147     device_mount=$1
148     umount_on_found=$2
149     if [ ! -d $device_mount  ];then
150        mkdir -p $device_mount
151     fi
152     read_devices
153     for device in ${hd_devices[@]}; do
154         if ( is_removable $device );then
155             if ( try_mount /dev/$device $device_mount $umount_on_found );then
156                 logmsg "installmedia: found image from device $device."
157                 export BOOTDEVICE=/dev/${device}
158                 return 0
159             fi
160         fi
161     done
162     return 1
163 }