diff options
author | Naresh Kamboju <naresh.kamboju@linaro.org> | 2020-07-27 17:32:02 +0530 |
---|---|---|
committer | Naresh Kamboju <naresh.kamboju@linaro.org> | 2020-07-28 08:32:21 +0530 |
commit | c8ef43d9f41f7fa14ca60fc65daeb828a0db57be (patch) | |
tree | a2f740d7406e259e439cba68166acfad239e2422 | |
parent | ca6bf26e05a608b0314534d4a9999efa3e53c7a3 (diff) |
lib: Add disk-partition-format library functionslib-disk-partition-format
Adding disk-partition-format library to add these functions
- partition_disk
- format_partition
Signed-off-by: Naresh Kamboju <naresh.kamboju@linaro.org>
-rwxr-xr-x | automated/lib/disk-partition-format | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/automated/lib/disk-partition-format b/automated/lib/disk-partition-format new file mode 100755 index 00000000..558c865e --- /dev/null +++ b/automated/lib/disk-partition-format @@ -0,0 +1,84 @@ +#!/bin/sh +# Description: parted is not working on SSD (Solid-State Drives) this is the +# reason to use fdisk instead. + +LANG=C +export LANG + +# fdisk command might in $PATH /sbin or /usr/sbin or /usr/local/sbin +# export PATH for better usage. +export PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin + +# shellcheck disable=SC2039 +partition_disk() { + local DEVICE="$1" + local SIZES="$2" + if [ -z "${DEVICE}" ]; then + echo "ERROR: no device given" + exit 1 + fi + + # Create a new empty DOS partition table + ( + echo o + echo w) | fdisk "${DEVICE}" + + if [ -n "${SIZES}" ]; then + # Create partitions as per sizes + for size in ${SIZES}; do + # Create patitions with ${size} + ( + echo n + echo p + echo + echo + echo "${size}" + echo w) | fdisk "${DEVICE}" + done + fi + + # Create a partition at the end. + # Use the reset of the disk. + ( + echo n + echo p + echo + echo + echo + echo w) | fdisk "${DEVICE}" + return 0 +} + +# shellcheck disable=SC2039 +format_partition() { + local DEVICE="$1" + local FILESYSTEM="$2" + local PARTITION="" + local TOTAL_PARTITIONS="" + + if [ -z "${DEVICE}" ]; then + echo "ERROR: no device given" + exit 1 + fi + if [ -z "${FILESYSTEM}" ]; then + echo "No file system type found" + exit 1 + fi + + # Total partitions in a block device + TOTAL_PARTITIONS=$(find "${DEVICE}"* | grep "[0-9]" | tr '\n' ' ' ) + + # Format each partitions in a given drive + for PARTITION in ${TOTAL_PARTITIONS} ; do + echo "Formatting ${PARTITION} to ${FILESYSTEM}" + if [ "${FILESYSTEM}" = "fat32" ]; then + echo "y" | mkfs -t vfat -F 32 "${PARTITION}" + else + echo "y" | mkfs -t "${FILESYSTEM}" "${PARTITION}" + fi + + sync + sleep 5 + done + return 0 +} |