From c9c2f7a889bf6746f093f3a413427163aa362800 Mon Sep 17 00:00:00 2001 From: Alex Gonzalez Date: Thu, 5 Dec 2024 12:16:48 +0100 Subject: [PATCH] os-helpers-fs: add function to erase disks 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 --- .../os-helpers/os-helpers/os-helpers-fs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/meta-balena-common/recipes-support/os-helpers/os-helpers/os-helpers-fs b/meta-balena-common/recipes-support/os-helpers/os-helpers/os-helpers-fs index 5f2538100b..5b29d575a5 100644 --- a/meta-balena-common/recipes-support/os-helpers/os-helpers/os-helpers-fs +++ b/meta-balena-common/recipes-support/os-helpers/os-helpers/os-helpers-fs @@ -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 +}