]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
swapon: add --fstab command line option
authorKarel Zak <kzak@redhat.com>
Mon, 16 Jan 2023 12:47:36 +0000 (13:47 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 16 Jan 2023 12:47:36 +0000 (13:47 +0100)
Let's add --fstab mount option for compatibility with mount(8).

Addresses: https://github.com/util-linux/util-linux/issues/2017
Signed-off-by: Karel Zak <kzak@redhat.com>
bash-completion/swapon
sys-utils/swapoff.c
sys-utils/swapon-common.c
sys-utils/swapon-common.h
sys-utils/swapon.8.adoc
sys-utils/swapon.c

index a6b6f092119f91677ffa2d2b4cc7394230389495..093710590c9fac9cb199fae67daca5ae751947bc 100644 (file)
@@ -59,6 +59,7 @@ _swapon_module()
                                --discard
                                --ifexists
                                --fixpgsz
+                               --fstab
                                --priority
                                --summary
                                --show
index 7bfb90a3e7810514fb8666360f5c079dde3e61fa..1ddfcda00d04cd242a067ced761add20c6878022 100644 (file)
@@ -192,7 +192,7 @@ static int swapoff_all(void)
         * already, so errors are not bad.  Doing swapoff -a twice should not
         * give error messages.
         */
-       tb = get_fstab();
+       tb = get_fstab(NULL);
        mnt_reset_iter(itr, MNT_ITER_FORWARD);
 
        while (tb && mnt_table_find_next_fs(tb, itr, match_swap, NULL, &fs) == 0) {
index dd1593d4927ca1a483a2146538fd734508f6a18e..bffedecfe82fe2fe1575ebf0790d0ba872e9c2f4 100644 (file)
@@ -20,7 +20,7 @@ static int table_parser_errcb(struct libmnt_table *tb __attribute__((__unused__)
        return 1;
 }
 
-struct libmnt_table *get_fstab(void)
+struct libmnt_table *get_fstab(const char *filename)
 {
        if (!fstab) {
                fstab = mnt_new_table();
@@ -28,7 +28,7 @@ struct libmnt_table *get_fstab(void)
                        return NULL;
                mnt_table_set_parser_errcb(fstab, table_parser_errcb);
                mnt_table_set_cache(fstab, mntcache);
-               if (mnt_table_parse_fstab(fstab, NULL) != 0)
+               if (mnt_table_parse_fstab(fstab, filename) != 0)
                        return NULL;
        }
 
index d1b679f2c2ccd91820361e7df56804ca6bc22c8b..a13c0de75baa8f1f63d23194e04d1107f98b22c8 100644 (file)
@@ -5,7 +5,7 @@
 
 extern struct libmnt_cache *mntcache;
 
-extern struct libmnt_table *get_fstab(void);
+extern struct libmnt_table *get_fstab(const char *filename);
 extern struct libmnt_table *get_swaps(void);
 extern void free_tables(void);
 
index 01de11d39f580663fd4337bda1831626128aa6e9..d5057ddd18da47730475c22138d898767bbf858b 100644 (file)
@@ -31,6 +31,9 @@ Calls to *swapon* normally occur in the system boot scripts making all swap devi
 *-a*, *--all*::
 All devices marked as "swap" in _/etc/fstab_ are made available, except for those with the "noauto" option. Devices that are already being used as swap are silently skipped.
 
+*-T*, *--fstab* _path_::
+Specifies an alternative fstab file for compatibility with *mount(8)*. If _path_ is a directory, then the files in the directory are sorted by strverscmp(3); files that start with "." or without an .fstab extension are ignored. The option can be specified more than once. This option is mostly designed for initramfs or chroot scripts where additional configuration is specified beyond standard system configuration.
+
 *-d*, *--discard*[**=**__policy__]::
 Enable swap discards, if the swap backing device supports the discard or trim operation. This may improve performance on some Solid State Devices, but often it does not. The option allows one to select between two available swap discard policies:
 
index 76c5cac51bd05d0cb4d958aee2b3dc4c5ae144b2..87355491d5cd2408f1d6f7f24612e851706ebf25 100644 (file)
@@ -735,9 +735,9 @@ static int parse_options(struct swap_prop *props, const char *options)
 }
 
 
-static int swapon_all(struct swapon_ctl *ctl)
+static int swapon_all(struct swapon_ctl *ctl, const char *filename)
 {
-       struct libmnt_table *tb = get_fstab();
+       struct libmnt_table *tb = get_fstab(filename);
        struct libmnt_iter *itr;
        struct libmnt_fs *fs;
        int status = 0;
@@ -817,6 +817,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -o, --options <list>     comma-separated list of swap options\n"), out);
        fputs(_(" -p, --priority <prio>    specify the priority of the swap device\n"), out);
        fputs(_(" -s, --summary            display summary about used swap devices (DEPRECATED)\n"), out);
+       fputs(_(" -T, --fstab <path>       alternative file to /etc/fstab\n"), out);
        fputs(_("     --show[=<columns>]   display summary in definable table\n"), out);
        fputs(_("     --noheadings         don't print table heading (with --show)\n"), out);
        fputs(_("     --raw                use the raw output format (with --show)\n"), out);
@@ -853,7 +854,7 @@ int main(int argc, char *argv[])
 {
        int status = 0, c;
        size_t i;
-       char *options = NULL;
+       char *options = NULL, *fstab_filename = NULL;
 
        enum {
                BYTES_OPTION = CHAR_MAX + 1,
@@ -879,6 +880,7 @@ int main(int argc, char *argv[])
                { "noheadings", no_argument,       NULL, NOHEADINGS_OPTION },
                { "raw",        no_argument,       NULL, RAW_OPTION        },
                { "bytes",      no_argument,       NULL, BYTES_OPTION      },
+               { "fstab",      required_argument, NULL, 'T'               },
                { NULL, 0, NULL, 0 }
        };
 
@@ -904,7 +906,7 @@ int main(int argc, char *argv[])
        mnt_init_debug(0);
        mntcache = mnt_new_cache();
 
-       while ((c = getopt_long(argc, argv, "ahd::efo:p:svVL:U:",
+       while ((c = getopt_long(argc, argv, "ahd::efo:p:svVL:U:T:",
                                long_opts, NULL)) != -1) {
 
                err_exclusive_options(c, long_opts, excl, excl_st);
@@ -926,6 +928,9 @@ int main(int argc, char *argv[])
                case 'U':
                        add_uuid(optarg);
                        break;
+               case 'T':
+                       fstab_filename = optarg;
+                       break;
                case 'd':
                        ctl.props.discard |= SWAP_FLAG_DISCARD;
                        if (optarg) {
@@ -1008,7 +1013,7 @@ int main(int argc, char *argv[])
        }
 
        if (ctl.all)
-               status |= swapon_all(&ctl);
+               status |= swapon_all(&ctl, fstab_filename);
 
        if (options)
                parse_options(&ctl.props, options);