Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / busybox / util-linux / mkswap.c
blobf9c28b498fe7523eb97a6cf6bf677cf19b7e4531
1 /* vi: set sw=4 ts=4: */
2 /* mkswap.c - format swap device (Linux v1 only)
4 * Copyright 2006 Rob Landley <rob@landley.net>
6 * Licensed under GPLv2, see file LICENSE in this source tree.
7 */
9 //usage:#define mkswap_trivial_usage
10 //usage: "[-L LBL] BLOCKDEV [KBYTES]"
11 //usage:#define mkswap_full_usage "\n\n"
12 //usage: "Prepare BLOCKDEV to be used as swap partition\n"
13 //usage: "\n -L LBL Label"
15 #include "libbb.h"
17 #if ENABLE_SELINUX
18 static void mkswap_selinux_setcontext(int fd, const char *path)
20 struct stat stbuf;
22 if (!is_selinux_enabled())
23 return;
25 xfstat(fd, &stbuf, path);
26 if (S_ISREG(stbuf.st_mode)) {
27 security_context_t newcon;
28 security_context_t oldcon = NULL;
29 context_t context;
31 if (fgetfilecon(fd, &oldcon) < 0) {
32 if (errno != ENODATA)
33 goto error;
34 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
35 goto error;
37 context = context_new(oldcon);
38 if (!context || context_type_set(context, "swapfile_t"))
39 goto error;
40 newcon = context_str(context);
41 if (!newcon)
42 goto error;
43 /* fsetfilecon_raw is hidden */
44 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
45 goto error;
46 if (ENABLE_FEATURE_CLEAN_UP) {
47 context_free(context);
48 freecon(oldcon);
51 return;
52 error:
53 bb_perror_msg_and_die("SELinux relabeling failed");
55 #else
56 # define mkswap_selinux_setcontext(fd, path) ((void)0)
57 #endif
59 /* from Linux 2.6.23 */
61 * Magic header for a swap area. ... Note that the first
62 * kilobyte is reserved for boot loader or disk label stuff.
64 struct swap_header_v1 {
65 /* char bootbits[1024]; Space for disklabel etc. */
66 uint32_t version; /* second kbyte, word 0 */
67 uint32_t last_page; /* 1 */
68 uint32_t nr_badpages; /* 2 */
69 char sws_uuid[16]; /* 3,4,5,6 */
70 char sws_volume[16]; /* 7,8,9,10 */
71 uint32_t padding[117]; /* 11..127 */
72 uint32_t badpages[1]; /* 128 */
73 /* total 129 32-bit words in 2nd kilobyte */
74 } FIX_ALIASING;
76 #define NWORDS 129
77 #define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
79 struct BUG_sizes {
80 char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1];
81 char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
84 /* Stored without terminating NUL */
85 static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
87 int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
88 int mkswap_main(int argc UNUSED_PARAM, char **argv)
90 int fd;
91 unsigned pagesize;
92 off_t len;
93 const char *label = "";
95 opt_complementary = "-1"; /* at least one param */
96 /* TODO: -p PAGESZ, -U UUID */
97 getopt32(argv, "L:", &label);
98 argv += optind;
100 fd = xopen(argv[0], O_WRONLY);
102 /* Figure out how big the device is */
103 len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1);
104 pagesize = getpagesize();
105 len -= pagesize;
107 /* Announce our intentions */
108 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
109 mkswap_selinux_setcontext(fd, argv[0]);
111 /* hdr is zero-filled so far. Clear the first kbyte, or else
112 * mkswap-ing former FAT partition does NOT erase its signature.
114 * util-linux-ng 2.17.2 claims to erase it only if it does not see
115 * a partition table and is not run on whole disk. -f forces it.
117 xwrite(fd, hdr, 1024);
119 /* Fill the header. */
120 hdr->version = 1;
121 hdr->last_page = (uoff_t)len / pagesize;
123 if (ENABLE_FEATURE_MKSWAP_UUID) {
124 char uuid_string[37];
125 generate_uuid((void*)hdr->sws_uuid);
126 printf("UUID=%s\n", unparse_uuid((uint8_t *)hdr->sws_uuid, uuid_string));
128 safe_strncpy(hdr->sws_volume, label, 16);
130 /* Write the header. Sync to disk because some kernel versions check
131 * signature on disk (not in cache) during swapon. */
132 xwrite(fd, hdr, NWORDS * 4);
133 xlseek(fd, pagesize - 10, SEEK_SET);
134 xwrite(fd, SWAPSPACE2, 10);
135 fsync(fd);
137 if (ENABLE_FEATURE_CLEAN_UP)
138 close(fd);
140 return 0;