大网数据广告活动
服务器运维问答

How to reset a Harddisk (delete Mbr & delete Partitions) from the Command Line with a script without rebooting?

· 52CE 我爱测

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 Partitio…

问题原文

原帖链接 · c33s · CC BY-SA 3.0

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?

社区高分解答

原帖链接 · user144437 · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。