Skip to content

Commit f94cdc7

Browse files
committed
Merge branch 'udev' into development
2 parents d559ec5 + 8a5597c commit f94cdc7

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

‎system/cuos/init.sh‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,101 @@ configure_network() {
424424

425425
}
426426

427+
configure_udev() {
428+
local rules_file="${T_FILE_UDEV_RULES:-/etc/udev/rules.d/99-cuos.rules}"
429+
local rules_file_new="${T_FILE_UDEV_RULES_NEW:-"${rules_file}.new"}"
430+
431+
local count
432+
count="$(jq_config '.udev | length')"
433+
434+
{
435+
echo "# Managed automatically"
436+
echo
437+
438+
for ((i=0; i<count; i++)); do
439+
local entry
440+
entry="$(jq_config -c ".udev[$i]")"
441+
442+
# --- unified field extraction (old + new schema) ---
443+
local subsystem vendor product serial mac symlink
444+
445+
subsystem="$(echo "$entry" | jq -r '.subsystem // .match.subsystem // empty')"
446+
symlink="$(echo "$entry" | jq -r '.symlink // empty')"
447+
448+
vendor="$(echo "$entry" | jq -r '.vendor // .match.vendor // empty')"
449+
product="$(echo "$entry" | jq -r '.product // .match.product // empty')"
450+
serial="$(echo "$entry" | jq -r '.serial // .match.serial // empty')"
451+
mac="$(echo "$entry" | jq -r '."mac-address" // .match."mac-address" // empty' | tr '[:upper:]' '[:lower:]')"
452+
453+
mode="$(echo "$entry" | jq -r '.mode // empty')"
454+
tag="$(echo "$entry" | jq -r '.tag // empty')"
455+
456+
# skip invalid entries
457+
[[ -z "$subsystem" || -z "$symlink" ]] && continue
458+
459+
local rule=""
460+
rule+="SUBSYSTEM==\"${subsystem}\""
461+
462+
case "$subsystem" in
463+
tty)
464+
[[ -n "$serial" ]] && rule+=", ATTRS{serial}==\"${serial}\""
465+
[[ -n "$vendor" ]] && rule+=", ATTRS{idVendor}==\"${vendor}\""
466+
[[ -n "$product" ]] && rule+=", ATTRS{idProduct}==\"${product}\""
467+
;;
468+
469+
hidraw)
470+
[[ -n "$vendor" ]] && rule+=", ATTRS{idVendor}==\"${vendor}\""
471+
[[ -n "$product" ]] && rule+=", ATTRS{idProduct}==\"${product}\""
472+
[[ -n "$serial" ]] && rule+=", ATTRS{serial}==\"${serial}\""
473+
;;
474+
475+
sound)
476+
[[ -n "$vendor" ]] && rule+=", ATTRS{idVendor}==\"${vendor}\""
477+
[[ -n "$product" ]] && rule+=", ATTRS{idProduct}==\"${product}\""
478+
;;
479+
480+
block)
481+
[[ -n "$serial" ]] && rule+=", ATTRS{serial}==\"${serial}\""
482+
[[ -n "$vendor" ]] && rule+=", ATTRS{idVendor}==\"${vendor}\""
483+
[[ -n "$product" ]] && rule+=", ATTRS{idProduct}==\"${product}\""
484+
;;
485+
486+
net)
487+
[[ -n "$mac" ]] && rule+=", ATTR{address}==\"${mac}\""
488+
;;
489+
esac
490+
491+
[[ -n "$mode" ]] && rule+=", MODE=\"${mode}\""
492+
[[ -n "$tag" ]] && rule+=", TAG+=\"${tag}\""
493+
494+
rule+=", SYMLINK+=\"${symlink}\""
495+
496+
echo "$rule"
497+
done
498+
} > "${rules_file_new}"
499+
500+
# --- atomic replace ---
501+
if [[ -n "${TEST:-}" ]]; then
502+
return
503+
fi
504+
505+
local changed=1
506+
507+
if [[ -f "${rules_file}" ]]; then
508+
if [[ "$(sha256sum "${rules_file}" | cut -d ' ' -f1)" == \
509+
"$(sha256sum "${rules_file_new}" | cut -d ' ' -f1)" ]]; then
510+
changed=0
511+
fi
512+
fi
513+
514+
mv "${rules_file_new}" "${rules_file}"
515+
516+
if (( changed )); then
517+
udevadm control --reload-rules
518+
udevadm trigger
519+
fi
520+
}
521+
427522
configure_keyboard() {
428523
# For lxc we do not configure the keyboard
429524
if [[ "${VIRT_TYPE}" == "lxc" || "${VIRT_TYPE}" == "docker" ]]; then
@@ -762,6 +857,8 @@ main() {
762857

763858
ensure_system_config
764859

860+
configure_udev
861+
765862
set_hostname
766863

767864
configure_network

‎system/cuos/init.test.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,14 @@ expect \
132132
$'auto lo\niface lo inet loopback\n\nauto eth0\niface eth0 inet dhcp\n metric 100\n\niface eth1 inet manual' \
133133
configure_network
134134

135+
136+
export T_FILE_UDEV_RULES_NEW=/dev/stdout
137+
export T_FILE_UDEV_RULES=/dev/stdout
138+
139+
export CONFIG_PATH="${SCRIPT_DIR}/init.test.system6.json"
140+
expect \
141+
"configure_udev: no config found" \
142+
$'# Managed automatically\n\nSUBSYSTEM=="tty", ATTRS{serial}=="A50285BI", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", MODE="0660", TAG+="iot", SYMLINK+="zigbee"\nSUBSYSTEM=="sound", ATTRS{idVendor}=="0499", ATTRS{idProduct}=="1503", MODE="0660", TAG+="audio", SYMLINK+="midi-keyboard"\nSUBSYSTEM=="hidraw", ATTRS{idVendor}=="046d", ATTRS{idProduct}=="c216", MODE="0660", TAG+="input", SYMLINK+="gamepad"\nSUBSYSTEM=="block", ATTRS{serial}=="4C530001230101118392", MODE="0660", TAG+="storage", SYMLINK+="backup-disk"\nSUBSYSTEM=="net", ATTR{address}=="02:11:22:33:44:55", MODE="0660", SYMLINK+="usbip-nic"' \
143+
configure_udev
144+
135145
summary

‎system/cuos/init.test.system6.json‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"udev": [
3+
{
4+
"name": "zigbee-serial",
5+
"subsystem": "tty",
6+
"vendor": "1a86",
7+
"product": "7523",
8+
"serial": "A50285BI",
9+
"symlink": "zigbee",
10+
"mode": "0660",
11+
"tag": "iot"
12+
},
13+
{
14+
"name": "usb-midi",
15+
"subsystem": "sound",
16+
"vendor": "0499",
17+
"product": "1503",
18+
"symlink": "midi-keyboard",
19+
"mode": "0660",
20+
"tag": "audio"
21+
},
22+
{
23+
"name": "usb-hid-gamepad",
24+
"subsystem": "hidraw",
25+
"vendor": "046d",
26+
"product": "c216",
27+
"symlink": "gamepad",
28+
"mode": "0660",
29+
"tag": "input"
30+
},
31+
{
32+
"name": "usb-storage",
33+
"subsystem": "block",
34+
"serial": "4C530001230101118392",
35+
"symlink": "backup-disk",
36+
"mode": "0660",
37+
"tag": "storage"
38+
},
39+
{
40+
"name": "usbip-nic",
41+
"subsystem": "net",
42+
"vendor": "0bda",
43+
"product": "8153",
44+
"mac-address": "02:11:22:33:44:55",
45+
"symlink": "usbip-nic",
46+
"mode": "0660"
47+
}
48+
]
49+
}

0 commit comments

Comments
 (0)