|
| 1 | +#!/bin/bash -eu |
| 2 | + |
| 3 | +. shell-error |
| 4 | +. initrd-sh-functions |
| 5 | +. uevent-sh-functions |
| 6 | +. /.initrd/initenv |
| 7 | + |
| 8 | +PROG="${QUEUE:--}: session=${SESSION:-0}: $PROG" |
| 9 | +message_time=1 |
| 10 | + |
| 11 | +pidfile="/var/run/$PROG.pid" |
| 12 | +tsfile=/.initrd/btrfs-member-timeout |
| 13 | +delay="${BTRFS_MEMBER_DELAY-}" |
| 14 | + |
| 15 | +for e in "$1"/btrfs-member.*; do |
| 16 | + done_event "$e" ||: |
| 17 | +done |
| 18 | + |
| 19 | +[ -n "$delay" ] && [ "$delay" -gt 0 ] || |
| 20 | + exit 0 |
| 21 | + |
| 22 | +now="$(timestamp boottime raw)" |
| 23 | +now="${now%.*}" |
| 24 | +echo "$(($now + $delay))" > "$tsfile" |
| 25 | + |
| 26 | +[ ! -e "$pidfile" ] || |
| 27 | + exit 0 |
| 28 | + |
| 29 | +btrfs_fstab_entries() |
| 30 | +{ |
| 31 | + local fsdev fsdir fstype fsopts freq passno uuid |
| 32 | + |
| 33 | + while read -r fsdev fsdir fstype fsopts freq passno; do |
| 34 | + [ -n "$fsdev" ] || continue |
| 35 | + case "$fstype" in |
| 36 | + btrfs|auto) ;; |
| 37 | + *) continue ;; |
| 38 | + esac |
| 39 | + |
| 40 | + case "$fsdev" in |
| 41 | + UUID=*) |
| 42 | + uuid="${fsdev#UUID=}" |
| 43 | + printf '%s %s\n' "$fsdev" "$uuid" |
| 44 | + ;; |
| 45 | + LABEL=*) |
| 46 | + uuid="$(blkid -l -o value -s UUID -t "$fsdev" 2>/dev/null)" ||: |
| 47 | + [ -z "$uuid" ] || printf '%s %s\n' "$fsdev" "$uuid" |
| 48 | + ;; |
| 49 | + esac |
| 50 | + done < /etc/fstab |
| 51 | +} |
| 52 | + |
| 53 | +check_btrfs_arrays() |
| 54 | +{ |
| 55 | + local fsdev uuid show total missing need_wait= |
| 56 | + |
| 57 | + while read -r fsdev uuid; do |
| 58 | + show="$(btrfs filesystem show "$uuid" 2>/dev/null)" || |
| 59 | + continue |
| 60 | + |
| 61 | + total="$(printf '%s\n' "$show" | |
| 62 | + sed -n 's/.*Total devices \([0-9]\+\).*/\1/p')" |
| 63 | + |
| 64 | + [ -n "$total" ] && [ "$total" -gt 1 ] || |
| 65 | + continue |
| 66 | + |
| 67 | + missing="$(printf '%s\n' "$show" | grep -c 'missing' ||:)" |
| 68 | + |
| 69 | + if [ "${missing:-0}" -gt 0 ]; then |
| 70 | + need_wait=1 |
| 71 | + fi |
| 72 | + done < <(btrfs_fstab_entries | sort -u -k2,2) |
| 73 | + |
| 74 | + [ -n "$need_wait" ] |
| 75 | +} |
| 76 | + |
| 77 | +# No multi-device btrfs in fstab, or all devices already present. |
| 78 | +check_btrfs_arrays || |
| 79 | + exit 0 |
| 80 | + |
| 81 | +( |
| 82 | + while :; do |
| 83 | + now="$(timestamp boottime raw)" |
| 84 | + now="${now%.*}" |
| 85 | + |
| 86 | + ts= |
| 87 | + readline ts "$tsfile" |
| 88 | + |
| 89 | + [ "$ts" -ge "$now" ] || |
| 90 | + break |
| 91 | + sleep 1 |
| 92 | + done |
| 93 | + |
| 94 | + # Run a full device scan to resolve any per-device scan race conditions. |
| 95 | + btrfs device scan ||: |
| 96 | + |
| 97 | + changed= |
| 98 | + while read -r fsdev uuid; do |
| 99 | + show="$(btrfs filesystem show "$uuid" 2>/dev/null)" || |
| 100 | + continue |
| 101 | + |
| 102 | + total="$(printf '%s\n' "$show" | |
| 103 | + sed -n 's/.*Total devices \([0-9]\+\).*/\1/p')" |
| 104 | + |
| 105 | + [ -n "$total" ] && [ "$total" -gt 1 ] || |
| 106 | + continue |
| 107 | + |
| 108 | + missing="$(printf '%s\n' "$show" | grep -c 'missing' ||:)" |
| 109 | + |
| 110 | + if [ "${missing:-0}" -gt 0 ]; then |
| 111 | + message "btrfs $uuid: $missing of $total device(s) missing, adding degraded mount option" |
| 112 | + |
| 113 | + # shellcheck disable=SC2016 |
| 114 | + fsdev_escaped="$(printf '%s\n' "$fsdev" | sed 's/[[\.*^$()+?{|]/\\&/g')" |
| 115 | + if ! grep -qE "^${fsdev_escaped}[[:space:]].*degraded" /etc/fstab; then |
| 116 | + sed -i "/^${fsdev_escaped}[[:space:]]/s/\(auto\|btrfs\)\([[:space:]]\+\)/btrfs\2degraded,/" /etc/fstab |
| 117 | + changed=1 |
| 118 | + fi |
| 119 | + else |
| 120 | + message "btrfs $uuid: all $total devices present" |
| 121 | + fi |
| 122 | + done < <(btrfs_fstab_entries | sort -u -k2,2) |
| 123 | + |
| 124 | + [ -z "$changed" ] || |
| 125 | + rootdelay_reset_timer |
| 126 | +) & |
| 127 | + |
| 128 | +echo "$!" > "$pidfile" |
0 commit comments