This is a trivial patch to add a -f option to losetup. losetup -f shows the first free loop device. After the patch executable size is not changed in two of three arch that I've tested:
AMD64: unchanged i386: unchanged i386: +4060bytes (different version of gcc) The code is incorrect if 1) there are less than 10 loop devices and 2) they are all busy (but I don't know loop devices enough to correct...). I use a custom version of ubuntu initrd using busybox 1.6.1 for nearly all executables (resulting in a 700kb smaller initrd.gz). losetup -f is used during the boot, so that it'd be better if it got included in busybox. Untested suggestion to shave a couple of bytes from losetup.c: char dev[sizeof(LOOP_NAME"0")] = LOOP_NAME"0"; char c; for (c = '0'; c <= '9'; ++c) { char *s; dev[sizeof(LOOP_NAME"0")-2] = c; s = query_loop(dev); can be modified into: char dev[sizeof(LOOP_NAME"0")] = LOOP_NAME"0"; for (; dev[sizeof(dev)-2] <= '9'; ++dev[sizeof(dev)-2]) { char *s; s = query_loop(dev); (stylewise I prefer char dev[] = LOOP_NAME"0"; but I don't know whether all compilers would accept it and produce correct code). Cheers, Loïc
diff -c -r busybox-1.6.1/include/usage.h busybox-1.6.1.loic/include/usage.h *** busybox-1.6.1/include/usage.h Sat Jun 30 17:06:45 2007 --- busybox-1.6.1.loic/include/usage.h Thu Sep 20 22:41:43 2007 *************** *** 1845,1863 **** " -f Output data as the log grows" #define losetup_trivial_usage \ ! "[-o OFFSET] [-d] LOOPDEVICE [FILE]]" #define losetup_full_usage \ "(Dis)associate LOOPDEVICE with FILE, or display current associations" \ "\n\nOptions:\n" \ " -d Disassociate LOOPDEVICE\n" \ ! " -o OFFSET Start OFFSET bytes into FILE" #define losetup_notes_usage \ "No arguments will display all current associations.\n" \ "One argument (losetup /dev/loop1) will display the current association\n" \ "(if any), or disassociate it (with -d). The display shows the offset\n" \ "and filename of the file the loop device is currently bound to.\n\n" \ "Two arguments (losetup /dev/loop1 file.img) create a new association,\n" \ ! "with an optional offset (-o 12345). Encryption is not yet supported.\n\n" #define ls_trivial_usage \ "[-1Aa" USE_FEATURE_LS_TIMESTAMPS("c") "Cd" \ --- 1845,1865 ---- " -f Output data as the log grows" #define losetup_trivial_usage \ ! "{[-o OFFSET] [-d] LOOPDEVICE [FILE] | -f }" #define losetup_full_usage \ "(Dis)associate LOOPDEVICE with FILE, or display current associations" \ "\n\nOptions:\n" \ " -d Disassociate LOOPDEVICE\n" \ ! " -o OFFSET Start OFFSET bytes into FILE\n" \ ! " -f Show first free loop device" #define losetup_notes_usage \ "No arguments will display all current associations.\n" \ "One argument (losetup /dev/loop1) will display the current association\n" \ "(if any), or disassociate it (with -d). The display shows the offset\n" \ "and filename of the file the loop device is currently bound to.\n\n" \ "Two arguments (losetup /dev/loop1 file.img) create a new association,\n" \ ! "with an optional offset (-o 12345). Encryption is not yet supported.\n" \ ! "losetup -f will show the first loop free loop device\n\n" #define ls_trivial_usage \ "[-1Aa" USE_FEATURE_LS_TIMESTAMPS("c") "Cd" \ diff -c -r busybox-1.6.1/util-linux/losetup.c busybox-1.6.1.loic/util-linux/losetup.c *** busybox-1.6.1/util-linux/losetup.c Sat Jun 30 17:06:48 2007 --- busybox-1.6.1.loic/util-linux/losetup.c Thu Sep 20 22:43:22 2007 *************** *** 18,28 **** char *opt_o; unsigned long long offset = 0; ! opt = getopt32(argc, argv, "do:", &opt_o); argc -= optind; argv += optind; ! if (opt == 0x3) // -d + -o (illegal) bb_show_usage(); if (opt == 0x1) { // -d --- 18,28 ---- char *opt_o; unsigned long long offset = 0; ! opt = getopt32(argc, argv, "do:f", &opt_o); argc -= optind; argv += optind; ! if (opt == 0x3 || opt == 0x5 || opt == 0x6) // two options (illegal) bb_show_usage(); if (opt == 0x1) { // -d *************** *** 37,43 **** if (opt == 0x2) // -o offset = xatoull(opt_o); ! /* -o or no option */ if (argc == 2) { if (set_loop(&argv[0], argv[1], offset) < 0) --- 37,46 ---- if (opt == 0x2) // -o offset = xatoull(opt_o); ! if (opt == 0x4 && argc) // -f does not take any argument ! bb_show_usage(); ! ! /* -o, -f or no option */ if (argc == 2) { if (set_loop(&argv[0], argv[1], offset) < 0) *************** *** 56,66 **** char *s; dev[sizeof(LOOP_NAME"0")-2] = c; s = query_loop(dev); ! if (s) { printf("%s: %s\n", dev, s); if (ENABLE_FEATURE_CLEAN_UP) free(s); } } } return EXIT_SUCCESS; --- 59,74 ---- char *s; dev[sizeof(LOOP_NAME"0")-2] = c; s = query_loop(dev); ! if (s && opt != 0x4) { printf("%s: %s\n", dev, s); if (ENABLE_FEATURE_CLEAN_UP) free(s); } + else if (!s && opt == 0x4) + { + printf("%s\n", dev); + return EXIT_SUCCESS; + } } } return EXIT_SUCCESS;
_______________________________________________ busybox mailing list busybox@busybox.net http://busybox.net/cgi-bin/mailman/listinfo/busybox