=== modified file 'zsh-functions/confz_fs_init' --- zsh-functions/confz_fs_init 2018-02-01 11:39:34 +0000 +++ zsh-functions/confz_fs_init 2017-12-13 18:06:40 +0000 @@ -43,40 +43,6 @@ 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() { @@ -87,7 +53,6 @@ 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}" @@ -188,8 +153,16 @@ confz_disklabel_dos_check() { checkvars device - do_command=( parted --script $vars[device] -- mklabel msdos ) - fs_check_type $vars[device] dos PTTYPE + 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 } @@ -197,8 +170,16 @@ confz_disklabel_gpt_check() { checkvars device - do_command=( parted --script $vars[device] -- mklabel gpt ) - fs_check_type $vars[device] gpt PTTYPE + 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 } @@ -311,9 +292,16 @@ confz_swap_check() { checkvars device - do_command=( mkswap $vars[device] ) - - fs_check_type $vars[device] swap + 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 } @@ -406,7 +394,10 @@ all_empty=1 for device in $devices; do # all devices either need to have empty labels or be linux_raid_member - if ! fs_check_type $device 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}" all_empty=0 fi done @@ -433,8 +424,16 @@ # set up LVM2 physical volume confz_physical_volume_check() { checkvars device - do_command=( lvm pvcreate $vars[device] ) - fs_check_type $vars[device] LVM2_member + 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 }