Skip to content

Conversation

@crass
Copy link

@crass crass commented Aug 19, 2025

Some changes in this patch series:

  • Ensure that devices with no parent are marked as readonly.
  • Do not set block device to readonly when it is in READONLY_IGNORE
  • Remove dependency on printf binary by replacing with builtin echo
  • Fix properly finding the parent of a device-mapper device
  • Add support for devices with multiple parents
  • Add support for loop devices, which need special handling

crass added 4 commits August 19, 2025 02:44
…arent

When parent_device is empty, `grep -qw "${parent_device}"` will always
return true regardless of input. So when READONLY_IGNORE is set and no
parent device is found for a device, that device is never marked readonly.
However, newly seen devices should be marked as readonly unless otherwise
specified (eg. in READONLY_IGNORE). Ensure that devices with no parent are
marked as readonly.

Signed-off-by: Glenn Washburn <[email protected]>
…READONLY_IGNORE

The script never checked for the block device in READONLY_IGNORE, but it
appears that this was intended. So a new block device would be set to
readonly, even if it was specified in READONLY_IGNORE.

Signed-off-by: Glenn Washburn <[email protected]>
There is no need to depend on a separate binary when the shell's builtin
`echo` function will suffice.

Signed-off-by: Glenn Washburn <[email protected]>
…device-mapper device

Parent devices of device-mapper devices use a directory named "holders" in
their sysfs block device directory that lists all device-mapper devices
which use that device. This is unlike partition devices of block devices
which have directory directly in the sysfs block device directory. So if
the current device name is not found directly in the sysfs block device
directory, then check the "holders" directory.

Signed-off-by: Glenn Washburn <[email protected]>
@zeha zeha requested a review from mika August 19, 2025 08:12
@zeha
Copy link
Member

zeha commented Aug 19, 2025

Hi, thanks for the PR! The build-and-push failure is on me (and I'll try to fix that), however the reported shellcheck issues are, I think, real. Please check them :-)

crass added 2 commits August 20, 2025 20:44
… parents

Some block devices can have multiple parents, such as LVM raid volumes.

Signed-off-by: Glenn Washburn <[email protected]>
@mika
Copy link
Member

mika commented Aug 22, 2025

Thanks for working on this, I like what I see but need some more time to properly review/test it.
Trying to look into this at the beginning of September!

@crass
Copy link
Author

crass commented Sep 21, 2025

Thanks for working on this, I like what I see but need some more time to properly review/test it. Trying to look into this at the beginning of September!

Checking in, in case this dropped off the radar.

@mika
Copy link
Member

mika commented Sep 21, 2025

Thanks for working on this, I like what I see but need some more time to properly review/test it. Trying to look into this at the beginning of September!

Checking in, in case this dropped off the radar.

Thanks, definitely didn't forget it, just didn't have time yet, will try to take care in the next days! Thanks

@crass
Copy link
Author

crass commented Nov 23, 2025

Ping. Still not forgotten?

@mika
Copy link
Member

mika commented Nov 24, 2025

Ping. Still not forgotten?

Sorry, definitely not forgotten, will try to take care this week!

Copy link
Member

@mika mika left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your changes, they all make sense for me, I have just some minor questions, suggestions and nitpicks.

I'm curious though, in what scenario did you stumble upon this, would you have any STR (Steps To Reproduce) for us, to verify the existing vs new behavior from your PoV?

Thanks! 🙇

parent_devices=$(findmnt -n -o source --target "$backingfile")

# Set loop device to read-write, will be set to readonly later if need be
blockdev --setrw "${BLOCK_DEVICE}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why we should set this to rw by default instead of leaving it as-is?
E.g. the squashfs file that's usually behind /dev/sr0 is set to read-only already, no reason to set it temporarily to rw, nor?

blockdev --setrw "${BLOCK_DEVICE}"

# Check if either the backing file's block device or filesystem are read-only
if is_ro "$parent_devices" || findmnt -n -o OPTIONS --target "$backingfile" | grep -E '^ro,|,ro,|,ro$'; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The grep returns its match on stdout, use grep -qE ... instead?

else
logger -t forensic-mark-readonly "not setting '${BLOCK_DEVICE}' to read-only as present in ignore list"
if [ -n "${parent_devices}" ] ; then
patternsfile=$(mktemp /dev/shm/forensic-mark-readonly.patterns.XXXXXX)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why we'd want to hard-code the directory /dev/shm/ here?
Otherwise I'd prefer mktemp -t forensic-mark-readonly.patterns.XXXXXX instead, so mktemp(1) can decide whether $TMPDIR is set, or use /tmp elsewhere.

patternsfile=
cleanup() {
if [ -f "$patternsfile" ] ; then
rm "$patternsfile"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's maybe use rm -f "${patternsfile}" to not (report) fail(ure) if removing should fail?


patternsfile=
cleanup() {
if [ -f "$patternsfile" ] ; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use "${patternsfile}" instead of "$patternsfile" to keep coding style style consistent, same for e.g "$backingfile" etc further below

logger -t forensic-mark-readonly "not setting '${BLOCK_DEVICE}' (parent device: '${parent_device}') to read-only as present in ignore list"
else
logger -t forensic-mark-readonly "not setting '${BLOCK_DEVICE}' to read-only as present in ignore list"
if [ -n "${parent_devices}" ] ; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe use "${parent_devices:-}" also here, as it used to be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants