summarylogtreecommitdiffstats
path: root/initcpio-install-udev
blob: 713fbf4cc7eadc0ea65d003ed377994f43a5d5fa (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
#!/bin/bash

# This is in 'udev' and 'systemd' hook... Let's hope we have
# it in mkinitcpio soon.
# https://github.com/archlinux/mkinitcpio/pull/54
add_udev_rule() {
    # Add an udev rules file to the initcpio image. Dependencies on binaries
    # will be discovered and added.
    #   $1: path to rules file (or name of rules file)

    local rules="$1" rule= key= value= binary=

    if [[ ${rules:0:1} != '/' ]]; then
        rules=$(PATH=/usr/lib/udev/rules.d:/lib/udev/rules.d type -P "$1")
    fi
    if [[ -z $rules ]]; then
        # complain about not found rules
        return 1
    fi

    add_file "$rules" /usr/lib/udev/rules.d/"${rules##*/}"

    while IFS=, read -ra rule; do
        # skip empty lines, comments
        [[ -z $rule || $rule = @(+([[:space:]])|#*) ]] && continue

        for pair in "${rule[@]}"; do
            IFS=' =' read -r key value <<< "$pair"
            case $key in
                RUN@({program}|+)|IMPORT{program}|ENV{REMOVE_CMD})
                    # strip quotes
                    binary=${value//[\"\']/}
                    # just take the first word as the binary name
                    binary=${binary%% *}
                    [[ ${binary:0:1} == '$' ]] && continue
                    if [[ ${binary:0:1} != '/' ]]; then
                        binary=$(PATH=/usr/lib/udev:/lib/udev type -P "$binary")
                    fi
                    add_binary "$binary"
                    ;;
            esac
        done
    done <"$rules"
}

build() {
    local rules tool

    map add_binary \
        '/usr/lib/systemd/systemd-udevd' \
        '/usr/bin/udevadm' \
        '/usr/bin/systemd-tmpfiles'

    map add_udev_rule \
        '50-udev-default.rules' \
        '60-persistent-storage.rules' \
        '64-btrfs.rules' \
        '80-drivers.rules'
    
    map add_file \
        '/usr/lib/udev/ata_id' \
        '/usr/lib/udev/scsi_id'

    add_runscript
}

help() {
    cat <<HELPEOF
This hook adds the udev daemon to the initramfs, allowing for dynamic loading
of modules and reliable detection of the root device via tags (e.g. UUID or
LABEL). Do not remove this hook unless you are using the systemd hook, or you
know what you're doing.
HELPEOF
}

# vim: set ft=sh ts=4 sw=4 et: