Proxmox Wipe Disk

The Promox VE user interface has a button called Wipe Disk that is roughly equivalent to this command:

find /dev/ -name nvme0n1* -exec wipefs --all '{}' \; && dd if=/dev/zero of=/dev/nvme0n1 bs=1M conv=fdatasync count=200

Source Code

# Wipes all labels and the first 200 MiB of a disk/partition (or the whole if it is smaller).
# If called with a partition, also sets the partition type to 0x83 'Linux filesystem'.
# Expected to be called with a result of verify_blockdev_path().
sub wipe_blockdev {
    my ($devpath) = @_;

    my $devname = basename($devpath);
    my $dev_size = PVE::Tools::file_get_contents("/sys/class/block/$devname/size");

    ($dev_size) = $dev_size =~ m|(\d+)|; # untaint $dev_size
    die "Couldn't get the size of the device $devname\n" if !defined($dev_size);

    my $size = ($dev_size * 512 / 1024 / 1024);
    my $count = ($size < 200) ? $size : 200;

    my $to_wipe = [];
    dir_glob_foreach("/sys/class/block/${devname}", "${devname}.+", sub {
        my ($part) = @_;
        push $to_wipe->@*, "/dev/${part}" if -b "/dev/${part}";
    });

    if (scalar($to_wipe->@*) > 0) {
        print "found child partitions to wipe: ". join(', ', $to_wipe->@*) ."\n";
    }
    push $to_wipe->@*, $devpath; # put actual device last

    print "wiping block device ${devpath}\n";

    run_command(['wipefs', '--all', $to_wipe->@*], errmsg => "error wiping '${devpath}'");

    run_command(
        ['dd', 'if=/dev/zero', "of=${devpath}", 'bs=1M', 'conv=fdatasync', "count=${count}"],
        errmsg => "error wiping '${devpath}'",
    );

    if (is_partition($devpath)) {
        eval { change_parttype($devpath, '8300'); };
        warn $@ if $@;
    }
}

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.