Scripting fdisk with filesystem signature issues

I’m trying to automatize fdisk with my Bash scripts. In my script, I have the following code block:

echo "Creating root filesystem partition..."
(
   echo n
   echo 3 
   echo
   echo
   echo w
) | fdisk ${DEVICE}

Where the DEVICE is physical disks like /dev/sda /dev/nvme0n1 etc. but not partitions.
However, if the disk has a encrypted file system created before, fdisk asks for removing crypto_LUKS signature in simple Yes/No prompt. I can simply add a echo Y line on the block, but that would lead to a problem for the disks that does not contain any crypto_LUKS signature.

I’ve tried calling wipefs --all --force ${DEVICE} and wipefs --all --force ${DEVICE}[1-9]* before calling that block, however it only removes some ordinary file systems and partition tables, not working for LUKS signatures.

Asked By: izarc

||

So far echoing options to fdisk from standard input/output seems very hardcoded approach, by suggestion of @phuclv using sgdisk is very versatile way automatize your partitioning.

For example, I changed the those lines in my main post to sgdisk based lines, it’s very efficent way for that:

# Create a GPT on the disk:
sgdisk -o ${DEVICE}

# Create the EFI partition:
sgdisk -n 1:0:+512M ${DEVICE}

# Create the /boot partition:
sgdisk -n 2:0:+$512M ${DEVICE}

# Create the / partition:
sgdisk -n 3:0:0 ${DEVICE}

In that triple number group, first number indicates that desired partition number, second number indicates first sector number (put zero if you want to first available sector), and the last number indicates for the partition (you can either use a fixed location or relative location to first sector by size units as I did, or put zero if you want to use a partition along to latest available sector).

If you ask how sgdisk helps to remove LUKS signatures, I can say that sgdisk forces to remove those signatures, not asking like fdisk’s CLI prompt.

Answered By: izarc
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.