A rapid way to erase multiple partition tables on Ubuntu/Debian Linux

When doing labs / playing with ZFS on Linux, ZFS pool may creates a few partitions on each drive, we may want to erase all the playground partition table to start from scratch again(though it may not be required), if we have many drives like more than 10, and we want to erase them all, it’ll take a while to finish the job.

Here is a rapid method to do it, using the non-interactive command sgdisk from package gdisk – the GPT fdisk text-mode partitioning tool, with parameter -Z or -o (both work in my case):

$ sudo sgdisk -Z /dev/sda
GPT data structures destroyed! You may now partition the disk using fdisk or other utilities.
$ sudo sgdisk -o /dev/sdb
The operation has completed successfully.

According to the help message, by specifing parameters -Z, it’ll zap (destroy) the GPT and MBR data structures and exit, and by specifing parameters -o, it’ll clear out all partition data. This includes GPT header data, all partition definitions, and the protective MBR.

Because the command sgdisk is non-interactive, so we can simply integrate the whole massive partition clean up job with shell scripts, for example:

#!/bin/bash
sudo sgdisk -Z /dev/sda &
sudo sgdisk -Z /dev/sdb &
sudo sgdisk -Z /dev/sdc &
sudo sgdisk -Z /dev/sdd &
sudo sgdisk -Z /dev/sde &
sudo sgdisk -Z /dev/sdf &
sudo sgdisk -Z /dev/sdg &

That’s it! Happy hacking!

How to determine/find UUID of a partition?

In Linux, UUID(Universally Unique Identifier) can identify media more accurately and reliable, identify media via /dev/hdxy or /dev/sdxy is not a good method because the order may be different between boots, so it was no longer preferred any more, especially in fstab or grub config.

How to find UUIDs of my hard disk partitions?

$ ls -l /dev/disk/by-uuid

total 0
lrwxrwxrwx 1 root root 11 Feb 16 03:59 1898d5ea-dcea-4959-94a9-ff21a163ba85 -> ../../zram5
lrwxrwxrwx 1 root root 11 Feb 16 03:59 41e9022c-9a91-446c-a05c-2e3cd03f2180 -> ../../zram6
lrwxrwxrwx 1 root root 11 Feb 16 03:59 4ca3ac3b-0ada-419c-8c9b-a292c3e41e43 -> ../../zram3
lrwxrwxrwx 1 root root 11 Feb 16 03:59 6165ed95-d7f2-4997-9496-9bc8df9be3d2 -> ../../zram4
lrwxrwxrwx 1 root root 11 Feb 16 03:59 7356c5f2-3379-4953-914c-ba2e598c828e -> ../../zram0
lrwxrwxrwx 1 root root 11 Feb 16 03:59 92d32f20-bc55-4698-9096-a4966061dcf8 -> ../../zram1
lrwxrwxrwx 1 root root 10 Feb 16 03:59 a6127375-d994-4cd0-ab11-5f4bf65f9f3e -> ../../sda1
lrwxrwxrwx 1 root root 11 Feb 16 03:59 af5e0104-1213-4e49-aea2-c177c97e7118 -> ../../zram7
lrwxrwxrwx 1 root root 11 Feb 16 03:59 b4f51d6c-29bd-4655-b6c9-8acb6b8a2c9f -> ../../zram2
lrwxrwxrwx 1 root root 10 Feb 16 03:59 dca199dd-3e9f-4381-8637-22354b0b11f4 -> ../../sda5

or

$ blkid

/dev/sda5: UUID=”dca199dd-3e9f-4381-8637-22354b0b11f4″ TYPE=”swap”
/dev/sda1: UUID=”a6127375-d994-4cd0-ab11-5f4bf65f9f3e” TYPE=”ext4″
/dev/zram0: UUID=”7356c5f2-3379-4953-914c-ba2e598c828e” TYPE=”swap”
/dev/zram1: UUID=”92d32f20-bc55-4698-9096-a4966061dcf8″ TYPE=”swap”
/dev/zram2: UUID=”b4f51d6c-29bd-4655-b6c9-8acb6b8a2c9f” TYPE=”swap”
/dev/zram3: UUID=”4ca3ac3b-0ada-419c-8c9b-a292c3e41e43″ TYPE=”swap”
/dev/zram4: UUID=”6165ed95-d7f2-4997-9496-9bc8df9be3d2″ TYPE=”swap”
/dev/zram5: UUID=”1898d5ea-dcea-4959-94a9-ff21a163ba85″ TYPE=”swap”
/dev/zram6: UUID=”41e9022c-9a91-446c-a05c-2e3cd03f2180″ TYPE=”swap”
/dev/zram7: UUID=”af5e0104-1213-4e49-aea2-c177c97e7118″ TYPE=”swap”

(PS: blkid may need root privilege)

How to generate a new UUID for a partition?
# tune2fs -U random /dev/sda1

tune2fs 1.42.5 (29-Jul-2012)

Use blkid /dev/sda1to check /dev/sda1’s UUID, it’ll be a new one.

Example:

And in /etc/fstab, we should tell others we are now using UUID by UUID= like this: