Add routines to build input strings for sfdisk/sgdisk
authorPatrick McCarty <patrick.mccarty@linux.intel.com>
Thu, 1 Aug 2013 17:50:21 +0000 (10:50 -0700)
committerPatrick McCarty <patrick.mccarty@linux.intel.com>
Mon, 5 Aug 2013 17:00:48 +0000 (10:00 -0700)
To prepare for partitioning the device, build an input string for either
sfdisk (MBR support) or sgdisk (GPT support) by using the INSTALLERFW
variables from /etc/installer.conf.

Change-Id: If30fa2ea22846072f3f8e57f6b1c39365add24e5

scripts/system-installer

index c4f3e36..68d2ecc 100644 (file)
@@ -81,6 +81,76 @@ function find_devices {
     fi
 }
 
+# Global variable for storing the partition option parameters;
+params=
+
+function msdos_partition_options {
+    # the partition number
+    local i=$1
+    # used for storing the per-part variables,
+    # accessed via indirect referencing
+    local tmp=""
+
+    tmp="INSTALLERFW_PART${i}_SIZE"
+    local size=${!tmp}
+    [ -z "$size" ] && size="1024"
+    if [[ "$INSTALLERFW_PART_COUNT" = "$((i+1))" ]]; then
+        # an 'empty' value means that sfdisk will use
+        # the defaults, in this case expanding to the
+        # end of the disk
+        size=""
+    fi
+    params=$(printf "${params}\n%s" ",${size}")
+
+    tmp="INSTALLERFW_PART${i}_FSTYPE"
+    local fstype=${!tmp}
+    local typeid='83'
+    [ "$fstype" = "swap" ] && typeid='82'
+    params=$(printf "${params}%s" ",${typeid}")
+
+    # handle bootflag last
+    tmp="INSTALLERFW_PART${i}_BOOTFLAG"
+    local bootflag=${!tmp}
+    if [ "$bootflag" = "True" ]; then
+        params=$(printf "${params}%s" ',*')
+    else
+        params=$(printf "${params}\n")
+    fi
+}
+
+function gpt_partition_options {
+    # the partition number
+    local i=$1
+    # used for storing the per-part variables,
+    # accessed via indirect referencing
+    local tmp=""
+
+    tmp="INSTALLERFW_PART${i}_ALIGN"
+    local align=${!tmp}
+    [ -n "$align" ] && params=$(printf "${params}%s" " --set-alignment=${align}")
+
+    # sgdisk uses '0' to mean 'default value'
+    params=$(printf "$params%s" " --new=0:0")
+
+    tmp="INSTALLERFW_PART${i}_SIZE"
+    local size=${!tmp}
+    if [[ "$INSTALLERFW_PART_COUNT" = "$((i+1))" ]]; then
+        size="0"
+    fi
+    [ -z "$size" ] && size="1024"
+    [ -n "$size" ] && params=$(printf "${params}%s" ":${size}M")
+
+    # TODO: need to handle swap partition types if encountered
+
+    # the following GUID option does not accept default partition values,
+    # so partition numbering must start at index 1, not 0
+    local partnum="$((i+1))"
+
+    tmp="INSTALLERFW_PART${i}_TYPE_ID"
+    local typeid=${!tmp}
+    [ -n "$typeid" ] && params=$(printf "${params}%s" " --typecode=${partnum}:${typeid}")
+}
+
 function partition_device {
     /usr/sbin/sfdisk -uM $TARGET_DEV << EOF
 ,2000,82
@@ -213,6 +283,17 @@ read_config
 set_mounts
 find_devices
 message "Installing on to the hard disk now, this will take a few minutes..."
+
+pnum=0
+while [ "$pnum" -lt "$INSTALLERFW_PART_COUNT" ]; do
+    if [ "$INSTALLERFW_PTABLE_FORMAT" = "msdos" ]; then
+        msdos_partition_options $pnum
+    elif [ "$INSTALLERFW_PTABLE_FORMAT" = "gpt" ]; then
+        gpt_partition_options $pnum
+    fi
+    pnum="$((pnum+1))"
+done
+
 partition_device
 format_device
 mount_devices