Partition multiple drives: Scripting "parted"

Linux howto's, compile information, information on whatever we learned on working with linux, MACOs and - of course - Products of the big evil....
Post Reply
User avatar
peter_b
Chatterbox
Posts: 383
Joined: Tue Nov 12, 2013 2:05 am

Partition multiple drives: Scripting "parted"

Post 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"
Post Reply