@@ -39,6 +39,12 @@ extract_write_files_paths() {
3939 echo " ${paths[@]} "
4040}
4141
42+ # Function to convert date string to seconds since epoch
43+ convert_to_epoch () {
44+ local date_string=" $1 "
45+ date -d " ${date_string: 0: 8} ${date_string: 8: 2} :${date_string: 10: 2} :${date_string: 12: 2} " +%s
46+ }
47+
4248# Specify the configuration file
4349config_file= " /etc/cloud/config-file"
4450
@@ -62,6 +68,59 @@ error_exit() {
6268 exit 1
6369}
6470
71+ # Function to perform before update
72+ perform_update_check () {
73+ local image_path=" $1 "
74+
75+ # Mandatory checks before the update
76+
77+ # Decompress the image
78+ gunzip -c " $image_path " > /etc/cloud/update.raw
79+
80+ # Set up the loop device with the decompressed image
81+ loopdevice=$( losetup --find --partscan --show /etc/cloud/update.raw)
82+
83+ # Extract UUID from the loop device partition
84+ local uuid
85+ uuid=$( lsblk -no UUID " $loopdevice " p2)
86+
87+ # Get boot UUID from bootctl list
88+ bootctl list | grep -E ' default|boot_uuid' > /etc/cloud/input.txt
89+ local boot_uuid
90+ boot_uuid=$( awk ' /\(default\)/ {getline; if ($0 ~ /options/) {match($0, /boot_uuid=([a-f0-9-]+)/, arr); print arr[1]; exit}}' /etc/cloud/input.txt)
91+
92+ # Clean up temporary files
93+ rm -rf /etc/cloud/input.txt /etc/cloud/update.raw
94+
95+ # Check #1 Compare UUIDs
96+ if [ " $uuid " = " $boot_uuid " ]; then
97+ echo " UUID of update image and provisioned image are the same"
98+ losetup -d " $loopdevice "
99+ exit 1
100+ fi
101+
102+ # Check #2 Upgrades only with future dates
103+ # Convert both dates to seconds since epoch
104+ mount " $loopdevice " p2 /mnt
105+ IMAGE_BUILD_DATE=$( sed -n ' s/^IMAGE_BUILD_DATE=//p' /etc/image-id)
106+ FUTURE_DATE=$( sed -n ' s/^IMAGE_BUILD_DATE=//p' /mnt/etc/image-id)
107+ image_build_epoch=$( convert_to_epoch " $IMAGE_BUILD_DATE " )
108+ upgrade_image_date_epoch=$( convert_to_epoch " $FUTURE_DATE " )
109+ umount /mnt
110+
111+ if [ " $upgrade_image_date_epoch " -lt " $image_build_epoch " ]; then
112+ echo " Downgrades are not supported. Only upgrades with images having a future build date are allowed."
113+ losetup -d " $loopdevice "
114+ exit 1
115+ else
116+ echo " The image build date is on or after the future date."
117+ fi
118+
119+ # Detach the loop device
120+ losetup -d " $loopdevice "
121+ }
122+
123+
65124# Function to display usage information
66125show_help () {
67126 echo " Usage: $0 [options]"
182241 echo " Extracted SHA256 checksum: $SHA_ID "
183242fi
184243
244+ # Call the function with the path to the image
245+ perform_update_check " $IMAGE_PATH "
246+
185247# Invoke the os-update-tool.sh script
186248echo " Initiating OS update..."
187249/usr/bin/os-update-tool.sh -w -u " $IMAGE_PATH " -s " $SHA_ID "
0 commit comments