bash - test if disk is unpartitioned

Here's a way I'm using for backups.

if [ $(/sbin/sfdisk -d ${DEV} 2>&1) == "" ]; then echo "Device not partitioned"; fi

where ${DEV} is the device you are interrogating (Best ran from a udev script.)


One option to perform the checking and partitioning is the parted command. To get partition information on all disk block devices in the system, in "human-readable" format:

/sbin/parted -l

or, in "machine-readable" format, use:

/sbin/parted -lm

You will need to parse the output in the to determine if the disk has a valid partition table.

Then, use something like:

/sbin/parted <device> --script mkpart primary 1 -- -1

to make a single partition using the whole disk. The syntax on mkpart subcommand is from memory and I do not have a system with an available disk to verify it is correct