Page 1 of 1

Partition multiple drives: Scripting "parted"

Posted: Tue Feb 18, 2014 2:29 pm
by peter_b
On RHEL, there's no sgdisk :(
At least not in the default repositories.

So I'm using "parted", but most of the documentation found refers to its interactive mode. I'm dealing with 45 drives per chassis, so I need to script it.
Thanks to a blog post by Roderick Tapang, I now know that one can use "--script -- <command>".

Here's how you script "parted" for creating RAID partitions on 3 TB WDC Red disks:

Code: Select all

#!/bin/bash

RAID_DEV="$1"
RAID_NAME="$2"
DISKS="$3"

echo "Creating SoftRAID using the following devices:"
ls -la $DISKS

if [ -b "$RAID_DEV" ]; then
  echo "ERROR: '$RAID_DEV' already exists."
  exit 2
fi

for DISK in $DISKS; do
  echo "Disk: $DISK"
  if [ ! -b "$DISK" ]; then
    echo "ERROR: '$DISK' is not a block device."
    exit 1
  fi

  # Create GPT partition table:
  parted $DISK --script -- mklabel gpt
  # Set units to Sectors (s):
  parted $DISK --script -- unit s
  # Create one primary partition with 100% size, starting at sector 2048:
  parted $DISK --script -- mkpart primary 2048s 100%
  # Set the partition type to RAID:
  parted $DISK --script -- set 1 raid on
done

# Wait a bit for kernel partition tables to update:
sleep 10

# Create the RAID:
echo "Creating RAID '$RAID_DEV' with name '$RAID_NAME'..."
CMD="mdadm --create $RAID_DEV --level 5 --raid-devices 4 --spare-devices 1 -N $RAID_NAME $DISKS-part1"
echo "$CMD"
eval "$CMD"