=== modified file 'zsh-functions/confz_fs_init' --- zsh-functions/confz_fs_init 2014-10-28 13:37:48 +0000 +++ zsh-functions/confz_fs_init 2014-06-26 19:36:48 +0000 @@ -5,232 +5,6 @@ # -# helper for probing devices -fs_blkid_probe() { - typeset -gA fs_blkid_output - typeset -g fs_blkid_result - local out key val - fs_blkid_output=( ) - - [[ -b $1 ]] || die "no such block device: ${(qqq)1}" - - out=$( blkid -o export -p $1 ) - fs_blkid_result=$? - - case $fs_blkid_result in - (2);; - (0) while IFS='=' read key val; do - fs_blkid_output[$key]=$val - done <<<$out;; - (*) die "blkid probe failed on device: ${(qqq)1}" - esac -} - - -# helper for listing partition table -fs_parted_list() { - typeset -gA fs_parted_start fs_parted_end fs_parted_size fs_parted_type fs_parted_other - local out number start end size type other - - [[ -b $1 ]] || die "no such block device: ${(qqq)1}" - - out=$( parted --script $1 -- unit s print ) || \ - die "parted print of device failed: ${(qqq)1}" - - while read number start end size type other; do - [[ $number == [0-9]* ]] || continue - fs_parted_start[$number]=$start - fs_parted_end[$number]=$end - fs_parted_size[$number]=$size - fs_parted_type[$number]=$type - fs_parted_other[$number]=$other - done -} - - -# check for DOS-style MBR on device -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 -} - - -# create swap partition -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 -} - - -# enable swap device -confz_swapon_check() { - local out device rest - - checkvars device - require swap :device - - out=$(swapon -s) || die "$0: swapon -s comman failed" - while read device rest; do - [[ $device == $vars[device] ]] && return 0 - done <<<$out - - do_command=( swapon $vars[device] ) - return 1 -} - - -# set up /dev/md* device -confz_mdraid_check() { - local level out seen_level seen_header devices present - local -a seen_devices devices - local -A level_map - - checkvars md_device raid_devices level - - devices=( ${(z)vars[raid_devices]} ) - - level_map=( - linear linear - raid0 0 - 0 0 - stripe 0 - raid1 1 - 1 1 - mirror 1 - raid4 4 - 4 4 - raid5 5 - 5 5 - raid6 6 - 6 6 - raid10 10 - 10 10 - multipath multipath - mp multipath - faulty faulty - container container - ) - level=${level_map[${vars[level]}]} - - if [[ -b $vars[md_device] ]]; then - out=$( mdadm --detail $vars[md_device] ) || \ - die "$0: mdadm --detail ${(qqq)vars[md_device]} returned $?" - seen_header=0 - while read line; do - if ! (($seen_header)); then - case $line in - ('Raid Level :'*) - seen_level=$level_map[${line#* : }];; - (Number*Major*Minor*RaidDevice*State) - seen_header=1;; - esac - else - seen_devices+=( ${${=line}[-1]} ) - fi - done <<<$out - - [[ $level == $seen_level ]] || \ - die "$0: raid level mismatch." \ - "expected: ${(q)level} got: ${(q)seen_level}" - - [[ ${(o)vars[raid_devices]} == ${(o)seen_devices} ]] || \ - die $0$': raid device mismatch\nexpected:' \ - ${(oqqq)vars[raid_devices]}$'\ngot:' \ - ${(oqqq)seen_devices} - - return 0 - fi - - for device in $devices; do - fs_blkid_probe $device - (( $+present )) || present=$fs_blkid_result - (( $present != $fs_blkid_result )) || \ - die "$0: only some devices present" - if ! (($fs_blkid_result)); then - [[ ${fs_blkid_output[TYPE]:-} == linux_raid_member ]] || \ - die "$0: non-raid label present on device: ${(qqq)device}" - fi - done - - if (($present)); then - # empty labels - do_command=( mdadm --create -l $level -n $#devices $vars[md_device] $devices ) - else - # already created - do_command=( mdadm --assemble $vars[md_device] $devices ) - fi -} - - -# 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 -} - - -# configure LVM2 volume group -confz_volume_group_check() { - local -A devices volumes - local -a vg_devices - local device - - [[ -n ${vars[vg_name]:=${DEFAULT_VG}} ]] || \ - die "$0: DEFAULT_VG is unset and no 'vg_name' was passed" - - checkvars vg_devices - - vg_devices=( ${(z)vars[vg_devices]} ) - for device in $vg_devices; do - require physical_volume device=$device - done - - volumes=( $(lvm vgs --noheadings -o vg_name,pv_count) ) || \ - die "$0: lvm vgs command returned error" - if ! (($+volumes[${vars[vg_name]}])); then - do_command=( lvm vg_create ${vars[vg_name]} $vg_devices ) - return 1 - fi - devices=( $(lvm pvs --noheadings -o pv_name,vg_name) ) || \ - die "$0: lvm pvs command returned error" - do_command=( lvm vgextend ${vars[vg_name]} ) - for device in $vg_devices; do - [[ $+devices[$device] && $devices[$device] == $vars[vg_name] ]] || \ - do_command+=( $device ) - done - (( $#do_command == 3 )) -} - - # configure LVM2 logical volume confz_logical_volume_check() { [[ -n ${vars[vg_name]:=${DEFAULT_VG}} ]] || \