4

I wish to use sgdisk to delete all partition definitions and data from a storage device, then create a GPT and two partitions on it . The best solution I've found is to use the -Z and the -o options.

sgdisk -Zo \
    -n 1::16M -t 1:EF00 -c 1:ZNX_BOOT \
    -N 2 -t 2:8300 -c 2:ZNX_DATA /dev/[device]

This, however, fails if the drive was created with an MBR partition table or if a disk image was written to it. How can I get this command to wipe any disk regardless of the partition table style used on it?

3

I know this is changing the question but is it possible for you to use wipefs (which comes as part of the util-linux package)? wipefs understands how to clear all sorts of partition(ing) metadata and will correctly get rid of MBRs, GPTs (along with the backup GPT), filesystem signatures etc...

The examples from the EXAMPLES section at the bottom of the wipefs man page illustrate how you might do this:

wipefs /dev/sda*

Prints information about sda and all partitions on sda.

wipefs --all --backup /dev/sdb

Erases all signatures from the device /dev/sdb and creates a signature backup file ~/wipefs-sdb-<offset>.bak for each signature.

WARNING: As the name suggests wipefs destroys data! Please be careful...

16
  • What about it's performance? @K7AAY suggested dd, but it's too slow. If it's as fast (or at least similar) as sgdisk, then I could consider using it. Sep 7 '18 at 2:54
  • 1
    @luis-lavaire I'm amazed that a dd over one sector (assuming 512 byte sectors) of a disk is slow - it's pretty much the smallest write operation a disk could do! I'd guess that something else is wrong if that really was slow (e.g. count=1 had been left off). I suppose you could use bs=4k oflag=direct count=1 and see if that helps... Nonetheless to answer your question: in my experience wipefs finishes in less than a second (but the dd of the answer should have done so too).
    – Anon
    Sep 7 '18 at 5:40
  • 1
    Hmm blkid is probably grovelling around because UDF filesystems often aren't inside partitions. github.com/karelzak/util-linux/blob/… shows the routine. Did I mention that wipefs is good at removing filesystem signatures too? :-) It even uses blkid (github.com/mmalecki/util-linux/blob/… )...
    – Anon
    Sep 12 '18 at 21:30
  • 1
    Can you please [add another answer | edit your answer] so I can accept it? wipefs worked just as spected. ;) Sep 15 '18 at 6:13
  • 1
    Is the answer any better now? Oh and thanks for persevering :-)
    – Anon
    Sep 15 '18 at 7:06
2

I reget I see no way to do that exclusively within sgdisk, but fortunately, the kind folks at Virtualhelp.me have suggested the use of dd first, i.e.,

dd if=/dev/zero of=/dev/sda bs=512 count=1

which is intended to overwrite that pesky MBR carrier partition which throws both sgdisk and gdisk for a loop.

4
  • 1
    Yeah, I kew about dd. The drawback is that it is VERY slow. That's why I prefer gdisk. Sep 7 '18 at 2:53
  • Understood, but since a review of man sqdisk and man gdisk showed they could not deal with MBR carrier partitions, thought I would offer what I could. EFI is soooo much better than MBR, I cringe every time someone says they don;t need EFI...
    – K7AAY
    Sep 7 '18 at 17:15
  • 1
    I guess that possibly using while ! sgdisk -v /dev/device; do sgdisk -Z; done could do the task. And I agree with you, that GPT is way better than MBR. Sep 10 '18 at 4:35
  • 3
    Note that if you must use dd to blank GPT partitioning then you will need to remove the first 20 Kbytes from the start AND the end of the disk. See serverfault.com/a/787210/203726 for details (also see en.wikipedia.org/wiki/GUID_Partition_Table ).
    – Anon
    Sep 10 '18 at 21:14
0

This commands did the job:

wipefs -af $DEVICE

sgdisk -Z \
    -n 1::132M -t 1:EF00 -c 1:ZNX_BOOT \
    -N 2 -t 2:8300 -c 2:ZNX_DATA $DEVICE

The firs command clears the partition metadata, and the second one, clears the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.