Skip to content

Commit c7d2f0e

Browse files
committed
Use GPT label when parted is available or sfdisk supports it
- Use parted by default if available, fallback to sfdisk - Use GPT only when disk is larger than 2TB - Limit partition size to 2TB if parted is not available and sfdisk does not support GPT Signed-off-by: Eric Burgueño <[email protected]>
1 parent 0f60770 commit c7d2f0e

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

container-storage-setup.sh

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -712,32 +712,60 @@ scan_disks() {
712712
}
713713

714714
create_partition_sfdisk(){
715-
local dev="$1" size
715+
local dev="$1" size label="$2" type
716716
# Use a single partition of a whole device
717717
# TODO:
718-
# * Consider gpt, or unpartitioned volumes
718+
# * Consider unpartitioned volumes
719719
# * Error handling when partition(s) already exist
720720
# * Deal with loop/nbd device names. See growpart code
721+
721722
size=$(( $( awk "\$4 ~ /"$( basename $dev )"/ { print \$3 }" /proc/partitions ) * 2 - 2048 ))
722-
cat <<EOF | sfdisk $dev
723+
# if parted is not present, and this version of sfdisk
724+
# does not support GPT then we are limited to 2TB
725+
if [ $label = "gpt" ]; then
726+
if $(sfdisk --help|grep gpt >/dev/null) ; then
727+
label="label: gpt"
728+
type=""
729+
size=$(( $size - 34 )) #34 is the first usable sector for GPT labels
730+
else
731+
Info "Your version of sfdisk does not support GPT labels. Will use MBR instead but partition size will be limited to 2TB"
732+
label=""
733+
type=", Id=8e"
734+
# 2TB in sectors - 2048
735+
if [ $size -gt 4294965248 ]; then size=4294965248; fi
736+
fi
737+
elif [ $label = "msdos" ]; then
738+
label=""
739+
fi
740+
741+
cat <<EOF | sfdisk $dev
723742
unit: sectors
743+
$label
724744
725-
${dev}1 : start= 2048, size= ${size}, Id=8e
745+
${dev}1 : start= 2048, size= ${size}${type}
726746
EOF
727747
}
728748

729749
create_partition_parted(){
730-
local dev="$1"
731-
parted $dev --script mklabel msdos mkpart primary 0% 100% set 1 lvm on
750+
local dev="$1" label="$2"
751+
parted $dev --script mklabel $label mkpart primary 0% 100% set 1 lvm on
732752
}
733753

734754
create_partition() {
735-
local dev="$1"
755+
local dev="$1" size label
756+
757+
#Determine partition size. If larger than 2TB use GPT instead
758+
size=$( awk "\$4 ~ /"$( basename $dev )"/ { print \$3 }" /proc/partitions )
759+
if [ $size -gt 2147483648 ]; then
760+
label="gpt"
761+
else
762+
label="msdos"
763+
fi
736764

737765
if [ -x "/usr/sbin/parted" ]; then
738-
create_partition_parted $dev
766+
create_partition_parted $dev $label
739767
else
740-
create_partition_sfdisk $dev
768+
create_partition_sfdisk $dev $label
741769
fi
742770

743771
# Sometimes on slow storage it takes a while for partition device to

0 commit comments

Comments
 (0)