1 /* mkswap.c - Format swap device.
2  *
3  * Copyright 2009 Rob Landley <rob@landley.net>
4 
5 USE_MKSWAP(NEWTOY(mkswap, "<1>1", TOYFLAG_SBIN))
6 
7 config MKSWAP
8   bool "mkswap"
9   default y
10   help
11     usage: mkswap DEVICE
12 
13     Sets up a Linux swap area on a device or file.
14 */
15 
16 #include "toys.h"
17 
mkswap_main(void)18 void mkswap_main(void)
19 {
20   int fd = xopen(*toys.optargs, O_RDWR), pagesize = sysconf(_SC_PAGE_SIZE);
21   off_t len = fdlength(fd);
22   unsigned int pages = (len/pagesize)-1, *swap = (unsigned int *)toybuf;
23 
24   // Write header. Note that older kernel versions checked signature
25   // on disk (not in cache) during swapon, so sync after writing.
26 
27   swap[0] = 1;
28   swap[1] = pages;
29   xlseek(fd, 1024, SEEK_SET);
30   xwrite(fd, swap, 129*sizeof(unsigned int));
31   xlseek(fd, pagesize-10, SEEK_SET);
32   xwrite(fd, "SWAPSPACE2", 10);
33   fsync(fd);
34 
35   if (CFG_TOYBOX_FREE) close(fd);
36 
37   printf("Swapspace size: %luk\n", pages*(unsigned long)(pagesize/1024));
38 }
39