123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <!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
- my usbstick was essential to the boot process, and then, when I booted the
- laptop, I did not have that usb stick mounted. That caused some problems. I
- since learned that the preferred way of automounting a usb stick under OpenBSD
- 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
- and easy. Just put your scripts at <code>/etc/hotplugd/attach</code> and
- <code>/etc/hotplugd/detach</code>. And the man page gives you an example shell script, but
- since I am not a big fan of <code>sh</code> (its syntax is confusing), I decided to write
- my attach script in <a href="https://www.gnu.org/software/guile/">GNU Guile</a>. Writing that script made me want to write more
- scripts in <a href="https://scsh.net/">scheme shell</a>, but I the last time I tried to install the scheme shell
- on OpenBSD, it failed to compile.</p><p>Anyway, it is really easy to write your own script. <code>hotplugd</code> will call your
- script like so:</p><pre><code>attach <number> <label></code></pre><p>where <code><number></code> is one of the numbers in the table below and <code><label></code> is a
- short descriptive string of the device.</p><pre><code>|---+------------------------------------|
- | 0 | generic, no special info |
- |---+------------------------------------|
- | 1 | CPU (carries resource utilization) |
- |---+------------------------------------|
- | 2 | disk drive |
- |---+------------------------------------|
- | 3 | network interface |
- |---+------------------------------------|
- | 4 | tape device |
- |---+------------------------------------|
- | 5 | serial line interface |
- |---+------------------------------------|</code></pre><p>I am only really interested in <code>2</code> and <code>3</code>. Here is how I debbuged my attach
- 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
- stick type in:</p><pre><code>sysctl hw.disknames
- 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
- 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
- # /dev/rsd2c:
- type: SCSI
- disk: SCSI disk
- label: USB Flash Drive
- duid: 0000000000000000
- flags:
- bytes/sector: 512
- sectors/track: 63
- tracks/cylinder: 255
- sectors/cylinder: 16065
- cylinders: 1887
- total sectors: 30326784
- boundstart: 0
- boundend: 30326784
- 16 partitions:
- # size offset fstype [fsize bsize cpg]
- c: 14.5G 0 unused
- i: 14.5G 2048 MSDOS</code></pre><p>Notice from the output that this label is “USB Flash Drive”. That is the label
- that hotplugd will send to your attach script.</p><p>If you want a usb stick that is read-able/writeable accross all operating
- systems, currently you will want to use the <a href="https://wiki.archlinux.org/title/FAT">vfat</a> filesystem. That is what the
- output above shows. The <code>fstype</code> of <code>MSDOS</code> is a vfat filesystem. This usb stick
- is what I will use when I want to copy data between different OS-es (I do want
- 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
- set that up). According to some of the smart people on the <code>#openbsd</code> irc
- channel, if you have such a usb stick, then the <code>i</code> filesystem partition is the
- one that you want to mount to read the data. And we see that above as well (<code>c</code>
- is code for the whole drive. <code>/dev/rsd2c</code> is how you access the whole and raw
- disk).</p><p>Ok, so now that you know what arguments that <code>hotplugd</code> will send your script,
- go ahead and write your basic script. It probably won’t be perfect, which is ok.
- To test it, type in <code>su</code> in your terminal to get to root account, and then test
- your script in the exact same way that OpenBSD will use your script (<code>#</code> means
- that you are currently the root user):</p><pre><code># ./attach 2 "USB Flash Drive"</code></pre><p>You will probably get some weird errors, and that’s ok. After you have run your
- attach script, and it seemed to have no errors, verify that it properly mounted
- with:</p><pre><code>mount
- /dev/sd1a on / type ffs (local, softdep)
- /dev/sd1k on /home type ffs (local, nodev, nosuid, softdep)
- /dev/sd1d on /tmp type ffs (local, nodev, nosuid, softdep)
- /dev/sd1f on /usr type ffs (local, nodev, softdep)
- /dev/sd1g on /usr/X11R6 type ffs (local, nodev, softdep)
- /dev/sd1h on /usr/local type ffs (local, nodev, wxallowed, softdep)
- /dev/sd1j on /usr/obj type ffs (local, nodev, nosuid, softdep)
- /dev/sd1i on /usr/src type ffs (local, nodev, nosuid, softdep)
- /dev/sd1e on /var type ffs (local, nodev, nosuid, softdep)
- /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
- 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,
- and I am pretty sure that weird things will happen if I plug in two usb sticks
- 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>
|