20

To start from a clean state I need to reset the hard disk to an empty state from command line.

It is not about running a wipe utility, the data don't have to be overwritten.

This question is quite similar to Deleting All Partitions From the Command Line

The solution there works quite well,

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

but if I want to work with such an overwritten disk, I get the error that the device is still in use.

root@grml ~ # blockdev --rereadpt /dev/sda
BLKRRPART: Device or resource busy

or

root@grml ~ # partprobe
Error: Partition(s) 2, 3 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.
Error: Partition(s) 2, 3 on /dev/sdb have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.

So I have to manually disable everything which "sits" on the device

umount /mnt/debootstrap
umount /mnt/debootstrap/tmp
umount /mnt/debootstrap/var/log
umount /mnt/debootstrap/var
umount /mnt/debootstrap/home
service mdadm stop
service lvm2 stop
vgremove vg_main
pvremove /dev/md1
mdadm --stop /dev/md0
mdadm --stop /dev/md1
mdadm --remove /dev/md0
mdadm --remove /dev/md1

after that the partprobe command works.

is there some command which works simpler? like

harddiskreset /dev/sda

so it can easily be used on systems with different partition/lvm/md layout?

5
  • 1
    Besides rebooting, which is what it advised you to do? What is the context here? Jan 15, 2016 at 0:30
  • i am not sure if i understand your questions. i want to bootstrap a debian system with a bash script which is using setup-storage. to ensure the script always works, the harddisk in the target system should be emptied/reset before. is that the context you asked for? then i would extend my question. it has not seemed relevant to me, because i am just looking for a command which is able to delete mbr & partitions from the harddisk...
    – c33s
    Jan 15, 2016 at 0:55
  • You should wipe the MBR before you set up the partitions. It appears that here, you are doing so after you set them up! Jan 15, 2016 at 0:56
  • Ah ha! I got it. To make it work (from a LiveCD) I first had to do this: sudo wipefs -a /dev/sdb -- fdisk -l showed no partition tables on /dev/sdb -- then -- sudo dd if=/dev/sda of=/dev/sdb worked to make a bootable copy of the primary drive.
    – SDsolar
    Jul 15, 2017 at 17:21
  • Danger: this first command reduced the size of my 64GB sd card to 1KB, and i can't create or destroy filesystems on it because it's too small, and wipefs doesn't work either.
    – Owl
    Dec 5, 2022 at 1:38

5 Answers 5

22

The wipefs (Debian/Ubuntu package: util-linux) program lets you easily delete the partition-table signature:

wipefs -a /dev/sda

You still have to stop any process using the device though, such as LVM.

From man wipefs

wipefs can erase filesystem, raid or partition-table signatures (magic strings) from the specified device to make the signatures invisible for libblkid.

wipefs does not erase the filesystem itself nor any other data from the device. When used without any options, wipefs lists all visible filesystems and the offsets of their basic signatures.

wipefs calls the BLKRRPART ioctl when it has erased a partition-table signature to inform the kernel about the change.

0
5

I have always simply used parted for this. It works well for changing the disklabel type and adding/removing partitions, especially since it can handle modern large HDDs unlike fdisk.

You can run

$ sudo parted /dev/sda

This will get things started and get you into the parted terminal. You can then run the help command to show all the available commands. Its very self explanatory.

I will mention that yes you do have to have all partitions of the disk you want to format unmounted. If you were simply looking for a quicker way to unmount all the partitions, I guess you could do it with a regex in the umount command but that seems silly.

Using parted to manage the HDD I have never had to force a refresh of the disk or anything similar.

To completely refresh a drive for brand new use, I usually do the following:

1) start parted by running sudo parted /dev/sda

2) find any existing partitions by running print

3) remove existing partitions by running rm 1 replacing 1 with the partition number you want to remove. Then repeat for all remaining partitions on the disk.

4) reset the disklabel by running mklabel gpt I use the gpt label type but you could use the standard msdos or whatever your preference is. Here is a list of disklabel types

5) Create new partitions by running mkpart This will run you through the create partition wizard. The start and end points are defaulted to sectors. You can change this by running the unit command before you run mkpart This way you can specify it in GB or TB or MB etc.

6) check your results using print to view your new partition table info

7) You then need to format the partitions. This shouldn't be done through parted although some options for this are available. I would suggest instead running quit to exit the parted terminal and then using mkfs to format the partitions. Remember to run 'mkfs' on /dev/sda1 instead of /dev/sda because you are formatting the partition and not the disk as a whole.

That's about it.

I hope this answers your question.

Also, here is the online parted manual for reference: https://www.gnu.org/software/parted/manual/html_node/index.html

EDIT:

The OP wanted to do this sort of thing from a script and not from a terminal. You can do the same sort of procedure via a script by running parted via single line commands instead of within the parted terminal.

For example the command

$ sudo parted /dev/sda print

Will print out the drive information and partition table onto the bash console which can then be manipulated using grep etc to create variables or whatever you want in a bash script.

8
  • 1
    i need it automated from a script not manualy from the terminal
    – c33s
    Jan 15, 2016 at 1:55
  • Wait your original post doesn't mention a script. What part of this are you scripting? I think you can run parted commands without going into the parted terminal. I'm not sure about that though.
    – KroniK907
    Jan 15, 2016 at 2:46
  • Just to confirm, you can run parted from single line commands. For example parted /dev/sda print will print out the partition table for /dev/sda. You can do the same thing I describe above just with single line commands if you need to do it within a bash script.
    – KroniK907
    Jan 15, 2016 at 2:51
  • but i have to know what the name of the device will be to use the command parted /dev/sda also i have to know how many partitions are on the drive to call rm 1, i am looking for a command to simply clean the disk, so i can repartition it, without doing all the manual stuff of shutting down md's removing lv's and so on.
    – c33s
    Jan 15, 2016 at 3:02
  • 1) you still have to know what the name of the device will be for running a dd command... not sure what you are getting at there. Just grep through df if the drive is mounted. How do you plan on creating a "script" if you cant get the location of the drive? 2) its easy enough to run parted $drive print and grep the results to find all the available partition numbers and then store them in an array. Then use those values to run parted $drive rm $partition. All of this stuff is pretty simple bash scripting.
    – KroniK907
    Jan 21, 2016 at 20:02
4

Use improved non-interactive version of fdisk, which is sfdisk

To erase partition table use this command:

sfdisk --delete /dev/sda
2
  • This is for me. Don't waste my time with unnecessary interactive, non-copy-pasteabble instructions! May 2, 2022 at 21:55
  • Just in case it helps: The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8). May 2, 2022 at 21:56
0

Basically, this error

Error: Partition(s) 1 on /dev/sdc have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use. As a result, the old partition(s) will remain in use. You should reboot now before making further changes.

shows up when you miss some parameter while partitioning your disk. You can fix this error by looking closely to the parameters that you defined while partitioning.

Use : fdisk -l to check the parameters of your disk :

0

A very delayed answer that hopefully helps someone:

I've been using the following script to wipe the complete disk, and recreate a single partition with 100% utilization as below:

#!/bin/bash

DEVICE=$1
TARGET=$2

sudo dd if=/dev/zero of=${DEVICE} bs=1M count=10
sudo partprobe -s "${DEVICE}"
echo -e 'mktable gpt\nmkpart primary xfs 0% 100%\nquit' | sudo parted "${DEVICE}"
sudo partprobe -s "${DEVICE}"
sudo mkfs.xfs "${DEVICE}"1
sudo partprobe -s "${DEVICE}"
sleep 3
echo $(sudo lsblk -o UUID "${DEVICE}"1 |fmt -w 2000 |tr ' ' =) "${TARGET}" xfs defaults 0 0 | sudo tee -a /etc/fstab

Notes:

  • Use dd on the first 10M of the disk to wipe the GPT
  • Use parted to create a new GPT, and a single primary partition starting at 0% and ending at 100%, using echo -e to pipe the commands into parted.
  • make it and XFS partition
  • Add the entry for it into /etc/fstab
  • Also requires sudo systemctl daemon-reload on recent systems that use systemd-mountd.
  • partprobe is interspersed through the commands to update the kernel about the changes
  • A small delay is added between formatting and using the new UUID information, to let the changes percolate across the kernel interfaces (if not, there is a possibility of a corrupted /etc/fstab entry)
  • Parameterized to use device and new mount point.
  • WARNING: DO NOT USE ON THE BOOT DISK, which will be wiped, no questions asked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .