1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#define FOR_mkswap
17#include "toys.h"
18
19GLOBALS(
20 char *L;
21)
22
23void 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
31
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