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