commit 1948997a65aab3edd4ac84c885eb7185f3235152
parent b0c94536fb2e8c4142e7d6eec18ccfa2edcdde4c
Author: Jan Pobrislo <ccx@webprojekty.cz>
Date: Thu, 1 Feb 2018 12:39:34 +0100
Factor out the fs/layout checking and handle newer blkid not failing on blank devices but providing info about containing partition layout.
Diffstat:
1 file changed, 45 insertions(+), 44 deletions(-)
diff --git a/zsh-functions/confz_fs_init b/zsh-functions/confz_fs_init
@@ -43,6 +43,40 @@ fs_blkid_probe() {
esac
}
+# Check that given device is formatted with given type (return 0) or blank (return 1), die otherwise
+fs_check_type() {
+ local device fstype kind
+ device=$1
+ fstype=$2
+ kind=${3:-TYPE} # TYPE = filesystem type, PTTYPE = partition format
+ fs_blkid_probe $device
+ if (($fs_blkid_result)); then
+ # empty label
+ return 1
+ elif (( $+fs_blkid_output[$kind] )); then
+ if [[ ${fs_blkid_output[$kind]:-} == $fstype ]]; then
+ return 0
+ else
+ die "$0: non-$fstype label (${fs_blkid_output[$kind]}) already present on device: ${(qqq)device}"
+ fi
+ else
+ local -a labels
+ local k v
+ for k v in "${(kv)fs_blkid_output[@]}"; do
+ # Filter out the information about partition layout rather than content
+ case $k in
+ (DEVNAME) continue;;
+ (PART_ENTRY_*) continue;;
+ (*) labels+=( "$k=${(qqq)v}" )
+ esac
+ done
+ if (( $#labels )); then
+ die "$0: unexpected filesystem labels present on device: ${(qqq)device} $labels[*]"
+ fi
+ # empty label
+ return 1
+ fi
+}
# helper for listing partition table
fs_parted_list() {
@@ -53,6 +87,7 @@ fs_parted_list() {
local line got_header number out
local NumberE StartB StartE EndB EndE SizeB SizeE FSB FSE NameB NameE FlagsB
local TypeB TypeE
+ local MATCH MBEGIN MEND
[[ -b $1 ]] || die "no such block device: ${(qqq)1}"
@@ -153,16 +188,8 @@ confz_disk_id_check() {
confz_disklabel_dos_check() {
checkvars device
- fs_blkid_probe $vars[device]
- if (($fs_blkid_result)); then
- # empty label
- do_command=( parted --script $vars[device] -- mklabel msdos )
- return 1
- elif [[ ${fs_blkid_output[PTTYPE]:-} == dos ]]; then
- return 0
- else
- die "$0: non-MBR label already present on device: ${(qqq)vars[device]}"
- fi
+ do_command=( parted --script $vars[device] -- mklabel msdos )
+ fs_check_type $vars[device] dos PTTYPE
}
@@ -170,16 +197,8 @@ confz_disklabel_dos_check() {
confz_disklabel_gpt_check() {
checkvars device
- fs_blkid_probe $vars[device]
- if (($fs_blkid_result)); then
- # empty label
- do_command=( parted --script $vars[device] -- mklabel gpt )
- return 1
- elif [[ ${fs_blkid_output[PTTYPE]:-} == gpt ]]; then
- return 0
- else
- die "$0: non-GPT label already present on device: ${(qqq)vars[device]}"
- fi
+ do_command=( parted --script $vars[device] -- mklabel gpt )
+ fs_check_type $vars[device] gpt PTTYPE
}
@@ -292,16 +311,9 @@ confz_bootable_partition_check() {
confz_swap_check() {
checkvars device
- fs_blkid_probe $vars[device]
- if (($fs_blkid_result)); then
- # empty label
- do_command=( mkswap $vars[device] )
- return 1
- elif [[ ${fs_blkid_output[TYPE]:-} == swap ]]; then
- return 0
- else
- die "$0: non-swap label already present on device: ${(qqq)vars[device]}"
- fi
+ do_command=( mkswap $vars[device] )
+
+ fs_check_type $vars[device] swap
}
@@ -394,10 +406,7 @@ confz_mdraid_check() {
all_empty=1
for device in $devices; do
# all devices either need to have empty labels or be linux_raid_member
- fs_blkid_probe $device
- if ! (($fs_blkid_result)); then
- [[ ${fs_blkid_output[TYPE]:-} == linux_raid_member ]] || \
- die "$0: non-raid label present on device: ${(qqq)device}"
+ if ! fs_check_type $device linux_raid_member
all_empty=0
fi
done
@@ -424,16 +433,8 @@ confz_mdraid_check() {
# set up LVM2 physical volume
confz_physical_volume_check() {
checkvars device
- fs_blkid_probe $vars[device]
- if (($fs_blkid_result)); then
- # no label on blockdev
- do_command=( lvm pvcreate $vars[device] )
- return 1
- elif [[ ${fs_blkid_output[TYPE]:-} == LVM2_member ]]; then
- return 0
- else
- die "$0: non-LVM2 label on ${(qqq)vars[device]}"
- fi
+ do_command=( lvm pvcreate $vars[device] )
+ fs_check_type $vars[device] LVM2_member
}