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 Set 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 #ifdef TOYBOX_OH_ADAPT
27 if (fd < 0) {
28 return;
29 }
30 #endif
31 off_t len = fdlength(fd);
32 unsigned int pages = (len/pagesize)-1, *swap = (unsigned int *)toybuf;
33 char *label = (char *)(swap+7), *uuid = (char *)(swap+3);
34
35 // Write header. Note that older kernel versions checked signature
36 // on disk (not in cache) during swapon, so sync after writing.
37
38 swap[0] = 1;
39 swap[1] = pages;
40 xlseek(fd, 1024, SEEK_SET);
41 create_uuid(uuid);
42 if (TT.L) strncpy(label, TT.L, 15);
43 xwrite(fd, swap, 129*sizeof(unsigned int));
44 xlseek(fd, pagesize-10, SEEK_SET);
45 xwrite(fd, "SWAPSPACE2", 10);
46 fsync(fd);
47
48 if (CFG_TOYBOX_FREE) close(fd);
49
50 if (TT.L) sprintf(toybuf, ", LABEL=%s", label);
51 else *toybuf = 0;
52 printf("Swapspace size: %luk%s, UUID=%s\n",
53 pages*(unsigned long)(pagesize/1024),
54 toybuf, show_uuid(uuid));
55 }
56