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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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"
22 source /usr/lib/installmedia-lib.sh
24 logmsg "Installing OS to HDD"
26 mkdir -p $DEVICE_MOUNT
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
32 find_iso $DEVICE_MOUNT false
34 logmsg "No ISO image to install. Cannot proceed!"
39 if [ -e $CLOUD_CONFIGS/network_config ];then
40 logmsg "Sourcing network_config"
41 source $CLOUD_CONFIGS/network_config
44 if [ -n "${ROOTFS_DISK}" ];then
45 rootdev_by_path=${ROOTFS_DISK}
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)
51 rootdev=$(readlink -e $rootdev_by_path)
53 #Wait for the device to appear for 60 seconds and then give up
54 while [ -z "${rootdev}" ]; do
56 rootdev=$(readlink -e $rootdev_by_path)
57 count=$(( ${count}+1 ))
58 if [ ${count} == 20 ]; then
64 if [ -z ${rootdev} ] || ! [ -b ${rootdev} ]; then
65 logmsg "No matching HDD (${rootdev}) found to install OS image. Cannot proceed!"
68 if ! [ -e $DEVICE_MOUNT/guest-image.img ]; then
69 logmsg "No guest-image.img in CDROM. Cannot proceed!"
73 logmsg "Matching device found for root disk. Installing image on ${rootdev}"
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..."
80 elif ! [ -b /dev/$hd_dev ]; then
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
88 logmsg "Dumping $DEVICE_MOUNT/guest-image.img to $rootdev"
90 # limit the memory usage for qemu-img to 1 GiB
92 qemu-img convert -p -t directsync -O host_device $DEVICE_MOUNT/guest-image.img $rootdev > $CONSOLE_DEV
94 logmsg "Failed to dump image to disk... Failing installation."
100 is_esp_partition_present "${rootdev}"
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}"
114 logmsg "${rootdev} dumped successfully!"
115 echo "Finishing installation... Please wait." > $CONSOLE_DEV
118 # create a new partion for LVMs before rootfs expands till end
119 parted ${rootdev} --script -- mkpart primary 50GiB -1
122 # For GPT and 3 partitions, it might take some time to (re)discover them all
124 mount "${rootdev}${rootpartno}" /sysroot/
126 logmsg "FAILED TO MOUNT SYSROOT. All hope is lost"
130 if [ "${rootdev_has_uefi}" -eq 0 ]; then
131 mount "${rootdev}1" /sysroot/boot/efi
133 logmsg "FAILED TO MOUNT EFI SYSTEM PARTITION. All hope is lost"
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"
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
147 echo "GRUB_CMDLINE_LINUX=\"$kernel_cmdline\"" >> /sysroot/etc/default/grub
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\"
156 run_crit chroot /sysroot /bin/bash -c \"/usr/sbin/grub2-mkconfig -o /boot/grub2/grub.cfg\"
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 /\"
163 logmsg "Copying cloud guest image"
165 run_crit cp -f $DEVICE_MOUNT/guest-image.img $IMAGE_DIR
167 if [ -d $CLOUD_CONFIGS ]; then
168 logmsg "Copying user config"
169 mkdir -p $USERCONFIG_DIR
170 cp -rf $CLOUD_CONFIGS/* $USERCONFIG_DIR
173 if [ -e $DEVICE_MOUNT/rpms ]; then
174 logmsg "Copying build base RPMs"
175 mkdir -p $CLOUD_RPMS_DIR
177 for file in $DEVICE_MOUNT/rpms/*;do
178 cp -a $file $CLOUD_RPMS_DIR
179 echo -n "." > $CONSOLE_DEV
181 echo -n " done" > $CONSOLE_DEV
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
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/
200 if [ "${rootdev_has_uefi}" -eq 0 ]; then
201 umount /sysroot/boot/efi
209 logmsg "Everything done rebooting!"