aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2009-01-18 16:19:25 -0600
committerRob Landley <rob@landley.net>2009-01-18 16:19:25 -0600
commit52476716ad2bff9805e5749bf2c2a1f9d3512ec9 (patch)
tree23552e2460dde4b509c22e22c9190bc4f65aeb2f
parentd3e61fc8da81c38a82a02379397f513e99ba5249 (diff)
downloadtoybox-52476716ad2bff9805e5749bf2c2a1f9d3512ec9.tar.gz
Add mkswap.
-rw-r--r--lib/lib.c10
-rw-r--r--lib/lib.h1
-rw-r--r--toys/mkswap.c42
3 files changed, 53 insertions, 0 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 89c8781b..a5bf4755 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -266,6 +266,16 @@ void xwrite(int fd, void *buf, size_t len)
if (len != writeall(fd, buf, len)) perror_exit("xwrite");
}
+// Die if lseek fails, probably due to being called on a pipe.
+
+off_t xlseek(int fd, off_t offset, int whence)
+{
+ offset = lseek(fd, offset, whence);
+ if (offset<0) perror_exit("lseek");
+
+ return offset;
+}
+
char *xgetcwd(void)
{
char *buf = getcwd(NULL, 0);
diff --git a/lib/lib.h b/lib/lib.h
index a473b546..61e80cf9 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -73,6 +73,7 @@ ssize_t writeall(int fd, void *buf, size_t len);
size_t xread(int fd, void *buf, size_t len);
void xreadall(int fd, void *buf, size_t len);
void xwrite(int fd, void *buf, size_t len);
+off_t xlseek(int fd, off_t offset, int whence);
char *readfile(char *name);
char *xreadfile(char *name);
char *xgetcwd(void);
diff --git a/toys/mkswap.c b/toys/mkswap.c
new file mode 100644
index 00000000..f902efeb
--- /dev/null
+++ b/toys/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 SUSv3.
+
+USE_MKSWAP(NEWTOY(mkswap, "<1>2", TOYFLAG_SBIN))
+
+config MKSWAP
+ bool "mkswap"
+ default y
+ help
+ usage: mkswap DEVICE
+
+ Format a Linux v1 swap device.
+*/
+
+#include "toys.h"
+
+void mkswap_main(void)
+{
+ int fd = xopen(*toys.optargs, O_RDWR), pagesize = getpagesize();
+ 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));
+}