Repository navigation
Expand file tree
/
Copy pathmachine-id.init
More file actions
executable file
·176 lines (154 loc) · 5.17 KB
/
Copy pathmachine-id.init
File metadata and controls
executable file
·176 lines (154 loc) · 5.17 KB
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
#!/bin/sh
### BEGIN INIT INFO
# Provides: machine-id
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Should-Start:
# X-Start-Before: dbus
# Default-Start: 2 3 4 5
# Default-Stop: 0 6
# Short-Description: Handle flexible machine-id modes and session cleanup
# Description: Controls volatile or static machine-id layout variations
# and purges accumulated session bus files on shutdown.
### END INIT INFO
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=machine-id
DESC="Flexible machine-id and session management"
# Source the standard Debian/Devuan LSB logging utility library
. /lib/lsb/init-functions
VOLATILE_MACHINE_ID=""
DRYRUN=""
USE_MAC_FALLBACK=""
CONFIG="/etc/default/${NAME}.rc"
# Source configuration overrides if the file exists
if [ -f "$CONFIG" ]; then
. "$CONFIG"
fi
EtcMachineID=/etc/machine-id
RunMachineID=/run/machine-id
BaseDbusSessDir=".dbus/session-bus"
clean_dir() {
local target_dir="$1"
if [ -d "$target_dir" ] && [ "$(echo "$target_dir"/*)" != "$target_dir/*" ]; then
if [ "$DRYRUN" = "yes" ]; then
for item in "$target_dir"/*; do
log_action_msg "Dry-run: Would remove $item"
done
else
rm -f -- "$target_dir"/*
fi
fi
}
fallback_machine_id_gen() {
local mac_hex=""
local needed_chars=32
local machine_id_file="$1"
local BaseNetDir="/sys/class/net"
local net_dir
local raw_mac
# Only attempt MAC acquisition if explicitly enabled by the administrator
if [ "$USE_MAC_FALLBACK" = "yes" ]; then
for net_dir in "$BaseNetDir"/*; do
if [ -d "$net_dir" ] && [ "${net_dir##*/}" != "lo" ] && [ -f "$net_dir/address" ]; then
read -r raw_mac < "$net_dir/address"
mac_hex=$(printf '%s' "$raw_mac" | tr -d ':' | tr '[:upper:]' '[:lower:]')
if [ ${#mac_hex} -eq 12 ] && [ "$mac_hex" != "000000000000" ]; then
break
fi
mac_hex=""
fi
done
fi
# Calculate remainder space (defaults to 32 if MAC loop was bypassed or empty)
needed_chars=$((32 - ${#mac_hex}))
# Write out any discovered MAC slice first, then append random entropy
printf "%s" "$mac_hex" > "$machine_id_file"
tr -dc 'a-f0-8' < /dev/urandom 2>/dev/null | head -c "$needed_chars" >> "$machine_id_file"
printf "\n" >> "$machine_id_file"
}
machine_id_gen() {
local target_file="$1"
if command -v dbus-uuidgen >/dev/null 2>&1; then
dbus-uuidgen --ensure="$target_file"
else
fallback_machine_id_gen "$target_file"
fi
}
do_start() {
if [ -n "$VOLATILE_MACHINE_ID" ] && [ "$VOLATILE_MACHINE_ID" != "no" ]; then
log_daemon_msg "Setting up volatile machine-id" "$NAME"
if [ ! -L "$EtcMachineID" ] || [ "$(readlink "$EtcMachineID")" != "$RunMachineID" ]; then
rm -f "$EtcMachineID"
ln -sf "$RunMachineID" "$EtcMachineID"
fi
if [ ! -s "$RunMachineID" ]; then
machine_id_gen "$RunMachineID"
fi
log_end_msg 0
else
log_daemon_msg "Checking persistent static machine-id" "$NAME"
# If it was a volatile symlink, safely capture its contents to disk before destroying it
if [ -L "$EtcMachineID" ]; then
if [ -s "$RunMachineID" ]; then
local temp_id
temp_id=$(cat "$RunMachineID")
rm -f "$EtcMachineID" "$RunMachineID"
printf "%s\n" "$temp_id" > "$EtcMachineID"
else
rm -f "$EtcMachineID"
fi
fi
# Generate a standard static ID ONLY if the static file is genuinely missing or empty
if [ ! -f "$EtcMachineID" ] || [ ! -s "$EtcMachineID" ]; then
rm -f "$EtcMachineID"
machine_id_gen "$EtcMachineID"
fi
log_end_msg 0
fi
}
do_stop() {
PowerState="/sys/power/state"
PowerPMStatus="/sys/power/pm_status"
if [ -f "$PowerState" ]; then
if [ -f "$PowerPMStatus" ] && grep -q -E "freeze|suspend|hibernate" "$PowerPMStatus" 2>/dev/null; then
return 0
fi
fi
log_daemon_msg "Removing stale session-bus files" "$NAME"
clean_dir "/root/${BaseDbusSessDir}"
getent passwd | cut -d: -f6 | while read -r userhome; do
if [ -z "$userhome" ] || [ "$userhome" = "/" ]; then
continue
fi
clean_dir "$userhome/${BaseDbusSessDir}"
done
log_end_msg 0
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart|force-reload)
do_stop
do_start
;;
status)
if [ -L "$EtcMachineID" ]; then
log_success_msg "Machine-ID Mode: Volatile RAM (Value: $(cat "$EtcMachineID"))"
exit 0
elif [ -s "$EtcMachineID" ]; then
log_success_msg "Machine-ID Mode: Persistent Static (Value: $(cat "$EtcMachineID"))"
exit 0
else
log_failure_msg "Machine-ID file missing or uninitialized."
exit 3
fi
;;
*)
echo "Usage: $NAME {start|stop|restart|force-reload|status}" >&2
exit 3
;;
esac