Follow-up to 57c97d8, issue #224: add swapon/swapoff fstab wrappers

The swapon and swapoff commands do not support a custom fstab, they
assume /etc/fstab is the only true source of swap devices and files.

This change adds rudimentary (!) support for figuring out any swap
device or file to use from any finit.fstab.  Please note, options are
not supported at the moment.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2022-04-26 22:57:22 +02:00
1 parent 26409bc860
commit 605c489937
2 files changed
+53 -4

No files matched your search

+28 -2
View File
@@ -70,6 +70,9 @@
*/
#include <dirent.h>
#ifdef HAVE_MNTENT_H
#include <mntent.h>
#endif
#include <string.h> /* strerror() */
#include <sched.h>
#include <sys/reboot.h>
@@ -166,6 +169,30 @@ void mdadm_wait(void);
void unmount_tmpfs(void);
void unmount_regular(void);
static void fs_swapoff(void)
{
struct mntent *mnt;
char cmd[256];
FILE *fp;
if (!whichp("swapoff"))
return;
fp = setmntent(fstab, "r");
if (!fp)
return;
while ((mnt = getmntent(fp))) {
if (strcmp(mnt->mnt_type, MNTTYPE_SWAP))
continue;
snprintf(cmd, sizeof(cmd), "swapoff %s", mnt->mnt_fsname);
run_interactive(cmd, "Disabling swap %s", mnt->mnt_fsname);
}
endmntent(fp);
}
/*
* Kernel threads have no cmdline so fgets() returns NULL for them. We
* also skip "special" processes, e.g. mdadm/mdmon or watchdogd that must
@@ -323,8 +350,7 @@ void do_shutdown(shutop_t op)
/* Unmount any tmpfs before unmounting swap ... */
unmount_tmpfs();
if (whichp("swapoff"))
run_interactive("swapoff -a", "Disabling system swap");
fs_swapoff();
/* ... unmount remaining regular file systems. */
unmount_regular();