Added new script to help clear ZFS labels from a disk
[zfs-ubuntu/.git] / zfs-labelclear
1 #!/bin/bash
2 # Wipes the ZFS labels off of a disk
3 # The metadata of zfs is stored in the first and last two 256kB of each disk involved in zfs.
4
5 DISK=$1
6
7 if [[ -z "$1" ]]; then
8   echo "Usage: $0 <disk>"
9   exit 1
10 fi
11
12 # List the labels
13 echo "-> About to clear these labels off of this disk: $DISK" 
14 lsblk --output NAME,FSTYPE,MODEL,LABEL,PTTYPE,SIZE -e 7 "$DISK"
15 echo "-> Are you sure you want to continue? (y/n)"
16 read input && if [ "$input" != "y" ]; then exit 1; fi
17
18 # Wipe all partitions
19 sudo sgdisk -Z "$DISK"
20
21 # Wipe the metadata off the front of the disk
22 sudo dd if=/dev/zero of="$DISK" count=1 bs=512k
23
24 # Wipe the metadata off the back of the disk
25 sudo dd if=/dev/zero of="$DISK" seek=$(( $(blockdev --getsz "$DISK") - ( ( 256 * 1024 * 2) / $(blockdev --getpbsz "$DISK") ) ))
26
27 # List the labels
28 echo "-> Labels now on the disk"
29 lsblk --output NAME,FSTYPE,MODEL,LABEL,PTTYPE,SIZE -e 7 "$DISK"