loopdev: report lost loop device file error
Subsystems (2)
PATCH v2 1/1 loopdev: report lost loop device file error
If a /dev/loopX is lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"
Since /sysfs still have the loop device intact, detect that and report detailed log message to guide user to recover the lost loop device file.
# ./losetup -a
/dev/loop0: [64512]:19133870 (/tmp/test.img)
# rm -rf /dev/loop0
# ./losetup -d /dev/loop0
/dev/loop0 is lost, run "mknod /dev/loop0 b 7 0" to recover it.
lt-losetup: /dev/loop0: detach failed: No such file or directory---
v2 <- v1:
- Reporting lost loop device file instead of using mknode to recreate
v1:
https://www.spinics.net/lists/util-linux-ng/msg17471.html
lib/loopdev.c | 37 ++++++++++++++++++++++++++++++------- sys-utils/losetup.c | 14 +++++--------- 2 files changed, 35 insertions(+), 16 deletions(-)
lib/loopdev.c +30 -7
@@ -279,6 +279,29 @@ static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)return lc->sysfs; } +/* +* losetup cmds could fail if /dev/loopX is removed by mistake. +* This function will detect that and report a detailed error +* log to help sysadmin recreate the lost loop device file. +*/ +static void loopcxt_scan_lost_device_file (struct loopdev_cxt *lc) +{ +static int scanned =0 ; +dev_t devno; + +/*only scan sysfs once for losetup cmd*/ +if (!scanned) + scanned =1 ; +else +return ; + devno =sysfs_devname_to_devno (lc->device); +if (devno <=0 ) +return ; + +fprintf (stderr,"%s is lost, run \"mknod %s b %d %d\" to recover it.\n" , + lc->device, lc->device,major (devno),minor (devno)); +} +static int __loopcxt_get_fd (struct loopdev_cxt *lc,mode_t mode) {int old =-1 ;
@@ -304,6 +327,13 @@ static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode) mode ==O_RDONLY ?"ro" : mode == O_RDWR ? "rw" : "??")); +/* loop device file not exist. */ +if (lc->fd <0 && errno ==ENOENT ) { +DBG (CXT ,ul_debugobj (lc,"%s doesn't exist." , + lc->device)); +loopcxt_scan_lost_device_file (lc); + errno =ENOENT ; + }if (lc->fd <0 && old >=0 ) {/* restore original on error */ lc->fd = old;
@@ -416,13 +446,6 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) !(lc->iter.flags & LOOPITER_FL_FREE))return 0 ;/* caller does not care about device status */ -if (!is_loopdev (lc->device)) { -DBG (ITER ,ul_debugobj (&lc->iter,"%s does not exist" , lc->device)); -return -errno; - } - -DBG (ITER ,ul_debugobj (&lc->iter,"%s exist" , lc->device)); - used =loopcxt_get_offset (lc,NULL ) ==0 ;if ((lc->iter.flags &LOOPITER_FL_USED ) && used)
sys-utils/losetup.c +5 -9
@@ -653,7 +653,7 @@ static int create_loop(struct loopdev_cxt *lc, }/* errors */ - errpre = hasdev &&loopcxt_get_fd (lc) <0 ? + errpre = hasdev && lc->fd <0 ?loopcxt_get_device (lc) : file;warn (_ ("%s: failed to set up loop device" ), errpre);break ;
@@ -741,8 +741,7 @@ int main(int argc, char **argv)break ;case 'c' : act =A_SET_CAPACITY ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;
@@ -754,8 +753,7 @@ int main(int argc, char **argv)break ;case 'd' : act =A_DELETE ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;
@@ -883,8 +881,7 @@ int main(int argc, char **argv)else act =A_SHOW_ONE ; -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), argv[optind]); optind++;
@@ -935,8 +932,7 @@ int main(int argc, char **argv)case A_DELETE : res =delete_loop (&lc);while (optind < argc) { -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))warn (_ ("%s: failed to use device" ), argv[optind]); optind++;
Signature · 1 line
2.39.3
Quoted 9 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"Since /sysfs still have the loop device intact, detect that and report detailed log message to guide user to recover the lost loop device file.
Quoted 6 lines
# ./losetup -a
/dev/loop0: [64512]:19133870 (/tmp/test.img)
# rm -rf /dev/loop0
# ./losetup -d /dev/loop0
/dev/loop0 is lost, run "mknod /dev/loop0 b 7 0" to recover it.
lt-losetup: /dev/loop0: detach failed: No such file or directoryMaybe, but just maybe, we shouldn't provide instructions how to fix the issue, but just print the major/minor numbers and let the user figure out what's going on and how to fix the issue.
Quoted 130 lines
Signed-off-by Junxiao Bi <[email protected]>---
v2 <- v1:
- Reporting lost loop device file instead of using mknode to recreate
v1:
https://www.spinics.net/lists/util-linux-ng/msg17471.htmllib/loopdev.c | 37 ++++++++++++++++++++++++++++++------- sys-utils/losetup.c | 14 +++++--------- 2 files changed, 35 insertions(+), 16 deletions(-)lib/loopdev.c +23
-0@@ -279,6 +279,29 @@ static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc)return lc->sysfs; } +/* +* losetup cmds could fail if /dev/loopX is removed by mistake. +* This function will detect that and report a detailed error +* log to help sysadmin recreate the lost loop device file. +*/ +static void loopcxt_scan_lost_device_file (struct loopdev_cxt *lc) +{ +static int scanned =0 ; +dev_t devno; + +/*only scan sysfs once for losetup cmd*/ +if (!scanned) + scanned =1 ; +else +return ; + devno =sysfs_devname_to_devno (lc->device); +if (devno <=0 ) +return ; + + fprintf(stderr, "%s is lost, run \"mknod %s b %d %d\" to recover it.\n", + lc->device, lc->device,major (devno),minor (devno)); +} +static int __loopcxt_get_fd (struct loopdev_cxt *lc,mode_t mode){
lib/loopdev.c +7
-0int old =-1 ;@@ -304,6 +327,13 @@ static int __loopcxt_get_fd(struct loopdev_cxt *lc, mode_t mode) mode ==O_RDONLY ?"ro" : mode == O_RDWR ? "rw" : "??")); +/* loop device file not exist. */ +if (lc->fd <0 && errno ==ENOENT ) { +DBG (CXT ,ul_debugobj (lc,"%s doesn't exist." , + lc->device)); +loopcxt_scan_lost_device_file (lc); + errno =ENOENT ; + }if (lc->fd <0 && old >=0 ) {/* restore original on error */ lib/loopdev.c +0
-7lc->fd = old;@@ -416,13 +446,6 @@ static int loopiter_set_device(struct loopdev_cxt *lc, const char *device) !(lc->iter.flags & LOOPITER_FL_FREE))return 0 ;/* caller does not care about device status */ -if (!is_loopdev (lc->device)) { -DBG (ITER ,ul_debugobj (&lc->iter,"%s does not exist" , lc->device)); -return -errno; - } - -DBG (ITER ,ul_debugobj (&lc->iter,"%s exist" , lc->device)); - used =loopcxt_get_offset (lc,NULL ) ==0 ;lib/loopdev.c +0
-0if ((lc->iter.flags &LOOPITER_FL_USED ) && used)sys-utils/losetup.c +5
-9@@ -653,7 +653,7 @@ static int create_loop(struct loopdev_cxt *lc, }/* errors */ - errpre = hasdev &&loopcxt_get_fd (lc) <0 ? + errpre = hasdev && lc->fd <0 ?loopcxt_get_device (lc) : file;warn (_ ("%s: failed to set up loop device" ), errpre);break ;@@ -741,8 +741,7 @@ int main(int argc, char **argv)break ;case 'c' : act =A_SET_CAPACITY ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -754,8 +753,7 @@ int main(int argc, char **argv)break ;case 'd' : act =A_DELETE ; -if (!is_loopdev (optarg) || -loopcxt_set_device (&lc, optarg)) +if (loopcxt_set_device (&lc, optarg))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), optarg);break ;@@ -883,8 +881,7 @@ int main(int argc, char **argv)else act =A_SHOW_ONE ; -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))err (EXIT_FAILURE ,_ ("%s: failed to use device" ), argv[optind]); optind++;@@ -935,8 +932,7 @@ int main(int argc, char **argv)case A_DELETE : res =delete_loop (&lc);while (optind < argc) { -if (!is_loopdev (argv[optind]) || -loopcxt_set_device (&lc, argv[optind])) +if (loopcxt_set_device (&lc, argv[optind]))warn (_ ("%s: failed to use device" ), argv[optind]); optind++;
Karel Zak
maintainerQuoted 18 lines · Dragan Simic
Quoted 10 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake,
future losetup operations on that loop device will fail. For examples, following cmds will fail when loop device file is lost.
- "losetup -d $loop_device"
- "losetup -f $file" or "mount $file $dev"Since /sysfs still have the loop device intact, detect that and report detailed log message to guide user to recover the lost loop device file.
Quoted 6 lines
# ./losetup -a /dev/loop0: [64512]:19133870 (/tmp/test.img) # rm -rf /dev/loop0 # ./losetup -d /dev/loop0 /dev/loop0 is lost, run "mknod /dev/loop0 b 7 0" to recover it. lt-losetup: /dev/loop0: detach failed: No such file or directory
Quoted 3 lines
Maybe, but just maybe, we shouldn't provide instructions how to fix the issue, but just print the major/minor numbers and let the user figure out what's going on and how to fix the issue.
The error message is just detail from my point of view :) Junxiao forgot to highlight that losetup (--all/--list) do not report at all the loop devices without /dev node. That's bug, because such devices are still used by kernel and should not be hidden for system admins.
I'm just playing with it and I'll push Junxiao's patch with some modifications.
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Karel Zak
maintainerQuoted 2 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.
Thanks Junxiao,
I has move aggressive with the changes and there is result:
https://github.com/util-linux/util-linux/pull/2566
For me the most important is to report used devices although there is not /dev loop node.
Quoted 7 lines
lib/loopdev.c +7
-0+static void loopcxt_scan_lost_device_file (struct loopdev_cxt *lc) +{ +static int scanned =0 ; +dev_t devno; + +/*only scan sysfs once for losetup cmd*/ +if (!scanned)
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 303
Quoted 10 lines
lib/loopdev.c +10
-0+ scanned =1 ; + else + return; + devno =sysfs_devname_to_devno (lc->device); +if (devno <=0 ) +return ; + +fprintf (stderr,"%s is lost, run \"mknod %s b %d %d\" to recover it.\n" , + lc->device, lc->device,major (devno),minor (devno)); +}
The lib/loopdev.c code is also used in libmount, so it should not directly print anything on stderr. It's application business to print the warning. I have moved it to losetup.c
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com
Quoted 7 lines · Karel Zak
Quoted 2 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.Thanks Junxiao,
I has move aggressive with the changes and there is result:
https://github.com/util-linux/util-linux/pull/2566
Thanks a lot. The changes looks good to me.
Quoted 16 lines
For me the most important is to report used devices although there is not /dev loop node.
Quoted 12 lines
lib/loopdev.c +12
-0+static void loopcxt_scan_lost_device_file (struct loopdev_cxt *lc) +{ +static int scanned =0 ; +dev_t devno; + +/*only scan sysfs once for losetup cmd*/ +if (!scanned) + scanned =1 ; +else +return ; + devno =sysfs_devname_to_devno (lc->device); +if (devno <=0 )
Quoted 8 lines
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 303
Quoted 5 lines
lib/loopdev.c +5
-0+return ; + +fprintf (stderr,"%s is lost, run \"mknod %s b %d %d\" to recover it.\n" , + lc->device, lc->device,major (devno),minor (devno)); +}The lib/loopdev.c code is also used in libmount, so it should not directly print anything on stderr. It's application business to print the warning. I have moved it to
losetup.c
Should we patch mount to report that error? with current patch, it report this:
# ./mount -o loop /tmp/test.img /mnt/ lt-mount: /mnt/: mount failed: No such file or directory.
Thanks,
Junxiao.
Quoted 3 lines
Karel
Karel Zak
maintainerQuoted 35 lines · On Wed, Nov 01, 2023 at 12:54:16PM -0700, [email protected] wrote:
Quoted 7 lines · Karel Zak
Quoted 2 lines · Junxiao Bi
If a
/dev/loopXis lost because someone might have removed it by mistake, future losetup operations on that loop device will fail.Thanks Junxiao,
I has move aggressive with the changes and there is result:
https://github.com/util-linux/util-linux/pull/2566Thanks a lot. The changes looks good to me.
Quoted 16 lines
For me the most important is to report used devices although there is not /dev loop node.
Quoted 12 lines
lib/loopdev.c +12
-0+static void loopcxt_scan_lost_device_file (struct loopdev_cxt *lc) +{ +static int scanned =0 ; +dev_t devno; + +/*only scan sysfs once for losetup cmd*/ +if (!scanned) + scanned =1 ; +else +return ; + devno =sysfs_devname_to_devno (lc->device); +if (devno <=0 )Quoted 8 lines
lib/loopdev.c static struct path_cxt *loopcxt_get_sysfs(struct loopdev_cxt *lc) · line 303
Quoted 5 lines
lib/loopdev.c +5
-0+return ; + +fprintf (stderr,"%s is lost, run \"mknod %s b %d %d\" to recover it.\n" , + lc->device, lc->device,major (devno),minor (devno)); +}The lib/loopdev.c code is also used in libmount, so it should not directly print anything on stderr. It's application business to print the warning. I have moved it to
losetup.c
Quoted 1 line
Should we patch mount to report that error?
Yes, but first we need improve how libmount works with errors. Now there is possible to work with errno only. The code that generates error messages has no access to the 'struct loopdev_cxt`.
I have added a TODO issue for this:
https://github.com/util-linux/util-linux/issues/2605
Karel
Signature · 2 lines
Karel Zak <[email protected]> http://karelzak.blogspot.com