From 6909c3e71c2663e5ddd4dec881dc9cd3cea91501 Mon Sep 17 00:00:00 2001
From: "Alan J. Pippin" <ajp@pippins.net>
Date: Sun, 8 Jan 2023 10:19:58 -0700
Subject: [PATCH] Added new script to help clear ZFS labels from a disk

Change-Id: I0eea1cafb1f8ab4405809ee7156ce120a1927ae6
---
 zfs-labelclear | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100755 zfs-labelclear

diff --git a/zfs-labelclear b/zfs-labelclear
new file mode 100755
index 0000000..a1f07b4
--- /dev/null
+++ b/zfs-labelclear
@@ -0,0 +1,29 @@
+#!/bin/bash
+# Wipes the ZFS labels off of a disk
+# The metadata of zfs is stored in the first and last two 256kB of each disk involved in zfs.
+
+DISK=$1
+
+if [[ -z "$1" ]]; then
+  echo "Usage: $0 <disk>"
+  exit 1
+fi
+
+# List the labels
+echo "-> About to clear these labels off of this disk: $DISK" 
+lsblk --output NAME,FSTYPE,MODEL,LABEL,PTTYPE,SIZE -e 7 "$DISK"
+echo "-> Are you sure you want to continue? (y/n)"
+read input && if [ "$input" != "y" ]; then exit 1; fi
+
+# Wipe all partitions
+sudo sgdisk -Z "$DISK"
+
+# Wipe the metadata off the front of the disk
+sudo dd if=/dev/zero of="$DISK" count=1 bs=512k
+
+# Wipe the metadata off the back of the disk
+sudo dd if=/dev/zero of="$DISK" seek=$(( $(blockdev --getsz "$DISK") - ( ( 256 * 1024 * 2) / $(blockdev --getpbsz "$DISK") ) ))
+
+# List the labels
+echo "-> Labels now on the disk"
+lsblk --output NAME,FSTYPE,MODEL,LABEL,PTTYPE,SIZE -e 7 "$DISK"
-- 
2.34.1