hardware.sh 2.34 KB
Newer Older
1
2
3
#!/bin/sh

get_all_ethernet_nics() {
4
5
6
7
8
9
10
    for i in /sys/class/net/*; do
        # type = 1 means ethernet (ARPHDR_ETHER, see Linux' sources,
        # beginning of include/linux/if_arp.h)
        if [ "$(cat "${i}"/type)" = 1 ]; then
            basename "${i}"
        fi
    done
11
12
}

13
nic_exists() {
14
    [ -e /sys/class/net/"${1}" ]
15
16
}

17
nic_is_up() {
18
    [ "$(cat /sys/class/net/"${1}"/operstate)" = "up" ]
19
}
20
21
22

# The following "nic"-related functions require that the argument is a
# NIC that exists
23
24

# Will just output nothing on failure
25
get_current_mac_of_nic() {
26
27
28
29
30
    local mac
    mac="$(macchanger "${1}" | sed -n "s/^Current\s*MAC:\s*\([0-9a-f:]\+\)\s.*$/\1/p" || :)"
    if echo "${mac}:" | grep -q "^\([0-9a-fA-F]\{2\}:\)\{6\}$"; then
        echo "${mac}"
    fi
31
32
33
34
35
36
37
38
39
40
41
42
43
}

get_module_used_by_nic() {
    basename "$(readlink "/sys/class/net/${1}/device/driver/module")"
}

get_name_of_nic() {
  vendor=$(sed 's/^0x\(.*\)$/\1/' "/sys/class/net/${1}/device/vendor")
  device=$(sed 's/^0x\(.*\)$/\1/' "/sys/class/net/${1}/device/device")
  lspci -nn | sed -n "s/^\S\+\s\+[^:]\+:\s\+\(.*\)\s\+\[$vendor:$device\].*$/\1/p"
}

# Auxillary function for mod_rev_dep(). It recurses over the graph of
Tails developers's avatar
Tails developers committed
44
45
46
47
# kernel module depencies of $@ (note that it only works for loaded
# modules). To deal with circular dependencies a global variable
# MOD_REV_DEP_VISITED keeps track of already visited nodes, and it
# should be unset before the first call of this function.
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
mod_rev_dep_aux() {
  local mod
  local rev_deps
  for mod in ${@}; do
    if echo ${MOD_REV_DEP_VISITED} | grep -qw ${mod}; then
        continue
    fi
    MOD_REV_DEP_VISITED="${MOD_REV_DEP_VISITED} ${mod}"
    # extract the "Used by" column for $mod from lsmod
    rev_deps=$(lsmod | \
               sed -n "s/^${mod}\s\+\S\+\s\+\S\+\s\+\(\S\+\)/\1/p" | \
               tr ',' ' ')
    mod_rev_dep_aux ${rev_deps}
    echo ${mod}
  done
}

Tails developers's avatar
Tails developers committed
65
# Prints a list of all loaded modules depending on $1, including $1. It's
66
# ordered by descending "maximum dependency distance" from $1, so the
67
# output is ideal if we want to unload $1 and (by necessity) all
68
69
70
71
72
73
74
75
76
# modules that uses $1.
mod_rev_dep() {
  MOD_REV_DEP_VISITED=""
  mod_rev_dep_aux ${1}
}

# Unloads module $1, and all modules that (transatively) depends on
# $1 (i.e. its reverse dependencies).
unload_module_and_rev_deps() {
77
78
79
  for mod in $(mod_rev_dep ${1}); do
      /sbin/rmmod ${mod}
  done
80
}