Skip to content

Commit

Permalink
os-helpers-fs: add function to erase disks
Browse files Browse the repository at this point in the history
The function overwrites the first 16 MiB of each partition (the default
LUKS2 header size), as well as the primary and backup GPT partition
tables.

Change-type: patch
Signed-off-by: Alex Gonzalez <[email protected]>
  • Loading branch information
alexgg committed Dec 5, 2024
1 parent e6d699b commit c9c2f7a
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,22 @@ split_bootpartition() {

# Do not umount - flasher script will still copy configuration files
}

erase_disk() {
_internal_dev="${1}"
info "Erasing disk ${_internal_dev}"
_sector_size=$(lsblk -nlbo NAME,PHY-SEC,TYPE "/dev/${_internal_dev}" | grep disk | awk '{print $2}')
_size=$(lsblk -nlbo NAME,SIZE,TYPE "/dev/${_internal_dev}" | grep disk | awk '{print $2}')
_size_in_sectors=$( expr ${_size} / ${_sector_size} )
# Overwrite default LUKS2 header size of 16 MiB
_erased_sectors=$( expr 16 \* 1024 \* 1024 / ${_sector_size} )
# Erase the first sectors in each partition
for _ss in $(parted "/dev/${_internal_dev}" unit s print | awk '/^[ 0-9]/ {sub("s", ""); print $2}'); do
dd if=/dev/urandom of="/dev/${_internal_dev}" bs="${_sector_size}" count="${_erased_sectors}" seek="${_ss}" conv=sync
done
# Erase partition table
# GPT reserves 34 sector at the start of disk and end of disk as backup
GPT_RESERVED_LBA=34
dd if=/dev/urandom of="/dev/${_internal_dev}" bs="${_sector_size}" count="${GPT_RESERVED_LBA}" conv=sync
dd if=/dev/urandom of="/dev/${_internal_dev}" bs="${_sector_size}" seek="$(expr "${_size_in_sectors}" - "${GPT_RESERVED_LBA}" )" count="${GPT_RESERVED_LBA}" conv=sync
}

0 comments on commit c9c2f7a

Please sign in to comment.