[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] Add option -e to swapon and swapoff
From:       René Rhéaume <rene.rheaume () gmail ! com>
Date:       2014-12-07 17:01:53
Message-ID: CAPLjCyLSRFcYezpVbuaHyuHdCRfV71FqWSqnoh23o3g5k+vqxw () mail ! gmail ! com
[Download RAW message or body]

Hello,
I added the -e option to swapon and swapoff to match util-linux.
OpenRC on Linux requires this option in its swap init script. See
https://bugs.gentoo.org/show_bug.cgi?id=468598 for details.

There are three versions of the patch : for 1.20 and 1.21, for 1.22
and for the master branch.

["busybox-1.20-swaponoff-ifexists.patch" (text/x-patch)]

--- a/util-linux/swaponoff.c
+++ b/util-linux/swaponoff.c
@@ -8,19 +8,21 @@
  */
 
 //usage:#define swapon_trivial_usage
-//usage:       "[-a]" IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
+//usage:       "[-a] [-e]" IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
 //usage:#define swapon_full_usage "\n\n"
 //usage:       "Start swapping on DEVICE\n"
 //usage:     "\n	-a	Start swapping on all swap devices"
+//usage:     "\n	-e	Silently skip devices that do not exist"
 //usage:	IF_FEATURE_SWAPON_PRI(
 //usage:     "\n	-p PRI	Set swap device priority"
 //usage:	)
 //usage:
 //usage:#define swapoff_trivial_usage
-//usage:       "[-a] [DEVICE]"
+//usage:       "[-a] [-e] [DEVICE]"
 //usage:#define swapoff_full_usage "\n\n"
 //usage:       "Stop swapping on DEVICE\n"
 //usage:     "\n	-a	Stop swapping on all swap devices"
+//usage:     "\n	-e	Silently skip devices that do not exist"
 
 #include "libbb.h"
 #include <mntent.h>
@@ -49,13 +51,14 @@ struct globals {
 #endif
 #define INIT_G() do { } while (0)
 
-static int swap_enable_disable(char *device)
+static int swap_enable_disable(char *device, bool ifexists)
 {
 	int status;
 	struct stat st;
 
 	resolve_mount_spec(&device);
-	xstat(device, &st);
+	if (!ifexists)
+		xstat(device, &st);
 
 #if ENABLE_DESKTOP
 	/* test for holes */
@@ -70,14 +73,16 @@ static int swap_enable_disable(char *device)
 		status = swapoff(device);
 
 	if (status != 0) {
-		bb_simple_perror_msg(device);
-		return 1;
+		if ((!ifexists) || (errno != ENOENT)) {
+			bb_simple_perror_msg(device);
+			return 1;
+		}
 	}
 
 	return 0;
 }
 
-static int do_em_all(void)
+static int do_em_all(bool ifexists)
 {
 	struct mntent *m;
 	FILE *f;
@@ -95,7 +100,7 @@ static int do_em_all(void)
 			if (applet_name[5] != 'n'
 			 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
 			) {
-				err += swap_enable_disable(m->mnt_fsname);
+				err += swap_enable_disable(m->mnt_fsname, ifexists);
 			}
 		}
 	}
@@ -114,21 +119,21 @@ int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
 	INIT_G();
 
 #if !ENABLE_FEATURE_SWAPON_PRI
-	ret = getopt32(argv, "a");
+	ret = getopt32(argv, "ae");
 #else
 	if (applet_name[5] == 'n')
 		opt_complementary = "p+";
-	ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
+	ret = getopt32(argv, (applet_name[5] == 'n') ? "aep:" : "ae", &g_flags);
 
-	if (ret & 2) { // -p
+	if (ret & 4) { // -p
 		g_flags = SWAP_FLAG_PREFER |
 			((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
 		ret &= 1;
 	}
 #endif
 
-	if (ret /* & 1: not needed */) // -a
-		return do_em_all();
+	if (ret & 1) // -a
+		return do_em_all((ret & 2) > 0);
 
 	argv += optind;
 	if (!*argv)
@@ -136,7 +141,7 @@ int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
 
 	/* ret = 0; redundant */
 	do {
-		ret += swap_enable_disable(*argv);
+		ret += swap_enable_disable(*argv, ((ret & 2) > 0));
 	} while (*++argv);
 
 	return ret;

["busybox-1.22-swaponoff-ifexists.patch" (text/x-patch)]

--- a/util-linux/swaponoff.c
+++ b/util-linux/swaponoff.c
@@ -8,19 +8,21 @@
  */
 
 //usage:#define swapon_trivial_usage
-//usage:       "[-a]" IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
+//usage:       "[-a] [-e]" IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
 //usage:#define swapon_full_usage "\n\n"
 //usage:       "Start swapping on DEVICE\n"
 //usage:     "\n	-a	Start swapping on all swap devices"
+//usage:     "\n	-e	Silently skip devices that do not exist"
 //usage:	IF_FEATURE_SWAPON_PRI(
 //usage:     "\n	-p PRI	Set swap device priority"
 //usage:	)
 //usage:
 //usage:#define swapoff_trivial_usage
-//usage:       "[-a] [DEVICE]"
+//usage:       "[-a] [-e] [DEVICE]"
 //usage:#define swapoff_full_usage "\n\n"
 //usage:       "Stop swapping on DEVICE\n"
 //usage:     "\n	-a	Stop swapping on all swap devices"
+//usage:     "\n	-e	Silently skip devices that do not exist"
 
 #include "libbb.h"
 #include <mntent.h>
@@ -49,13 +51,14 @@ struct globals {
 #endif
 #define INIT_G() do { } while (0)
 
-static int swap_enable_disable(char *device)
+static int swap_enable_disable(char *device, bool ifexists)
 {
 	int status;
 	struct stat st;
 
 	resolve_mount_spec(&device);
-	xstat(device, &st);
+	if (!ifexists)
+		xstat(device, &st);
 
 #if ENABLE_DESKTOP
 	/* test for holes */
@@ -70,14 +73,16 @@ static int swap_enable_disable(char *device)
 		status = swapoff(device);
 
 	if (status != 0) {
-		bb_simple_perror_msg(device);
-		return 1;
+		if ((!ifexists) || (errno != ENOENT)) {
+			bb_simple_perror_msg(device);
+			return 1;
+		}
 	}
 
 	return 0;
 }
 
-static int do_em_all(void)
+static int do_em_all(bool ifexists)
 {
 	struct mntent *m;
 	FILE *f;
@@ -109,7 +114,7 @@ static int do_em_all(void)
 					}
 				}
 #endif
-				err += swap_enable_disable(m->mnt_fsname);
+				err += swap_enable_disable(m->mnt_fsname, ifexists);
 			}
 		}
 	}
@@ -128,21 +133,21 @@ int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
 	INIT_G();
 
 #if !ENABLE_FEATURE_SWAPON_PRI
-	ret = getopt32(argv, "a");
+	ret = getopt32(argv, "ae");
 #else
 	if (applet_name[5] == 'n')
 		opt_complementary = "p+";
-	ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
+	ret = getopt32(argv, (applet_name[5] == 'n') ? "aep:" : "ae", &g_flags);
 
-	if (ret & 2) { // -p
+	if (ret & 4) { // -p
 		g_flags = SWAP_FLAG_PREFER |
 			((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
 		ret &= 1;
 	}
 #endif
 
-	if (ret /* & 1: not needed */) // -a
-		return do_em_all();
+	if (ret & 1) // -a
+		return do_em_all((ret & 2) > 0);
 
 	argv += optind;
 	if (!*argv)
@@ -150,7 +155,7 @@ int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
 
 	/* ret = 0; redundant */
 	do {
-		ret += swap_enable_disable(*argv);
+		ret += swap_enable_disable(*argv, ((ret & 2) > 0));
 	} while (*++argv);
 
 	return ret;

["busybox-master-swaponoff-ifexists.patch" (text/x-patch)]

--- a/util-linux/swaponoff.c
+++ b/util-linux/swaponoff.c
@@ -8,7 +8,7 @@
  */
 
 //usage:#define swapon_trivial_usage
-//usage:       "[-a]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") \
IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]" +//usage:       "[-a] [-e]" \
IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " \
[DEVICE]"  //usage:#define swapon_full_usage "\n\n"
 //usage:       "Start swapping on DEVICE\n"
 //usage:     "\n	-a	Start swapping on all swap devices"
@@ -16,15 +16,17 @@
 //usage:     "\n	-d[POL]	Discard blocks at swapon (POL=once),"
 //usage:     "\n		as freed (POL=pages), or both (POL omitted)"
 //usage:	)
+//usage:     "\n	-e	Silently skip devices that do not exist"
 //usage:	IF_FEATURE_SWAPON_PRI(
 //usage:     "\n	-p PRI	Set swap device priority"
 //usage:	)
 //usage:
 //usage:#define swapoff_trivial_usage
-//usage:       "[-a] [DEVICE]"
+//usage:       "[-a] [-e] [DEVICE]"
 //usage:#define swapoff_full_usage "\n\n"
 //usage:       "Stop swapping on DEVICE\n"
 //usage:     "\n	-a	Stop swapping on all swap devices"
+//usage:     "\n	-e	Silently skip devices that do not exist"
 
 #include "libbb.h"
 #include <mntent.h>
@@ -77,15 +79,18 @@ struct globals {
 /* Command line options */
 enum {
 	OPTBIT_a,                              /* -a all      */
+	OPTBIT_e,                              /* -e ifexists */
 	IF_FEATURE_SWAPON_DISCARD( OPTBIT_d ,) /* -d discard  */
 	IF_FEATURE_SWAPON_PRI    ( OPTBIT_p ,) /* -p priority */
 	OPT_a = 1 << OPTBIT_a,
+	OPT_e = 1 << OPTBIT_e,
 	OPT_d = IF_FEATURE_SWAPON_DISCARD((1 << OPTBIT_d)) + 0,
 	OPT_p = IF_FEATURE_SWAPON_PRI    ((1 << OPTBIT_p)) + 0,
 };
 
 #define OPT_ALL      (option_mask32 & OPT_a)
 #define OPT_DISCARD  (option_mask32 & OPT_d)
+#define OPT_IFEXISTS (option_mask32 & OPT_e)
 #define OPT_PRIO     (option_mask32 & OPT_p)
 
 static int swap_enable_disable(char *device)
@@ -95,6 +100,8 @@ static int swap_enable_disable(char *device)
 	struct stat st;
 
 	resolve_mount_spec(&device);
+	if (!OPT_IFEXISTS)
+		xstat(device, &st);
 
 	if (do_swapoff) {
 		err = swapoff(device);
@@ -112,7 +119,8 @@ static int swap_enable_disable(char *device)
 			}
 			err = swapon(device, g_flags);
 			/* Don't complain on swapon -a if device is already in use */
-			quiet = (OPT_ALL && errno == EBUSY);
+			/* Don't complain if file does not exist with -e option */
+			quiet = (OPT_ALL && errno == EBUSY) || (OPT_IFEXISTS && errno == ENOENT);
 		}
 	}
 
@@ -229,7 +237,7 @@ static int do_all_in_proc_swaps(void)
 	return err;
 }
 
-#define OPTSTR_SWAPON "a" \
+#define OPTSTR_SWAPON "ae" \
 	IF_FEATURE_SWAPON_DISCARD("d::") \
 	IF_FEATURE_SWAPON_PRI("p:")
 
@@ -242,7 +250,7 @@ int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
 
 	INIT_G();
 
-	getopt32(argv, do_swapoff ? "a" : OPTSTR_SWAPON
+	getopt32(argv, do_swapoff ? "ae" : OPTSTR_SWAPON
 			IF_FEATURE_SWAPON_DISCARD(, &discard)
 			IF_FEATURE_SWAPON_PRI(, &prio)
 	);



_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic