EFI support for installmedia dracut module
[ta/image-provision.git] / dracut / modules / 00installmedia / installmedia.sh
1 #!/bin/sh
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 DEVICE_MOUNT="/tmp/installmedia"
18 CLOUD_RPMS_DIR="/sysroot/var/cloud/basebuild_rpms"
19 USERCONFIG_DIR="/sysroot/etc/userconfig/"
20 IMAGE_DIR="/sysroot/opt/images"
21
22 source /usr/lib/installmedia-lib.sh
23
24 logmsg "Installing OS to HDD"
25
26 mkdir -p $DEVICE_MOUNT
27
28 if ( is_using_boot_cd );then
29   warn "Must be boot_cd env. Mounting $BOOTCD_LOCATION"
30   run_crit mount -o ro -t iso9660 $BOOTCD_LOCATION $DEVICE_MOUNT
31 else
32   find_iso $DEVICE_MOUNT false
33   if [ $? -ne 0 ];then
34     logmsg "No ISO image to install. Cannot proceed!"
35     exit 1
36   fi
37 fi
38
39 if [ -e $CLOUD_CONFIGS/network_config ];then
40   logmsg "Sourcing network_config"
41   source $CLOUD_CONFIGS/network_config
42 fi
43
44 if [ -n "${ROOTFS_DISK}" ];then
45     rootdev_by_path=${ROOTFS_DISK}
46 else
47     # Find out the disk name where rootfs has to be installed
48     rootdev_by_path=$(python /usr/lib/python2.7/site-packages/hw_detector/local_os_disk.py)
49 fi
50
51 rootdev=$(readlink -e $rootdev_by_path)
52
53 #Wait for the device to appear for 60 seconds and then give up
54 while [ -z "${rootdev}" ]; do
55     sleep 3
56     rootdev=$(readlink -e $rootdev_by_path)
57     count=$(( ${count}+1 ))
58     if [ ${count} == 20 ]; then
59        break
60     fi
61
62 done
63
64 if [ -z ${rootdev} ] || ! [ -b ${rootdev} ]; then
65   logmsg "No matching HDD (${rootdev}) found to install OS image. Cannot proceed!"
66   exit 1
67 fi
68 if ! [ -e $DEVICE_MOUNT/guest-image.img ]; then
69   logmsg "No guest-image.img in CDROM. Cannot proceed!"
70   exit 1
71 fi
72
73 logmsg "Matching device found for root disk. Installing image on ${rootdev}"
74
75 read_devices
76 for hd_dev in ${hd_devices[@]}; do
77     if [ -b /dev/$hd_dev ] && (( is_removable $hd_dev ) || ( is_partition $hd_dev ) || ( is_loop $hd_dev )); then
78         logmsg "Removable, loop or partition $hd_dev. Skipping..."
79         continue
80     elif ! [ -b /dev/$hd_dev ]; then
81         continue
82     fi
83     logmsg "Erasing existing GPT and MBR data structures from ${hd_dev}"
84     sgdisk -Z /dev/$hd_dev
85     dd if=/dev/zero of=/dev/$hd_dev bs=1M count=1
86 done
87
88 logmsg "Dumping $DEVICE_MOUNT/guest-image.img to $rootdev"
89
90 # limit the memory usage for qemu-img to 1 GiB
91 ulimit -v 1048576
92 qemu-img convert -p -t directsync -O host_device $DEVICE_MOUNT/guest-image.img $rootdev > $CONSOLE_DEV
93 if [ $? -ne 0 ]; then
94     logmsg "Failed to dump image to disk... Failing installation."
95     exit 255
96 fi
97 sync
98
99 partprobe ${rootdev}
100 is_esp_partition_present "${rootdev}"
101 rootdev_has_uefi=$?
102
103 # For UEFI systems (which imply using a GPT partition table), fix the GPT backup
104 # data structures by moving them to the end of the disk; rootfs partition will
105 # have index 3 (1=ESP, 2=boot, 3=rootfs), unlike BIOS/MBR layout where it has 1.
106 if [ "${rootdev_has_uefi}" -eq 0 ]; then
107     sgdisk -e "${rootdev}"
108     sleep 5
109     rootpartno=3
110 else
111     rootpartno=1
112 fi
113
114 logmsg "${rootdev} dumped successfully!"
115 echo "Finishing installation... Please wait." > $CONSOLE_DEV
116
117 partprobe ${rootdev}
118 # create a new partion for LVMs before rootfs expands till end
119 parted ${rootdev} --script -- mkpart primary 50GiB -1
120 partprobe ${rootdev}
121
122 # For GPT and 3 partitions, it might take some time to (re)discover them all
123 sleep 5
124 mount "${rootdev}${rootpartno}" /sysroot/
125 if [ $? -ne 0 ];then
126     logmsg "FAILED TO MOUNT SYSROOT. All hope is lost"
127     exit 255
128 fi
129
130 if [ "${rootdev_has_uefi}" -eq 0 ]; then
131     mount "${rootdev}1" /sysroot/boot/efi
132     if [ $? -ne 0 ];then
133         logmsg "FAILED TO MOUNT EFI SYSTEM PARTITION. All hope is lost"
134         exit 255
135     fi
136 fi
137
138 kernel_cmdline="intel_iommu=on iommu=pt crashkernel=256M"
139 # Check if this has a iscsi target if so, add extra cmdline option for HDD boot.
140 iscsiadm -m fw >/dev/null 2>&1
141 if [[ $? == 0 ]]; then
142   kernel_cmdline="${kernel_cmdline} rd.iscsi.firmware=1 rd.retry=30"
143 fi
144 if grep -q "^GRUB_CMDLINE_LINUX=" /sysroot/etc/default/grub; then
145   sed -i "s/^\(GRUB_CMDLINE_LINUX=.*\)\"$/\1 $kernel_cmdline\"/g" /sysroot/etc/default/grub
146 else
147   echo "GRUB_CMDLINE_LINUX=\"$kernel_cmdline\"" >> /sysroot/etc/default/grub
148 fi
149
150 run_crit mount -o bind /dev /sysroot/dev
151 run_crit mount -o bind /proc /sysroot/proc
152 run_crit mount -o bind /sys /sysroot/sys
153 if [ "${rootdev_has_uefi}" -eq 0 ]; then
154     run_crit chroot /sysroot /bin/bash -c \"/usr/sbin/grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg\"
155 else
156     run_crit chroot /sysroot /bin/bash -c \"/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg\"
157 fi
158
159 logmsg "Extending partition and filesystem size"
160 run_crit chroot /sysroot /bin/bash -c \"growpart ${rootdev} ${rootpartno}\"
161 run_crit chroot /sysroot /bin/bash -c \"xfs_growfs /\"
162
163 logmsg "Copying cloud guest image"
164 mkdir -p $IMAGE_DIR
165 run_crit cp -f $DEVICE_MOUNT/guest-image.img $IMAGE_DIR
166
167 if [ -d $CLOUD_CONFIGS ]; then
168   logmsg "Copying user config"
169   mkdir -p $USERCONFIG_DIR
170   cp -rf $CLOUD_CONFIGS/* $USERCONFIG_DIR
171 fi
172
173 if [ -e $DEVICE_MOUNT/rpms ]; then
174     logmsg "Copying build base RPMs"
175     mkdir -p $CLOUD_RPMS_DIR
176     echo -n "."
177     for file in $DEVICE_MOUNT/rpms/*;do
178         cp -a $file $CLOUD_RPMS_DIR
179         echo -n "." > $CONSOLE_DEV
180     done
181     echo -n " done" > $CONSOLE_DEV
182     echo > $CONSOLE_DEV
183     echo > $CONSOLE_DEV
184 fi
185
186 logmsg "Disabling cloud-init services on this node"
187 run_crit chroot /sysroot /bin/systemctl --no-reload disable cloud-config.service
188 run_crit chroot /sysroot /bin/systemctl --no-reload disable cloud-final.service
189 run_crit chroot /sysroot /bin/systemctl --no-reload disable cloud-init.service
190 run_crit chroot /sysroot /bin/systemctl --no-reload disable cloud-init-local.service
191
192
193 logmsg "Copying installation logs"
194 mkdir -p /sysroot/var/log/provisioning-logs
195 cp -rf /run/log/ /sysroot/var/log/provisioning-logs/
196 cp -rf /run/initramfs/ /sysroot/var/log/provisioning-logs/
197
198
199 sync
200 if [ "${rootdev_has_uefi}" -eq 0 ]; then
201     umount /sysroot/boot/efi
202 fi
203 umount /sysroot/dev
204 umount /sysroot/proc
205 umount /sysroot/sys
206 umount /sysroot
207 umount $DEVICE_MOUNT
208 sleep 2
209 logmsg "Everything done rebooting!"
210 reboot -f