From: "Holger Hoffstätte" <holger@applied-asynchrony.com>
To: Hans van Kranenburg <Hans.van.Kranenburg@mendix.com>,
linux-btrfs <linux-btrfs@vger.kernel.org>
Subject: Re: Curious problem: btrfs device stats & unpriviliged access
Date: Wed, 23 Oct 2019 16:24:49 +0200 [thread overview]
Message-ID: <85bc00c7-a5b9-ffa7-3d68-97db8f199cf4@applied-asynchrony.com> (raw)
In-Reply-To: <faab0e97-c853-11dd-243d-021232b0783a@mendix.com>
It's been a while.. :D
On 10/8/18 10:02 PM, Hans van Kranenburg wrote:
> On 10/08/2018 06:37 PM, Holger Hoffstätte wrote:
>> On 10/08/18 17:46, Hans van Kranenburg wrote:
>> <snip>
>>> fs.devices() also looks for dev_items in the chunk tree:
>>>
>>> https://github.com/knorrie/python-btrfs/blob/master/btrfs/ctree.py#L481
>>>
>>> So, BOOM! you need root.
>>>
>>> Or just start a 0, ignore errors and start trying all devids until you
>>> found num_devices amount of them that work, yolo.
>>
>> Since I need to walk /sys/fs/btrfs/ anyway I *think* I can just look
>> at the entries in /sys/fs/btrfs/<fsid>/devices/ and query them all
>> directly.
>
> But, you still need root for that right? The progs code does a RO open
> directly on the block device.
>
> -$ btrfs dev stats /dev/xvdb
> ERROR: cannot open /dev/xvdb: Permission denied
> ERROR: '/dev/xvdb' is not a mounted btrfs device
>
> stat("/dev/loop0", {st_mode=S_IFBLK|0660, st_rdev=makedev(7, 0), ...}) = 0
> stat("/dev/loop0", {st_mode=S_IFBLK|0660, st_rdev=makedev(7, 0), ...}) = 0
> open("/dev/loop0", O_RDONLY) = -1 EACCES (Permission denied)
>
> But:
>
> -# btrfs dev stats /dev/xvdb
> [/dev/xvdb].write_io_errs 0
> [/dev/xvdb].read_io_errs 0
> [/dev/xvdb].flush_io_errs 0
> [/dev/xvdb].corruption_errs 0
> [/dev/xvdb].generation_errs 0
As it turns out you don't need full root, you need permissions to read the device.
Which permissions are those, you ask?
holger>ll /dev/loop0
brw-rw---- 1 root disk 7, 0 Oct 23 02:10 /dev/loop0
Indeed directly reading the device started working when I added myself to the
"disk" group (I was in wheel before, but that wasn't sufficient - good!). \o/
Never go full root. Adding the daemon to a group (e.g. during installation)
is IMHO acceptable.
This particular rabbit hole goes deep since it involves systemd, udev,
session management and eventually alcohol; see:
https://wiki.archlinux.org/index.php/Users_and_groups#Group_list and
https://enotty.pipebreaker.pl/2012/05/23/linux-automatic-user-acl-management/
for things you never really wanted to know. In my case I can get away with
the disk group menbership since I'm on Gentoo with OpenRC.
I still have to figure out the devid(s) for the ioctl, but the above
mentioned Yolo Method™ of iterating after finding the number of devices
in sysfs (under <fs-uuid>/devices/) might just be good enough; we'll see.
-h
prev parent reply other threads:[~2019-10-23 14:24 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-10-08 14:27 Curious problem: btrfs device stats & unpriviliged access Holger Hoffstätte
2018-10-08 14:40 ` Hans van Kranenburg
2018-10-08 14:54 ` Hans van Kranenburg
2018-10-08 15:29 ` Holger Hoffstätte
2018-10-08 15:46 ` Hans van Kranenburg
2018-10-08 16:37 ` Holger Hoffstätte
2018-10-08 20:02 ` Hans van Kranenburg
2019-10-23 14:24 ` Holger Hoffstätte [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=85bc00c7-a5b9-ffa7-3d68-97db8f199cf4@applied-asynchrony.com \
--to=holger@applied-asynchrony.com \
--cc=Hans.van.Kranenburg@mendix.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).