#!/bin/bash # ------------------------------------------------------------------------------ # Disks and partitions manipulation function # This file is part of the init.sh project # Copyright (c) 2019-2024 Geoffray Levasseur # ------------------------------------------------------------------------------ # This file is distributed under 3-clause BSD license. # The complete license agreement can be obtained at: # https://opensource.org/licenses/BSD-3-Clause # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Blank the given disks blank_disk() { if [[ -b /dev/$1 ]]; then prnt I "Wipping $1 drive signature (a backup is made in /root)..." wipefs --force --all --backup /dev/$1 prnt I "Filling beginning of $1 drive with zeroes..." if [[ $2 == "--full" ]]; then # If full we display progress as it might take a very long time # Need true to avoid error as the last byte will generate a disk # full error dd if="/dev/zero" of="$1" bs="512" status=progress || true else dd if="/dev/zero" of="$1" bs="512" count="1024" fi # Update kernel partition scheme partprobe else prnt E "The /dev/$1 file is not a bloc device!" die 19 fi } export -f blank_disk # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Check if $1 is a blank block device. Return values: # * 0 if empty block device # * 1 if not empty block device # * 2 if not block device or if it don't exists is_blank() { if [[ -b /dev/$1 ]]; then # That technique is fast but might not be relayable enough if we are on # exotic data structure or filesystem... local devstat=$(file /dev/$1 | sed "s@/dev/$1: @@") if [[ $devstat == 'data' ]]; then return 0 else return 1 fi else return 2 fi } export -f is_blank # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Creating empty partitions # mkparts [dos|gpt] [list_of_partition_size] # Partition size like 10G for 10 GiB, 600M for 600 MiB and so on... Without unit # it will use a number of cylinder. 0 will stand for all remaining size. # If no partition size is provided we create a single whole disk partition. # TODO: support extended partition for DOS type, add error if trying to create # more than 4 primary partitions mkparts() { local device=$1 && shit if [[ $1 == "gtp" || $1 == "dos" ]]; then local parttype=$1 && shift else # Default is GPT local parttype="gpt" fi if [[ $(is_blank $device) ]]; then prnt I "Creating a new ${parttype^^} partition table on $device..." sfdisk -label /dev/$device $parttype prnt I "Creating a new partition scheme on /dev/$1..." local tmpfile=$(mktemp sfd.XXXX) if [[ -n $1 ]]; then # For each given size we make a partition for part in $@; do # If size is zero we interpret it as all available space if [[ $part == 0 ]]; then echo ",,L" >> $tmpfile else echo ",$part,L" >> $tmpfile fi done else echo ",,L" >> $tmpfile fi echo "write" >> $tmpfile sfdisk "/dev/$device" < $tmpfile rm $tmpfile unset tmpfile else prnt E "The $1 block device is not blank, for security we won't proceed!" die 20 fi unset device parttype } export -f mkparts # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Generic caller for mkfs.* tools (not exported) mkfs_gen() { for drv in $@; do if [[ -b /dev/$drv ]]; then $mkfstool $MKFSOPT /dev/$drv else prnt E "/dev/$drv is not a bloc device!" die 18 fi done } export -f mkfs_gen # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Format drive using ext4 filesystem, parameters will be a list of block device mkext4() { export mkfstool="mkfs.ext4" mkfs_gen $@ unset mkfstool } export -f mkext4 # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # Format a XFS filesystem... mkxfs() { export mkfstool="mkfs.xfs" mkfs_gen $@ unset mkfstool } export -f mkxfs # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # ... NTFS ... mkntfs() { export mkfstool="mkfs.ntfs" mkfs_gen $@ unset mkfstool } export -f mkntfs # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # ... FAT32 ... mkfat32() { export mkfstool="mkfs.vfat" mkfs_gen $@ unset mkfstool } export -f mkfat32 # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ # ... BTRFS ... mkbtrfs() { export mkfstool="mkfs.btrfs" mkfs_gen $@ unset mkfstool } export -f mkbtrfs # ------------------------------------------------------------------------------ # EOF