Commit Graph

128 Commits

Author SHA1 Message Date
Luca Boccassi
f5bb0a3163 loop-util: restart loop_configure and reopen FD when O_DIRECT fails
On kernel 5.10.178, when a squashfs file is stored on an EXT4 filesystem
backed by a dm-crypt volume, dissecting fails:

$ SYSTEMD_LOG_LEVEL=debug systemd-dissect /var/foo/bar.raw
Opened '/var/foo/bar.raw' in O_RDONLY access mode, with O_DIRECT enabled.
Couldn't find any partition table to derive sector size of.
loop2: Acquired exclusive lock.
Could not enable direct IO mode, proceeding in buffered IO mode.
Successfully acquired /dev/loop2, devno=7:2, nr=2, diskseq=87
Opened /dev/loop2 (fd=3, whole_block_devnum=7:2, diskseq=87).
      Name: bar.raw
      Size: 67.2M
 Sec. Size: 512
     Arch.: n/a

Successfully forked off '(sd-dissect)' as PID 4110.
Mounting /proc/self/fd/3 (squashfs) on /tmp/dissect-Zk3K5F (MS_RDONLY|MS_NODEV "")...
Failed to mount /proc/self/fd/3 (type squashfs) on /tmp/dissect-Zk3K5F (MS_RDONLY|MS_NODEV ""): Input/output error
Failed to mount dissected image: Input/output error
Failed to read /etc/hostname of image: No such file or directory
/etc/machine-id file of image is empty.
Failed to read has-init-system boolean: Input/output error
(sd-dissect) failed with exit status 1.
Failed to acquire image metadata: Input/output error

The kernel shows I/O errors:

kernel: blk_update_request: I/O error, dev loop2, sector 0 op 0x0:(READ) flags 0x800 phys_seg 1 prio class 0
kernel: SQUASHFS error: Failed to read block 0x0: -5
kernel: unable to read squashfs_super_block

This is independent of a particular filesystem and can be reproduced
reliably in my setup, starting from freshly formatted disks.

Instead of continuing when O_DIRECT fails, start over the setup
process without the flag, including opening a new FD, to make the
kernel happy.
2023-11-16 21:12:40 +00:00
Lennart Poettering
ac110243a8 bsod,loop-util: fix fd validity check 2023-11-01 15:21:20 +01:00
Yu Watanabe
7a05926fbe udev: re-introduce symlinks for loopback block device
But the directories are changed from /dev/loop/by-ref/ -> /dev/disk/by-loop-ref/
and /dev/loop/by-inode/ -> /dev/disk/by-loop-inode/.
As /dev/loop/ is used by losetup command for other purpose.
See issue #28475.

This effectively reverts commits 9915cc6086,
5022fab15f, and
c0d998248e.
2023-08-10 07:05:47 +09:00
Yu Watanabe
c0d998248e tree-wide: drop references to /dev/loop/by-ref
Follow-up for #28476.
2023-07-21 10:24:20 +09:00
Yu Watanabe
4251512ea9 time-util: introduce usleep_safe()
We use usec_t for storing time value, which is 64bit.
However, usleep() takes useconds_t that is (typically?) 32bit.
Also, usleep() may only support the range [0, 1000000].

This introduce usleep_safe() which takes usec_t.
2023-06-22 15:33:56 +09:00
Daan De Meyer
05d1cbb33c Merge pull request #26969 from DaanDeMeyer/xopenat-label
fs-util: Add labelling support to xopenat()
2023-05-31 14:35:50 +02:00
Daan De Meyer
420d2e3136 fs-util: Add XOpenFlags with XO_LABEL flag to have xopenat() MAC label files/dirs 2023-05-31 13:15:56 +02:00
Zbigniew Jędrzejewski-Szmek
6483bcef54 shared/loop-util: add comment
Requested in afbe20b7d4 (r115653459).
2023-05-30 16:04:35 +02:00
Zbigniew Jędrzejewski-Szmek
afbe20b7d4 shared/loop-util: use longer delay when waiting for loop device
The kernel may be syncing a file system or doing something else that requires
more time. So make the delay a bit longer, but provide some feedback and also
grow the delay exponentially (though with a long exponent). If the kernel is
doing something else, no need to repeat so often. With 38 attempts, we get a
total of slightly above 5000 ms.

I wrote this when I thought that the the delay is not long enough. It turned
out that we were blocking the file system on the loop device, so waiting longer
wasn't helpful. But I think it's nicer to do it this way anyway.
2023-05-30 13:41:56 +02:00
Lennart Poettering
8e398254ba loop-util: port some code over to ASSERT_FD() 2023-04-24 20:52:52 +02:00
Daan De Meyer
972c8db589 loop-util: Add loop_device_make_by_path_at()
On top of taking a directory file descriptor, we use xopenat() so
that the function can also be used to work on existing file
descriptors to image files including all the logic to use O_DIRECT
and fallback to O_RDONLY if needed.
2023-03-23 17:36:17 +01:00
Yu Watanabe
432f1fa8c7 loop-util: add more debugging logs in loop_configure() 2023-03-19 20:32:09 +09:00
Lennart Poettering
999ac3e2b0 loop-util: add API for selecting "lo_file_name" field for a loopback device 2023-03-09 16:40:55 +01:00
Lennart Poettering
d2430d5097 loop-util: add call for setting the autoclear flag at arbitrary times 2023-03-09 16:40:55 +01:00
Lennart Poettering
4d2a9e3ea6 loop-util: keep track of inode/devnum of backing file 2023-03-09 16:40:55 +01:00
David Tardon
92651a7a2d tree-wide: initialize fds with -EBADF 2023-02-23 09:48:54 +01:00
Yu Watanabe
2421dd7267 loop-util: fix error condition and return value
Fixes a bug introduced by da4fd28871.
2023-02-19 05:20:53 +09:00
Lennart Poettering
22ee78a898 loop-util: always tell kernel explicitly about loopback sector size
Let's not leave the sector size unspecified: either set a user supplied
value, or auto-detect the right size by probing the disk image
accordingly.
2023-01-18 10:47:17 +01:00
Lennart Poettering
1163ddb386 loop-util: insist on setting the sector size correctly
If we attach a disk image to a loopback device the sector size of the
image must match the one of the loopback device, hence be more careful
here.
2023-01-18 10:10:57 +01:00
Lennart Poettering
65046b92dc blockdev-util: add simple wrapper around BLKSSZGET
Just adds some typesafety and generates an error if the field is not
initialized in the block device yet.
2023-01-18 10:10:57 +01:00
Yu Watanabe
5bb1d7fbab tree-wide: use -EBADF more 2022-12-21 01:50:33 +09:00
Zbigniew Jędrzejewski-Szmek
254d1313ae tree-wide: use -EBADF for fd initialization
-1 was used everywhere, but -EBADF or -EBADFD started being used in various
places. Let's make things consistent in the new style.

Note that there are two candidates:
EBADF 9 Bad file descriptor
EBADFD 77 File descriptor in bad state

Since we're initializating the fd, we're just assigning a value that means
"no fd yet", so it's just a bad file descriptor, and the first errno fits
better. If instead we had a valid file descriptor that became invalid because
of some operation or state change, the other errno would fit better.

In some places, initialization is dropped if unnecessary.
2022-12-19 15:00:57 +01:00
Lennart Poettering
fcd8a19da8 loop-util: add new loop_device_make_by_path_memory() helper
This uses the new memfd_clone_fd() call to make an in-memory copy of
some file before setting up a loopback block device on it.
2022-12-08 12:47:28 +01:00
Christian Göttsche
3e6b7d2626 loop-util: open lock fd read-only
flock(2) works with file descriptors opened with O_RDONLY.

This affects SELinux systems where access to block devices is quite
restricted to avoid bypasses on filesystem objects.
2022-11-05 19:19:56 +01:00
Aidan Dang
fd83c98e8a Implement --luks-sector-size for homed 2022-10-07 16:36:04 +02:00