Files
init.sh/lib/diskman.sh

102 lines
3.2 KiB
Bash

# ------------------------------------------------------------------------------
# Disks and partitions manipulation function
# This file is part of the init.sh project
# Copyright (c) 2019-2021 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
# ------------------------------------------------------------------------------
# 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 [[ $(test -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..."
dd if="/dev/zero" of="$1" bs="512" count="1024"
# Update kernel partition scheme
partprobe
else
prnt E "The /dev/$1 file is not a bloc device!"
die 19
fi
}
# ------------------------------------------------------------------------------
# Check if $1 is a block device and if it's blank
is_blank()
{
# 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
}
# ------------------------------------------------------------------------------
# Creating empty partitions
# mkparts <disk> [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.
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 $@; then
if [[ $part == 0 ]]; then
echo ",,L" >> $tmpfile
else
echo ",$part,L" >> $tmpfile
fi
done
else
echo ",,L" >> $tmpfile
done
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
}
# ------------------------------------------------------------------------------
conf_ext4()
{
# Todo
}
# ------------------------------------------------------------------------------
conf_xfs()
{
# todo
}
# EOF