7baa3ecfb4ecef55b9ddd87eaa2f7d6ceffa5a5b
[oweals/busybox.git] / util-linux / mkswap.c
1 /* vi: set sw=4 ts=4: */
2 /* mkswap.c - format swap device (Linux v1 only)
3  *
4  * Copyright 2006 Rob Landley <rob@landley.net>
5  *
6  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
7  */
8
9 #include "busybox.h"
10
11 int mkswap_main(int argc, char *argv[])
12 {
13         int fd, pagesize;
14         off_t len;
15         unsigned int hdr[129];
16
17         // No options supported.
18
19         if (argc != 2) bb_show_usage();
20
21         // Figure out how big the device is and announce our intentions.
22
23         fd = xopen(argv[1], O_RDWR);
24         len = fdlength(fd);
25         pagesize = getpagesize();
26         printf("Setting up swapspace version 1, size = %"OFF_FMT"d bytes\n",
27                         len - pagesize);
28
29         // Make a header.
30
31         memset(hdr, 0, sizeof(hdr));
32         hdr[0] = 1;
33         hdr[1] = (len / pagesize) - 1;
34
35         // Write the header.  Sync to disk because some kernel versions check
36         // signature on disk (not in cache) during swapon.
37
38         xlseek(fd, 1024, SEEK_SET);
39         xwrite(fd, hdr, sizeof(hdr));
40         xlseek(fd, pagesize-10, SEEK_SET);
41         xwrite(fd, "SWAPSPACE2", 10);
42         fsync(fd);
43
44         if (ENABLE_FEATURE_CLEAN_UP) close(fd);
45
46         return 0;
47 }