Skip to content

Instantly share code, notes, and snippets.

@polachok
Created July 15, 2012 14:52
Show Gist options
  • Save polachok/3117279 to your computer and use it in GitHub Desktop.
Save polachok/3117279 to your computer and use it in GitHub Desktop.
hotplugd attach script
#!/usr/bin/awk -f
function rstrip(str) {
gsub(/ *$/, "", str)
return str
}
function lstrip(str) {
gsub(/^ */, "", str)
return str
}
function disk(dev) {
stderr = "/dev/stderr"
uid = 1000
cmd = sprintf("/sbin/disklabel %s", dev)
while (cmd | getline) {
FS=": "
if ($1 == "label") {
label = rstrip($2)
}
if ($1 == "duid") {
duid = $2
}
if ($1 ~ /^ [a-z]/) {
# partition
p = lstrip($1)
if (p == c)
continue
$0 = $2
FS=" "
parts[p] = $3
printf("part %s type %s\n", p, parts[p])
}
}
printf("disk %s label: %s, duid: %s\n", dev, label, duid)
if (duid == 0) { # dos partition table
for (p in parts) {
mountpoint = sprintf("/mnt/usb/%s", label)
cmd = sprintf("mkdir -p \"%s\"", mountpoint)
if (system(cmd) != 0)
mountpoint = "/mnt/usb/"
if (parts[p] == "MSDOS") {
mount = sprintf("mount_msdos -u %s", uid)
} else if (parts[p] == "4.2BSD") {
mount = "mount"
} else
return
cmd = sprintf("%s /dev/%s%c \"%s\"",
mount, dev, p,
mountpoint)
if (system(cmd) != 0)
printf("mount failed\n") >> stderr
}
} else {
for (p in parts) {
if (parts[p] == "MSDOS") {
mount = sprintf("mount_msdos -u %s", uid)
} else if (parts[p] == "4.2BSD") {
mount = "mount"
} else
return
cmd = sprintf("fsck %s.%c", duid, p)
if (((r = system(cmd)) != 0))
printf("fsck failed\n") >> stderr
cmd = sprintf("%s %s.%c", mount, duid, p)
if (system(cmd) != 0)
printf("mount failed\n") >> stderr
}
}
}
function netif(dev) {
printf("netif %s, doing nothing", dev)
}
BEGIN {
devclass = ARGV[1]
devname = ARGV[2]
if (devclass == 2) # disk
disk(devname)
else if (devclass == 3) # netif
netif(devname)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment