Make exact match on `GRUB_CMDLINE_LINUX`
[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 logmsg "${rootdev} dumped successfully!"
100 echo "Finishing installation... Please wait." > $CONSOLE_DEV
101
102 partprobe ${rootdev}
103 # create a new partion for LVMs before rootfs expands till end
104 parted ${rootdev} --script -- mkpart primary 50GiB -1
105 partprobe ${rootdev}
106
107 mount ${rootdev}1 /sysroot/
108 if [ $? -ne 0 ];then
109     logmsg "FAILED TO MOUNT SYSROOT. All hope is lost"
110     exit 255
111 fi
112
113 kernel_cmdline="intel_iommu=on iommu=pt crashkernel=256M"
114 # Check if this has a iscsi target if so, add extra cmdline option for HDD boot.
115 iscsiadm -m fw >/dev/null 2>&1
116 if [[ $? == 0 ]]; then
117   kernel_cmdline="${kernel_cmdline} rd.iscsi.firmware=1 rd.retry=30"
118 fi
119 if grep -q "^GRUB_CMDLINE_LINUX=" /sysroot/etc/default/grub; then
120   sed -i "s/^\(GRUB_CMDLINE_LINUX=.*\)\"$/\1 $kernel_cmdline\"/g" /sysroot/etc/default/grub
121 else
122   echo "GRUB_CMDLINE_LINUX=\"$kernel_cmdline\"" >> /sysroot/etc/default/grub
123 fi
124
125 run_crit mount -o bind /dev /sysroot/dev
126 run_crit mount -o bind /proc /sysroot/proc
127 run_crit mount -o bind /sys /sysroot/sys
128 run_crit chroot /sysroot /bin/bash -c \"/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg\"
129
130 logmsg "Extending partition and filesystem size"
131 run_crit chroot /sysroot /bin/bash -c \"growpart ${rootdev} 1\"
132 run_crit chroot /sysroot /bin/bash -c \"xfs_growfs /\"
133
134 logmsg "Copying cloud guest image"
135 mkdir -p $IMAGE_DIR
136 run_crit cp -f $DEVICE_MOUNT/guest-image.img $IMAGE_DIR
137
138 if [ -d $CLOUD_CONFIGS ]; then
139   logmsg "Copying user config"
140   mkdir -p $USERCONFIG_DIR
141   cp -rf $CLOUD_CONFIGS/* $USERCONFIG_DIR
142 fi
143
144 if [ -e $DEVICE_MOUNT/rpms ]; then
145     logmsg "Copying build base RPMs"
146     mkdir -p $CLOUD_RPMS_DIR
147     echo -n "."
148     for file in $DEVICE_MOUNT/rpms/*;do
149         cp -a $file $CLOUD_RPMS_DIR
150         echo -n "." > $CONSOLE_DEV
151     done
152     echo -n " done" > $CONSOLE_DEV
153     echo > $CONSOLE_DEV
154     echo > $CONSOLE_DEV
155 fi
156
157 logmsg "Disabling cloud-init services on this node"
158 run_crit chroot /sysroot /bin/systemctl --no-reload disable cloud-config.service
159 run_crit chroot /sysroot /bin/systemctl --no-reload disable cloud-final.service
160 run_crit chroot /sysroot /bin/systemctl --no-reload disable cloud-init.service
161 run_crit chroot /sysroot /bin/systemctl --no-reload disable cloud-init-local.service
162
163
164 logmsg "Copying installation logs"
165 mkdir -p /sysroot/var/log/provisioning-logs
166 cp -rf /run/log/ /sysroot/var/log/provisioning-logs/
167 cp -rf /run/initramfs/ /sysroot/var/log/provisioning-logs/
168
169
170 sync
171 umount /sysroot/dev
172 umount /sysroot/proc
173 umount /sysroot/sys
174 umount /sysroot
175 umount $DEVICE_MOUNT
176 sleep 2
177 logmsg "Everything done rebooting!"
178 reboot -f