]> git.sesse.net Git - bcachefs-tools-debian/blob - initramfs/script
Depend on python3:any due to bcachefsck_all being a Python script (Lintian warning).
[bcachefs-tools-debian] / initramfs / script
1 #!/bin/sh
2
3 PREREQ=""
4
5 prereqs()
6 {
7     echo "$PREREQ"
8 }
9
10 case $1 in
11 # get pre-requisites
12 prereqs)
13     prereqs
14     exit 0
15     ;;
16 esac
17
18 # Nothing to do if ROOTFSTYPE is set to something other than bcachefs
19 if [ -n "$ROOTFSTYPE" -a "$ROOTFSTYPE" != bcachefs ]; then
20     exit 0
21 fi
22
23 # source for resolve_device() and panic() functions
24 . /scripts/functions
25
26 #
27 # Helper functions
28 #
29 message()
30 {
31     if [ -x /bin/plymouth ] && plymouth --ping; then
32         plymouth message --text="$*"
33     else
34         echo "$*" >&2
35     fi
36 }
37
38 panic2()
39 {
40     # Send the panic message to plymouth
41     if [ -x /bin/plymouth ] && plymouth --ping; then
42         plymouth message --text="$*"
43     fi
44     panic "$@"
45     exit 1
46 }
47
48 unlock()
49 {
50     local msg=$1
51     shift
52
53     if [ -x /bin/plymouth ] && plymouth --ping; then
54         msg=$(plymouth ask-for-password --prompt="$msg" | \
55               bcachefs unlock "$@" 2>&1)
56         # If the unlock failed, send any printed messages to plymouth
57         if [ $? -ne 0 ]; then
58             plymouth message --text="Bcachefs: $msg"
59             return 1
60         fi
61     else
62         # If unlock() is called multiple times, don't re-print the prompt message
63         # unless it has changed
64         if [ "$LAST_UNLOCK_MSG" != "$msg" ]; then
65             echo "$msg" >&2
66             LAST_UNLOCK_MSG=$msg
67         fi
68         bcachefs unlock "$@"
69     fi
70 }
71
72 # Resolve the root device (e.g. if root is specified by UUID)
73 DEV=$(resolve_device "$ROOT")
74
75 # Check if the root device needs unlocking:
76 if bcachefs unlock -c $DEV >/dev/null 2>&1; then
77     if [ "$DEV" == "$ROOT" ]; then
78         msg="Please unlock $DEV:"
79     else
80         msg="Please unlock $DEV ($ROOT):"
81     fi
82
83     count=0
84     tries=3
85     while [ $tries -le 0 -o $count -lt $tries ]; do
86         if unlock "$msg" "$DEV"; then
87             message "Bcachefs: $DEV successfully unlocked"
88             break
89         fi
90
91         let count++
92     done
93
94     if [ $tries -gt 0 -a $count -ge $tries ]; then
95         panic2 "Bcachefs: maximum number of tries exceeded for $DEV"
96     fi
97 fi
98
99 exit 0