[Toybox] I aten't dead.

Isaac Dunham ibid.ag at gmail.com
Thu Feb 26 10:01:19 PST 2015


On Thu, Feb 26, 2015 at 06:26:19AM -0600, Rob Landley wrote:
> On 02/25/2015 10:27 AM, Isaac Dunham wrote:
> > On Wed, Feb 25, 2015 at 03:19:23AM -0600, Rob Landley wrote:
> >> There's a marvelous book called "the unix philosophy" by mike gancarz
> >> that talks about output intended for humans vs output intended to be
> >> processed by other tools.
> >>
> >> A scriptable tool is one where you can do:
> >>
> >>   echo $(( $(wc -l < blah) * 3))
> >>
> >> If it's scriptable, the output of one tool is naturally the input to
> >> another. Admittedly this can be annoying to use from the command line
> >> such as the way "ls" with no arguments will produce no output when run
> >> in an empty directory. But the _reason_ for that is so "for i in $(ls);
> >> do blah $i; done" doesn't have to filter the ls output to use it in a
> >> script.
> > 
> > Thread hijack/blkid feature I'd like to see (no need to do it now, I'm
> > willing to try writing it at a later point).
> > 
> > I was just fighting with non-scriptable output in another tool:
> > blkid.
> 
> Which is why fstype exists, although it only gives you the type not the
> other attributes.

I'm actually after just the label/just the uuid.

> > By default it's ROUGHLY shell-quoted, with fields in variable positions
> > (sample is not actual output, but how it looks):
> > /dev/sda1: SEC_TYPE="msdos" LABEL="ECS_CDRUN" UUID="DEAD-BEEF" TYPE="vfat" PARTUUID="12345678-01"
> > /dev/sda2: UUID="01234567-abcd-bcde-cdef-0123456789ab" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="12345678-02"
> > /dev/sda3: UUID="01234567-abcd-bcde-cdef-0123456789ab" TYPE="ext4" PARTUUID="12345678-03"
> > /dev/sdb: LABEL="HA\"; `rm -rf /`" UUID="12345678-dcba-bcde-cdef-0123456789ab" TYPE="ext4"
> > 
> > That's how it does the quoting: "" around each field, \ before " in the label.
> > 
> > util-linux blkid has a whole bunch of rarely used options,
> 
> Which is why I didn't implement them. (That and the lack of standard.)
> 
> > but two of them
> > seem to make it a whole lot easier to parse:
> > 
> >  -o <format> output format; can be one of:
> >                value, device, export or full; (default: full)
> > (They don't mention -o udev here, but it works.)
> >
> >  -s <tag>    show specified tag(s) (default show all tags)
> > 
> > <tag> is TYPE/SEC_TYPE/UUID/PARTUUID/LABEL, and means that only
> > the fields specified get shown, in the same order as usual.
> > Example:
> > blkid -s TYPE -s LABEL -s UUID /dev/sda1
> > /dev/sda1: LABEL="ECS_CDRUN" UUID="DEAD-BEEF" TYPE="vfat"
> 
> If it takes a comma separated list and maybe I can get it to share code
> with ps -o and mount -o.

No, but a linked list of strings (optstring "s*") should work.

> (I'm currently filing rough edges off of ps, not yet a proper cleanup
> but working my way towards it. I want to implement the bsd ps option
> behavior but haven't figured out a graceful way to do it yet. Unlike
> with tar, the options mean different things in the different contexts...)
> 
> > Of the listed output formats, 
> > -o device prints the block device/file and nothing else,
> > 
> > -o export shell-quotes everything:
> > /sbin/blkid -s LABEL -s UUID -o export /dev/sdb
> > DEVNAME=/dev/sdb
> > LABEL=HA\";\ \`rm\ -rf\ /\`
> > UUID=282e77a6-cbd3-4692-8c62-715c45970377
> 
> I notice each field of that is on its own line, and unquoted by default.

Sorry, wrong term. It's escaped not quoted.

> > -o value uses unquoted strings like this:
> > blkid -o value -s UUID -s LABEL /dev/sdb
> > HA"; `rm -rf /`
> > 282e77a6-cbd3-4692-8c62-715c45970377
> 
> The previous strings were unquoted (although their contents were
> escaped), these are unlabeled. (And unescaped.)

> > -o udev (which uses \x<ff> style quoting) is also available,
> > but only mentioned in the manpage.
> > Example:
> > /sbin/blkid -o udev test.img
> > ID_FS_LABEL=HA____rm_-rf_/_
> > ID_FS_LABEL_ENC=HA\x22\x3b\x20\x60rm\x20-rf\x20\x2f\x60
> > ID_FS_UUID=282e77a6-cbd3-4692-8c62-715c45970377
> > ID_FS_UUID_ENC=282e77a6-cbd3-4692-8c62-715c45970377
> > ID_FS_TYPE=ext2

 
> > this is needed for mount by label to work.
> 
> I've never understood why anyone does that, but it's not my place to
> second guess people's use cases...

I rarely use it, but it can be handy (for example, if you have a couple
flash drives which you want mounted in fixed places).
 
> > And the ID_FS_LABEL_ENC field of -o udev matches what I'm looking for
> > exactly.
> 
> I'm confused: what is your use case?

Narrowly:
Populate /dev/disk/by-{label,uuid}/ with symlinks having the same name 
that udev gives them, so that mount by label and mount by uuid work right.

Broadly:
I've been working on a package to allow busybox mdev to replace udev
in Debian-based distros, which has already gotten some interest from
others on the "Dng" list ("Dng" stands for "Debian's not Gnome"; it's
the list for Devuan, the fork of Debian over systemd).
Meanwhile, Jude Nelson has been working on a daemon that should
be a replacement for udev (vdev); this will use helper scripts much like
mdev does, and I'm planning to add /dev/disk/by-label/ support for it.
On Debian/Devuan, I know that blkid is provided by util-linux, but
I'd rather be *able* to switch out components.


> In theory properly quoted shell strings shouldn't care what their
> contents are (modulo NUL bytes and stripping trailing whitespace):
> 
>   X="$(fruitbat -x)"
>   command "$X"
> 
> Should be fine about having semicolons and quotes and stuff in what $X
> expands to...? So the classic definition of "scriptable" would be not
> having any LABEL= at all, but instead having a way to request the
> specific field of interest...?

Which is  what "blkid -s LABEL -o value" does.
But any of the other 

> (And if you want a label that's really screwing with mount-by-label, put
> a / in it.)

udev/blkid -o udev handles this by converting all "unsafe" chars to
"\x<hex value>" so a label "/" becomes "/dev/disk/by-label/\x2f".
 
Thanks,
Isaac Dunham

 1424973679.0


More information about the Toybox mailing list