aboutsummaryrefslogtreecommitdiff
path: root/rc.lib
blob: e70d7e0cd48a5c88861712ec1d4aaa0721dd0623 (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
#
# rc.lib
#

# vim:filetype=sh
# shellcheck disable=1090

# Functions for rc.boot and rc.shutdown

out() { printf '\033[1;36m-> \033[m\033[1m%s\033[m\n' "$@" ;}
error() { printf '\033[1;31m!> ERROR: \033[m\033[1m%s\033[m\n' "$@" >&2 ;}
welcome() { printf '\033[1;36m->\033[m\033[1m Welcome to\033[35m %s\033[m\033[1m!\n' "Carbs $(uname -sr)" ;}

mnt() {
    mountpoint -q "$1" || {
        dir=$1
        shift
        mount "$@" "$dir"
    }
}

emergency_shell() {
    error "Cannot continue init due to errors above, starting emergency shell" \
    "When ready, type 'exit' to continue the boot."

    /bin/sh -l
}

parse_crypttab() {
    # This is a pure shell crypttab parser which supports
    # a subset of the format.

    # Function by Dylan Araps
    exec 3<&0

    # shellcheck disable=2086
    while read -r name dev pass opts err; do
        # Skip comments.
        [ "${name##\#*}" ] || continue

        # Break on invalid crypttab (> 5 columns).
        [ "$err" ] && break

        # Turn 'UUID=*' lines into device names.
        [ "${dev##UUID*}" ] || dev=$(blkid -l -o device -t "$dev")

        # Parse options by turning the list into a pseudo array.
        { old_ifs=$IFS; IFS=,; set -f; set +f -- $opts; IFS=$old_ifs; }

        # Create an argument list (no other way to do this in sh).
        for opt; do case $opt in
            discard)            copts="$copts --allow-discards" ;;
            readonly|read-only) copts="$copts -r" ;;
            tries=*)            copts="$copts -T ${opt##*=}" ;;
        esac; done

        # If password is 'none', '-' or empty ask for it.
        case $pass in
            none|-|"") cryptsetup luksOpen $copts "$dev" "$name" <&3 ;;
            *)         cryptsetup luksOpen $copts -d "$pass" "$dev" "$name" ;;
        esac
    done < /etc/crypttab

    exec 3>&-

    [ "$copts" ] && [ -x /bin/vgchange ] && {
        out "Activating LVM devices for dm-crypt..."
        vgchange --sysinit -a y || emergency_shell
    }
}