Resolve symlinks in device path for removable check#52
Conversation
7ae5ae3 to
f2fc471
Compare
|
Reformatted as suggested by |
When running with --removable-device, the basename of dest is used to construct the path of the "removable" sysfs file for the device. However, if dest is a symlink the basename may be very different from the actual device name, and theoretically might even match a different device. Make the path absolute and resolve any symlinks before taking the basename to avoid that problem. This is of practical importance because it allows using /dev/disk/... symlinks created by udev, which is much less error-prone than using e.g. /dev/sdX. Signed-off-by: Fiona Klute <fiona.klute@gmx.de>
f2fc471 to
2586500
Compare
twoerner
left a comment
There was a problem hiding this comment.
Verdict: APPROVE
This PR is a one-line change in src/bmaptool/CLI.py that wraps args.dest in os.path.realpath() before taking the basename, so the --removable-device precondition resolves /dev/disk/by-id/...-style symlinks to their underlying block device before looking up /sys/block/<name>/removable.
Must-fix before merge
Nothing must change before merge.
Suggestions
- Consider mentioning
/dev/disk/by-id/...(orby-uuid,by-label, etc.) inbmaptool --help's description of the--removable-deviceflag, since the new behavior makes those forms first-class arguments. Optional - not blocking.
Test follow-up
Regression test recommended as follow-up: a unit test that monkeypatches os.path.realpath and the sysfs open(...) to pin the sysfs path the precondition cascade computes for a symlinked destination, so a future regression that drops the realpath call surfaces as an assertion failure rather than silently re-introducing the bug. Not added on this PR to keep the scope honest; will be landed as its own maintainer PR.
CI status
All eight required contexts green on the rebased head (lint, test (3.9) through test (3.14), test (native)). The three advisory CodeQL / Analyze checks are also green.
Reviewed against
Head SHA 258650043ece7072aa0bc337d790c96e50108637, rebased clean onto main at 0c3f66af538f. smoke-full green end-to-end on the rebased tree.
When running with
--removable-device, the basename ofdestis used to construct the path of the "removable" sysfs file for the device. However, ifdestis a symlink the basename may be very different from the actual device name, and theoretically might even match a different device. Make the path absolute and resolve any symlinks before taking the basename to avoid that problem.This is of practical importance because it allows using
/dev/disk/...symlinks created by udev, which is much less error-prone than using e.g./dev/sdX.