summaryrefslogtreecommitdiff
path: root/initramfs/script
blob: e98a6239459359c3b925521ed133f3ea7017cefc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh

PREREQ=""

prereqs()
{
    echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
    prereqs
    exit 0
    ;;
esac

# Nothing to do if ROOTFSTYPE is set to something other than bcachefs
if [ -n "$ROOTFSTYPE" -a "$ROOTFSTYPE" != bcachefs ]; then
    exit 0
fi

# source for resolve_device() and panic() functions
. /scripts/functions

#
# Helper functions
#
message()
{
    if [ -x /bin/plymouth ] && plymouth --ping; then
        plymouth message --text="$*"
    else
        echo "$*" >&2
    fi
}

panic2()
{
    # Send the panic message to plymouth
    if [ -x /bin/plymouth ] && plymouth --ping; then
        plymouth message --text="$*"
    fi
    panic "$@"
    exit 1
}

unlock()
{
    local msg=$1
    shift

    if [ -x /bin/plymouth ] && plymouth --ping; then
        msg=$(plymouth ask-for-password --prompt="$msg" | \
              bcachefs unlock "$@" 2>&1)
        # If the unlock failed, send any printed messages to plymouth
        if [ $? -ne 0 ]; then
            plymouth message --text="Bcachefs: $msg"
            return 1
        fi
    else
        # If unlock() is called multiple times, don't re-print the prompt message
        # unless it has changed
        if [ "$LAST_UNLOCK_MSG" != "$msg" ]; then
            echo "$msg" >&2
            LAST_UNLOCK_MSG=$msg
        fi
        bcachefs unlock "$@"
    fi
}

# Resolve the root device (e.g. if root is specified by UUID)
DEV=$(resolve_device "$ROOT")

# Check if the root device needs unlocking:
if bcachefs unlock -c $DEV >/dev/null 2>&1; then
    if [ "$DEV" == "$ROOT" ]; then
        msg="Please unlock $DEV:"
    else
        msg="Please unlock $DEV ($ROOT):"
    fi

    count=0
    tries=3
    while [ $tries -le 0 -o $count -lt $tries ]; do
        if unlock "$msg" "$DEV"; then
            message "Bcachefs: $DEV successfully unlocked"
            break
        fi

        let count++
    done

    if [ $tries -gt 0 -a $count -ge $tries ]; then
        panic2 "Bcachefs: maximum number of tries exceeded for $DEV"
    fi
fi

exit 0