loop/014: Add test for backing file on loop device partition
PATCH 1/1 loop/014: Add test for backing file on loop device partition
Add a new loop driver test that verifies that attempting to set the backing file of a loop device to a file located on a filesystem on one of the same loop device's partitions fails as expected by triggering the kernel function loop_validate_file().
Modify src/loop_change_fd.c to accept a -r option to open the loop device and backing file in read-only mode (O_RDONLY).
The test performs the following steps:
- Create a 1 GB image file ($TMPDIR/img).
- Instantiate a loop device for the image file with partition scanning enabled.
- Create two partitions of equal size on that loop device.
- Format the second partition with the ext4 filesystem and mount it.
- Create a 1 GB file on the mounted filesystem.
- Unmount the filesystem, detach the loop device, and re-instantiate it in read-only mode.
- Mount the partition read-only.
- Use src/loop_change_fd -r to attempt to change the backing file descriptor of the loop device to the file on its own partition via LOOP_CHANGE_FD, and verify that loop_validate_file() rejects it.
- Clean up by unmounting the filesystem and detaching the loop device.
--- src/loop_change_fd.c | 20 ++++++-- tests/loop/014 | 107 +++++++++++++++++++++++++++++++++++++++++++ tests/loop/014.out | 2 + 3 files changed, 125 insertions(+), 4 deletions(-) create mode 100755 tests/loop/014 create mode 100644 tests/loop/014.out
src/loop_change_fd.c +16 -4
@@ -11,25 +11,37 @@void usage (const char *progname) { -fprintf (stderr,"usage: %s LOOPDEV PATH\n" , progname); +fprintf (stderr,"usage: %s [-r] LOOPDEV PATH\n" , progname);exit (EXIT_FAILURE ); }int main (int argc,char **argv) { +int flags =O_RDWR ;int ret;int fd, filefd; +int c; -if (argc !=3 ) +while ((c =getopt (argc, argv,"r" )) !=-1 ) { +switch (c) { +case 'r' : + flags =O_RDONLY ; +break ; +default : +usage (argv[0 ]); + } + } + +if (optind +2 != argc)usage (argv[0 ]); - fd =open (argv[1 ],O_RDWR ); + fd =open (argv[optind], flags);if (fd ==-1 ) {perror ("open" );return EXIT_FAILURE ; } - filefd =open (argv[2 ],O_RDWR ); + filefd =open (argv[optind +1 ], flags);if (filefd ==-1 ) {perror ("open" );return EXIT_FAILURE ;
tests/loop/014 new file +107 -0
107 lines not on this page · raw message
tests/loop/014.out new file +2 -0
@@ -0,0 +1,2 @@ +Running loop/014 +Test complete
Quoted 23 lines · Bart Van Assche
Add a new loop driver test that verifies that attempting to set the backing file of a loop device to a file located on a filesystem on one of the same loop device's partitions fails as expected by triggering the kernel function
loop_validate_file().Modify
src/loop_change_fd.cto accept a -r option to open the loop device and backing file in read-only mode (O_RDONLY).The test performs the following steps:
- Create a 1 GB image file ($TMPDIR/img).
- Instantiate a loop device for the image file with partition scanning enabled.
- Create two partitions of equal size on that loop device.
- Format the second partition with the ext4 filesystem and mount it.
- Create a 1 GB file on the mounted filesystem.
- Unmount the filesystem, detach the loop device, and re-instantiate it in read-only mode.
- Mount the partition read-only.
- Use src/loop_change_fd -r to attempt to change the backing file descriptor of the loop device to the file on its own partition viaLOOP_CHANGE_FD, and verify thatloop_validate_file()rejects it.
- Clean up by unmounting the filesystem and detaching the loop device.
Quoted 1 line
Signed-off-by Bart Van Assche <[email protected]>
Thanks for the patch. Overall, it looks good to me. I ran the test using the kernel at the current Linus master branch tip, and confirmed the test case recreates the failrue, and the kernel side fix patch avoids the failure [1]. Good.
Please find two nit comments in line below. They are not a big deal. If you want, I can fold-in the changes when I apply this patch after the kernel side fix get upstreamed.
tests/loop/014 line 7
Quoted 13 lines
tests/loop/014 new file +7
-0@@ -0,0 +1,107 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-3.0+ +# Copyright (C) 2026 Google LLC +# +# Verify that attempting to change a loop device's backing file (via +# LOOP_CHANGE_FD) to a file on a filesystem residing on one of its own +# partitions triggers the kernel function loop_validate_file() and fails.
I think the kernel side fix commit should be mentioned here.
Quoted 21 lines
tests/loop/014 new file +21
-0+ +. tests/loop/rc + +DESCRIPTION="trigger loop_validate_file() when changing backing file to own partition" +QUICK=1 + +loop_device= +mount_dir= + +requires() { + _have_program mkfs.ext4 + _have_program parted + _have_src_program loop_change_fd +} + +cleanup() { + if [[ -n "$mount_dir" ]]; then + umount "$mount_dir" &>/dev/null + fi + if [[ -n "$loop_device" ]]; then + losetup --detach "$loop_device" &>/dev/null
tests/loop/014 line 38
Quoted 10 lines
tests/loop/014 new file +10
-0+ fi +} + +test() { + echo "Running ${TEST_NAME}" + _register_test_cleanup cleanup + + mount_dir="$TMPDIR/mnt" + + truncate -s 1G "$TMPDIR/img"
Nit: I suggest the long option "--size=1G" instead of the short option "-s 1G".
Same for mkdir -p option and mount -t option.