openbsds-hotplugd-rocks.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <!DOCTYPE html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><meta name="keywords" content="GNU, Emacs, Libre Software, Hurd, Guile, Guix" /><meta name="description" content="GNUcode.me is a website focusing on libre software projects, especially the GNU project." /><link type="application/atom+xml" rel="alternate" title="GNUcode.me -- Feed" href="/feed.xml" /><a rel="me" href="https://fosstodon.org/@thegnuguy"></a><link type="text/css" href="css/footer.min.css" rel="stylesheet"></link><link type="text/css" href="css/header.min.css" rel="stylesheet"></link><link type="text/css" href="css/main.min.css" rel="stylesheet"></link><title>OpenBSD's hotplugd rocks! — GNUcode.me</title></head><body><header><nav><ul><li><a href="index.html">GNUcode.me</a></li><li><a href="services.html">Services</a></li><li><a href="about.html">About</a></li><li><a href="business-ideas.html">Business-ideas</a></li></ul></nav></header><h1>OpenBSD's hotplugd rocks!</h1><main><section class="basic-section-padding"><article><h3>by Joshua Branson — April 13, 2023</h3><div><p>My last post talked about how I broke my OpenBSD laptop by telling OpenBSD that
  2. my usbstick was essential to the boot process, and then, when I booted the
  3. laptop, I did not have that usb stick mounted. That caused some problems. I
  4. since learned that the preferred way of automounting a usb stick under OpenBSD
  5. is with <code>hotplugd</code>.</p><p><a href="https://man.openbsd.org/hotplugd">hotplugd</a> is OpenBSD’s automounting functionality, and it’s actually super simple
  6. and easy. Just put your scripts at <code>/etc/hotplugd/attach</code> and
  7. <code>/etc/hotplugd/detach</code>. And the man page gives you an example shell script, but
  8. since I am not a big fan of <code>sh</code> (its syntax is confusing), I decided to write
  9. my attach script in <a href="https://www.gnu.org/software/guile/">GNU Guile</a>. Writing that script made me want to write more
  10. scripts in <a href="https://scsh.net/">scheme shell</a>, but I the last time I tried to install the scheme shell
  11. on OpenBSD, it failed to compile.</p><p>Anyway, it is really easy to write your own script. <code>hotplugd</code> will call your
  12. script like so:</p><pre><code>attach &lt;number&gt; &lt;label&gt;</code></pre><p>where <code>&lt;number&gt;</code> is one of the numbers in the table below and <code>&lt;label&gt;</code> is a
  13. short descriptive string of the device.</p><pre><code>|---+------------------------------------|
  14. | 0 | generic, no special info |
  15. |---+------------------------------------|
  16. | 1 | CPU (carries resource utilization) |
  17. |---+------------------------------------|
  18. | 2 | disk drive |
  19. |---+------------------------------------|
  20. | 3 | network interface |
  21. |---+------------------------------------|
  22. | 4 | tape device |
  23. |---+------------------------------------|
  24. | 5 | serial line interface |
  25. |---+------------------------------------|</code></pre><p>I am only really interested in <code>2</code> and <code>3</code>. Here is how I debbuged my attach
  26. script, and you can easily do the same.</p><p>First find out what <code>sd</code> device your usb stick is. Before you put in your usb
  27. stick type in:</p><pre><code>sysctl hw.disknames
  28. hw.disknames=sd0:ec557d42f5cbfa41,sd1:5583d235b610c8a2</code></pre><p>Now put in your usb stick and run the same command.</p><pre><code>sysctl hw.disknames
  29. hw.disknames=sd0:ec557d42f5cbfa41,sd1:5583d235b610c8a2,sd2:</code></pre><p>So now I know that my usb stick is sd2. Let’s do a disklabel command on it:</p><pre><code># disklabel sd2
  30. # /dev/rsd2c:
  31. type: SCSI
  32. disk: SCSI disk
  33. label: USB Flash Drive
  34. duid: 0000000000000000
  35. flags:
  36. bytes/sector: 512
  37. sectors/track: 63
  38. tracks/cylinder: 255
  39. sectors/cylinder: 16065
  40. cylinders: 1887
  41. total sectors: 30326784
  42. boundstart: 0
  43. boundend: 30326784
  44. 16 partitions:
  45. # size offset fstype [fsize bsize cpg]
  46. c: 14.5G 0 unused
  47. i: 14.5G 2048 MSDOS</code></pre><p>Notice from the output that this label is “USB Flash Drive”. That is the label
  48. that hotplugd will send to your attach script.</p><p>If you want a usb stick that is read-able/writeable accross all operating
  49. systems, currently you will want to use the <a href="https://wiki.archlinux.org/title/FAT">vfat</a> filesystem. That is what the
  50. output above shows. The <code>fstype</code> of <code>MSDOS</code> is a vfat filesystem. This usb stick
  51. is what I will use when I want to copy data between different OS-es (I do want
  52. an <a href="https://www.openbsd.org/faq/faq14.html#softraidCrypto">encrypted OpenBSD-specific usb stick</a> to store my gpg keys, but I have not yet
  53. set that up). According to some of the smart people on the <code>#openbsd</code> irc
  54. channel, if you have such a usb stick, then the <code>i</code> filesystem partition is the
  55. one that you want to mount to read the data. And we see that above as well (<code>c</code>
  56. is code for the whole drive. <code>/dev/rsd2c</code> is how you access the whole and raw
  57. disk).</p><p>Ok, so now that you know what arguments that <code>hotplugd</code> will send your script,
  58. go ahead and write your basic script. It probably won’t be perfect, which is ok.
  59. To test it, type in <code>su</code> in your terminal to get to root account, and then test
  60. your script in the exact same way that OpenBSD will use your script (<code>#</code> means
  61. that you are currently the root user):</p><pre><code># ./attach 2 &quot;USB Flash Drive&quot;</code></pre><p>You will probably get some weird errors, and that’s ok. After you have run your
  62. attach script, and it seemed to have no errors, verify that it properly mounted
  63. with:</p><pre><code>mount
  64. /dev/sd1a on / type ffs (local, softdep)
  65. /dev/sd1k on /home type ffs (local, nodev, nosuid, softdep)
  66. /dev/sd1d on /tmp type ffs (local, nodev, nosuid, softdep)
  67. /dev/sd1f on /usr type ffs (local, nodev, softdep)
  68. /dev/sd1g on /usr/X11R6 type ffs (local, nodev, softdep)
  69. /dev/sd1h on /usr/local type ffs (local, nodev, wxallowed, softdep)
  70. /dev/sd1j on /usr/obj type ffs (local, nodev, nosuid, softdep)
  71. /dev/sd1i on /usr/src type ffs (local, nodev, nosuid, softdep)
  72. /dev/sd1e on /var type ffs (local, nodev, nosuid, softdep)
  73. /dev/sd2i on /mnt/usb type msdos (local, nodev, noexec)</code></pre><p>And it look like I properly mounted my usb stick (the last line says it was).</p><p>One thing that users might find confusing is that OpenBSD passes in <code>2</code>, but
  74. Guile accepted the <code>2</code> as a string.</p><p>My simple <a href="https://notabug.org/jbranso/prog/src/master/gnu/guile/scripts/attach">attach script</a> works for me. It will only auto mount vfat filesystems,
  75. and I am pretty sure that weird things will happen if I plug in two usb sticks
  76. at once, but it works.</p></div></article></section></main><footer><p>© 2020 Joshua Branson. The text on this site is free culture under the Creative Commons Attribution Share-Alike 4.0 International license.</p><p>This website is build with Haunt, a static site generator written in Guile Scheme. Source code is <a href="https://notabug.org/jbranso/gnucode.me">available.</a></p><p>The color theme of this website is based off of the famous <a href="#3f3f3f" target="_blank">zenburn</a> theme.</p></footer></body>