aboutsummaryrefslogtreecommitdiff
path: root/rc.boot
blob: a86a29fc6d72a21a2e240834598900ffa3ee1af4 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/sh

. PREFIX/lib/init/rc.conf

welcome() { printf '\033[1;36m=>\033[m\033[1m Welcome to\033[35m Carbs Linux\033[m\033[1m!\n' ;}

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
}

main() {
    PATH=/usr/bin:/usr/sbin
    old_ifs=$IFS
    set -f

    welcome

    log "Mounting pseudo filesystems..."; {
        mnt /proc -o nosuid,noexec,nodev    -t proc     proc
        mnt /sys  -o nosuid,noexec,nodev    -t sysfs    sys
        mnt /run  -o mode=0755,nosuid,nodev -t tmpfs    run
        mnt /dev  -o mode=0755,nosuid       -t devtmpfs dev

        mkdir -pm 0755 /run/runit \
                       /run/lvm   \
                       /run/user  \
                       /run/lock  \
                       /run/log   \
                       /dev/pts   \
                       /dev/shm

        mnt /dev/pts -o mode=0620,gid=5,nosuid,noexec -nt devpts     devpts
        mnt /dev/shm -o mode=1777,nosuid,nodev        -nt tmpfs      shm
    }

    log "Setting dmesg level..."; {
	 [ -n "$dmesg_level" ] && dmesg -n$dmesg_level
    }

    log "Starting eudev..."; {
        command -v udevd >/dev/null && {
            udevd --daemon
            udevadm trigger --action=add --type=subsystems
            udevadm trigger --action=add --type=devices
            udevadm settle
        }
    }

    log "Remounting rootfs as ro..."; {
        mount -o remount,ro / || emergency_shell
    }

    log "Activating encrypted devices (if any exist)..."; {
        [ -e /etc/crypttab ] && [ -x /bin/cryptsetup ] && {
            exec 3<&0

            while read -r name dev pass opts err; do
                [ "${name##\#*}" ] || continue

                # Break on invalid crypttab.
                [ "$err" ] && {
                    printf 'error: A valid crypttab has only 4 columns.\n'
                    break
                }

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

                # Parse options by turning list into a pseudo array.
                IFS=,
                set -- $opts
                IFS=$old_ifs

                copts="cryptsetup luksOpen"

                # 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|-|"") $copts "$dev" "$name" <&3 ;;
                    *)         $copts -d "$pass" "$dev" "$name" ;;
                esac
            done < /etc/crypttab

            exec 3>&-

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

    log "Checking filesystems..."; {
        fsck -ATat noopts=_netdev
        [ $? -gt 1 ] && emergency_shell
    }

    log "Mounting rootfs rw..."; {
        mount -o remount,rw / || emergency_shell
    }

    log "Mounting all local filesystems..."; {
        mount -at nosysfs,nonfs,nonfs4,nosmbfs,nocifs -O no_netdev ||
            emergency_shell
    }

    log "Enabling swap..."; {
        swapon -a || emergency_shell
    }

    log "Seeding random..."; {
        if [ -f /var/random.seed ]; then
            cat /var/random.seed > /dev/urandom
        else
            log "This may hang."
            log "Mash the keyboard to generate entropy..."

            dd count=1 bs=512 if=/dev/random of=/var/random.seed
        fi
    }

    log "Setting up loopback..."; {
        ip link set up dev lo
    }

    log "Setting hostname..."; {
        read -r hostname < /etc/hostname
        printf '%s\n' "${hostname:-carbs-linux}" > /proc/sys/kernel/hostname
    } 2>/dev/null

    log "Getting keymap settings..."; {
	[ -n "$keymap" ] && loadkmap < "$keymap"
    }

    log "Loading sysctl settings..."; {
        find /run/sysctl.d \
             /etc/sysctl.d \
             /usr/local/lib/sysctl.d \
             /usr/lib/sysctl.d \
             /lib/sysctl.d \
             /etc/sysctl.conf \
             -name \*.conf -type f 2>/dev/null \
        | while read -r conf; do
            seen="$seen ${conf##*/}"

            case $seen in
                *" ${conf##*/} "*) ;;
                *) printf '%s\n' "* Applying $conf ..."
                   sysctl -p "$conf" ;;
            esac
        done
    }

    command -v udevd >/dev/null &&
        udevadm control --exit

    log "Boot stage complete..."
}

main