From: Karel Zak Date: Mon, 16 Jan 2023 12:47:36 +0000 (+0100) Subject: swapon: add --fstab command line option X-Git-Tag: v2.39-rc1~161 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a5a9b54429e1959e89801d7bdedd7651d3da1214;p=thirdparty%2Futil-linux.git swapon: add --fstab command line option 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 --- diff --git a/bash-completion/swapon b/bash-completion/swapon index a6b6f09211..093710590c 100644 --- a/bash-completion/swapon +++ b/bash-completion/swapon @@ -59,6 +59,7 @@ _swapon_module() --discard --ifexists --fixpgsz + --fstab --priority --summary --show diff --git a/sys-utils/swapoff.c b/sys-utils/swapoff.c index 7bfb90a3e7..1ddfcda00d 100644 --- a/sys-utils/swapoff.c +++ b/sys-utils/swapoff.c @@ -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) { diff --git a/sys-utils/swapon-common.c b/sys-utils/swapon-common.c index dd1593d492..bffedecfe8 100644 --- a/sys-utils/swapon-common.c +++ b/sys-utils/swapon-common.c @@ -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; } diff --git a/sys-utils/swapon-common.h b/sys-utils/swapon-common.h index d1b679f2c2..a13c0de75b 100644 --- a/sys-utils/swapon-common.h +++ b/sys-utils/swapon-common.h @@ -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); diff --git a/sys-utils/swapon.8.adoc b/sys-utils/swapon.8.adoc index 01de11d39f..d5057ddd18 100644 --- a/sys-utils/swapon.8.adoc +++ b/sys-utils/swapon.8.adoc @@ -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: diff --git a/sys-utils/swapon.c b/sys-utils/swapon.c index 76c5cac51b..87355491d5 100644 --- a/sys-utils/swapon.c +++ b/sys-utils/swapon.c @@ -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 comma-separated list of swap options\n"), out); fputs(_(" -p, --priority specify the priority of the swap device\n"), out); fputs(_(" -s, --summary display summary about used swap devices (DEPRECATED)\n"), out); + fputs(_(" -T, --fstab alternative file to /etc/fstab\n"), out); fputs(_(" --show[=] 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);