#!/bin/sh

if [ `whoami` != "root" ]
then
    echo "Error: $0 must be run as root"
    exit 1
fi

target=$1

if [ "x$target" = "x" ]
then
    echo "Error: no taget specified"
    echo "Usage: $0 target"
    exit 1
fi

if [ `echo $target | cut -f1 -d/` != "dev" ]
then
    target="/dev/$target"
fi

if [ ! -b $target ]
then
    echo "Error: $target is no block device"
    exit 1
fi

echo "target: $target"

# zero the first 512MB, in case there already is a partition out there

dd if=/dev/zero of=$target bs=16384k count=32 status=progress

# give the OS some time to figure out that there is no partition on
# this disk.
sleep 15

# create a "msdos" disk label.
parted -a optimal $target mklabel msdos

# create a primary partition of type ext 4, covering the whole disk
parted -a optimal $target mkpart primary ext4 '0%' '100%'

# make sure that the alignment checks out.
parted -a optimal $target align-check optimal 1

# display the partition table
parted -a optimal $target print

# create an ext4 partition
mkfs -t ext4 -v ${target}1

# take a peek
ls -ls ${target}*

# done.
sync



