<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>GNUcode.me</title><id>https://gnucode.me/feeds/tags/OpenBSD hotplugd usb.xml</id><subtitle>Tag: OpenBSD hotplugd usb</subtitle><updated>2024-05-01T13:21:27Z</updated><link href="https://gnucode.me/feeds/tags/OpenBSD hotplugd usb.xml" rel="self" /><link href="https://gnucode.me" /><entry><title>OpenBSD's hotplugd rocks!</title><id>https://gnucode.me/openbsds-hotplugd-rocks.html</id><author><name>Joshua Branson</name><email>jbranso@dismail.de</email></author><updated>2023-04-13T11:41:00Z</updated><link href="https://gnucode.me/openbsds-hotplugd-rocks.html" rel="alternate" /><content type="html">&lt;p&gt;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 &lt;code&gt;hotplugd&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://man.openbsd.org/hotplugd&quot;&gt;hotplugd&lt;/a&gt; is OpenBSD’s automounting functionality, and it’s actually super simple
and easy. Just put your scripts at &lt;code&gt;/etc/hotplugd/attach&lt;/code&gt; and
&lt;code&gt;/etc/hotplugd/detach&lt;/code&gt;. And the man page gives you an example shell script, but
since I am not a big fan of &lt;code&gt;sh&lt;/code&gt; (its syntax is confusing), I decided to write
my attach script in &lt;a href=&quot;https://www.gnu.org/software/guile/&quot;&gt;GNU Guile&lt;/a&gt;. Writing that script made me want to write more
scripts in &lt;a href=&quot;https://scsh.net/&quot;&gt;scheme shell&lt;/a&gt;, but I the last time I tried to install the scheme shell
on OpenBSD, it failed to compile.&lt;/p&gt;&lt;p&gt;Anyway, it is really easy to write your own script.  &lt;code&gt;hotplugd&lt;/code&gt; will call your
script like so:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;attach &amp;lt;number&amp;gt; &amp;lt;label&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;where &lt;code&gt;&amp;lt;number&amp;gt;&lt;/code&gt; is one of the numbers in the table below and &lt;code&gt;&amp;lt;label&amp;gt;&lt;/code&gt; is a
short descriptive string of the device.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;|---+------------------------------------|
| 0 | generic, no special info           |
|---+------------------------------------|
| 1 | CPU (carries resource utilization) |
|---+------------------------------------|
| 2 | disk drive                         |
|---+------------------------------------|
| 3 | network interface                  |
|---+------------------------------------|
| 4 | tape device                        |
|---+------------------------------------|
| 5 | serial line interface              |
|---+------------------------------------|&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I am only really interested in &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt;. Here is how I debbuged my attach
script, and you can easily do the same.&lt;/p&gt;&lt;p&gt;First find out what &lt;code&gt;sd&lt;/code&gt; device your usb stick is.  Before you put in your usb
stick type in:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;sysctl hw.disknames

hw.disknames=sd0:ec557d42f5cbfa41,sd1:5583d235b610c8a2&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now put in your usb stick and run the same command.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;sysctl hw.disknames

hw.disknames=sd0:ec557d42f5cbfa41,sd1:5583d235b610c8a2,sd2:&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So now I know that my usb stick is sd2.  Let’s do a disklabel command on it:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;# 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&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice from the output that this label is “USB Flash Drive”.  That is the label
that hotplugd will send to your attach script.&lt;/p&gt;&lt;p&gt;If you want a usb stick that is read-able/writeable accross all operating
systems, currently you will want to use the &lt;a href=&quot;https://wiki.archlinux.org/title/FAT&quot;&gt;vfat&lt;/a&gt; filesystem. That is what the
output above shows. The &lt;code&gt;fstype&lt;/code&gt; of &lt;code&gt;MSDOS&lt;/code&gt; 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 &lt;a href=&quot;https://www.openbsd.org/faq/faq14.html#softraidCrypto&quot;&gt;encrypted OpenBSD-specific usb stick&lt;/a&gt; to store my gpg keys, but I have not yet
set that up). According to some of the smart people on the &lt;code&gt;#openbsd&lt;/code&gt; irc
channel, if you have such a usb stick, then the &lt;code&gt;i&lt;/code&gt; filesystem partition is the
one that you want to mount to read the data. And we see that above as well (&lt;code&gt;c&lt;/code&gt;
is code for the whole drive. &lt;code&gt;/dev/rsd2c&lt;/code&gt; is how you access the whole and raw
disk).&lt;/p&gt;&lt;p&gt;Ok, so now that you know what arguments that &lt;code&gt;hotplugd&lt;/code&gt; 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 &lt;code&gt;su&lt;/code&gt; in your terminal to get to root account, and then test
your script in the exact same way that OpenBSD will use your script (&lt;code&gt;#&lt;/code&gt; means
that you are currently the root user):&lt;/p&gt;&lt;pre&gt;&lt;code&gt;# ./attach 2 &amp;quot;USB Flash Drive&amp;quot;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;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:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;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)&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And it look like I properly mounted my usb stick (the last line says it was).&lt;/p&gt;&lt;p&gt;One thing that users might find confusing is that OpenBSD passes in &lt;code&gt;2&lt;/code&gt;, but
Guile accepted the &lt;code&gt;2&lt;/code&gt; as a string.&lt;/p&gt;&lt;p&gt;My simple &lt;a href=&quot;https://notabug.org/jbranso/prog/src/master/gnu/guile/scripts/attach&quot;&gt;attach script&lt;/a&gt; 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.&lt;/p&gt;</content></entry></feed>