From 4c97d6afff1aba7fe17f2c603b40d93dda0554ce Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Thu, 27 Jan 2022 20:00:16 +0100 Subject: [PATCH] started new diskman.sh lib and reflect consequences on conf_disks module --- lib/diskman.sh | 101 ++++++++++++++++++++++++++++++++++++++++++ modules/conf_disks.sh | 24 ---------- 2 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 lib/diskman.sh diff --git a/lib/diskman.sh b/lib/diskman.sh new file mode 100644 index 0000000..bd76900 --- /dev/null +++ b/lib/diskman.sh @@ -0,0 +1,101 @@ +# ------------------------------------------------------------------------------ +# Disks and partitions manipulation function +# This file is part of the init.sh project +# Copyright (c) 2019-2021 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 [[ $(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 [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 diff --git a/modules/conf_disks.sh b/modules/conf_disks.sh index 6d11cae..c216b73 100644 --- a/modules/conf_disks.sh +++ b/modules/conf_disks.sh @@ -69,30 +69,6 @@ conf_zfs() fi } -# ------------------------------------------------------------------------------ -# Creation partitions for XFS or Ext4 -mkpart() -{ - prnt I "Création d'une nouvelle table de partition GPT..." - #fdisk -g -} - -# ------------------------------------------------------------------------------ -conf_ext4() -{ - mkpart - # Todo -} - -# ------------------------------------------------------------------------------ -conf_xfs() -{ - prnt I "Installation des outils XFS..." - pkginst xfsprogs - mkpart - # todo -} - # ------------------------------------------------------------------------------ conf_disks()