aboutsummaryrefslogtreecommitdiff
path: root/toys/other/mkswap.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-08-25 14:25:22 -0500
committerRob Landley <rob@landley.net>2012-08-25 14:25:22 -0500
commit3a9241add947cb6d24b5de7a8927517426a78795 (patch)
treed122ab6570439cd6b17c7d73ed8d4e085e0b8a95 /toys/other/mkswap.c
parent689f095bc976417bf50810fe59a3b3ac32b21105 (diff)
downloadtoybox-3a9241add947cb6d24b5de7a8927517426a78795.tar.gz
Move commands into "posix", "lsb", and "other" menus/directories.
Diffstat (limited to 'toys/other/mkswap.c')
-rw-r--r--toys/other/mkswap.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/toys/other/mkswap.c b/toys/other/mkswap.c
new file mode 100644
index 00000000..87c1550f
--- /dev/null
+++ b/toys/other/mkswap.c
@@ -0,0 +1,42 @@
+/* vi: set sw=4 ts=4:
+ *
+ * mkswap.c - Format swap device.
+ *
+ * Copyright 2009 Rob Landley <rob@landley.net>
+ *
+ * Not in SUSv4.
+
+USE_MKSWAP(NEWTOY(mkswap, "<1>1", TOYFLAG_SBIN))
+
+config MKSWAP
+ bool "mkswap"
+ default y
+ help
+ usage: mkswap DEVICE
+
+ Sets up a Linux swap area on a device or file.
+*/
+
+#include "toys.h"
+
+void mkswap_main(void)
+{
+ int fd = xopen(*toys.optargs, O_RDWR), pagesize = sysconf(_SC_PAGE_SIZE);
+ off_t len = fdlength(fd);
+ unsigned int pages = (len/pagesize)-1, *swap = (unsigned int *)toybuf;
+
+ // Write header. Note that older kernel versions checked signature
+ // on disk (not in cache) during swapon, so sync after writing.
+
+ swap[0] = 1;
+ swap[1] = pages;
+ xlseek(fd, 1024, SEEK_SET);
+ xwrite(fd, swap, 129*sizeof(unsigned int));
+ xlseek(fd, pagesize-10, SEEK_SET);
+ xwrite(fd, "SWAPSPACE2", 10);
+ fsync(fd);
+
+ if (CFG_TOYBOX_FREE) close(fd);
+
+ printf("Swapspace size: %luk\n", pages*(unsigned long)(pagesize/1024));
+}