@@ -35,6 +35,8 @@ usage() {
3535 exit 1
3636}
3737
38+ MAX_DISK_PARTITIONS=128
39+
3840CONFIGDRIVE=" $1 "
3941DEVICE=" $2 "
4042
@@ -57,13 +59,41 @@ if [[ $? == 0 ]]; then
5759 log " Existing configdrive found on ${DEVICE} at ${EXISTING_PARTITION} "
5860 ISO_PARTITION=$EXISTING_PARTITION
5961else
60- # Create small partition at the end of the device
61- log " Adding configdrive partition to $DEVICE "
62- parted -a optimal -s -- $DEVICE mkpart primary ext2 -64MiB -0 || fail " creating configdrive on ${DEVICE} "
6362
64- # Find partition we just created
65- # Dump all partitions, ignore empty ones, then get the last partition ID
66- ISO_PARTITION=` sfdisk --dump $DEVICE | grep -v ' 0,' | tail -n1 | awk ' {print $1}' ` || fail " finding ISO partition created on ${DEVICE} "
63+ # Check if it is GPT partition and needs to be re-sized
64+ partprobe $DEVICE print 2>&1 | grep " fix the GPT to use all of the space"
65+ if [[ $? == 0 ]]; then
66+ log " Fixing GPT to use all of the space on device $DEVICE "
67+ sgdisk -e $DEVICE || fail " move backup GPT data structures to the end of ${DEVICE} "
68+
69+ # Need to create new partition for config drive
70+ # Not all images have partion numbers in a sequential numbers. There are holes.
71+ # These holes get filled up when a new partition is created.
72+ TEMP_DIR=" $( mktemp -d) "
73+ EXISTING_PARTITION_LIST=$TEMP_DIR /existing_partitions
74+ UPDATED_PARTITION_LIST=$TEMP_DIR /updated_partitions
75+
76+ gdisk -l $DEVICE | grep -A$MAX_DISK_PARTITIONS " Number Start" | grep -v " Number Start" > $EXISTING_PARTITION_LIST
77+
78+ # Create small partition at the end of the device
79+ log " Adding configdrive partition to $DEVICE "
80+ sgdisk -n 0:-64MB:0 $DEVICE || fail " creating configdrive on ${DEVICE} "
81+
82+ gdisk -l $DEVICE | grep -A$MAX_DISK_PARTITIONS " Number Start" | grep -v " Number Start" > $UPDATED_PARTITION_LIST
83+
84+ CONFIG_PARTITION_ID=` diff $EXISTING_PARTITION_LIST $UPDATED_PARTITION_LIST | tail -n1 | awk ' {print $2}' `
85+ ISO_PARTITION=" ${DEVICE}${CONFIG_PARTITION_ID} "
86+ else
87+ log " Working on MBR only device $DEVICE "
88+
89+ # Create small partition at the end of the device
90+ log " Adding configdrive partition to $DEVICE "
91+ parted -a optimal -s -- $DEVICE mkpart primary ext2 -64MiB -0 || fail " creating configdrive on ${DEVICE} "
92+
93+ # Find partition we just created
94+ # Dump all partitions, ignore empty ones, then get the last partition ID
95+ ISO_PARTITION=` sfdisk --dump $DEVICE | grep -v ' 0,' | tail -n1 | awk ' {print $1}' ` || fail " finding ISO partition created on ${DEVICE} "
96+ fi
6797fi
6898
6999# This writes the ISO image to the config drive.
0 commit comments