fixes, fully translated tomato, with english dictionary and Polish translation
[tomato.git] / release / src / router / busybox / util-linux / mkswap.c
blobfea6eaebbc0036edf6be29903d0bd82d9aa5f24b
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 */
8 #include "libbb.h"
10 #if ENABLE_SELINUX
11 static void mkswap_selinux_setcontext(int fd, const char *path)
13 struct stat stbuf;
15 if (!is_selinux_enabled())
16 return;
18 xfstat(fd, &stbuf, path);
19 if (S_ISREG(stbuf.st_mode)) {
20 security_context_t newcon;
21 security_context_t oldcon = NULL;
22 context_t context;
24 if (fgetfilecon(fd, &oldcon) < 0) {
25 if (errno != ENODATA)
26 goto error;
27 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
28 goto error;
30 context = context_new(oldcon);
31 if (!context || context_type_set(context, "swapfile_t"))
32 goto error;
33 newcon = context_str(context);
34 if (!newcon)
35 goto error;
36 /* fsetfilecon_raw is hidden */
37 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
38 goto error;
39 if (ENABLE_FEATURE_CLEAN_UP) {
40 context_free(context);
41 freecon(oldcon);
44 return;
45 error:
46 bb_perror_msg_and_die("SELinux relabeling failed");
48 #else
49 # define mkswap_selinux_setcontext(fd, path) ((void)0)
50 #endif
52 /* from Linux 2.6.23 */
54 * Magic header for a swap area. ... Note that the first
55 * kilobyte is reserved for boot loader or disk label stuff.
57 struct swap_header_v1 {
58 /* char bootbits[1024]; Space for disklabel etc. */
59 uint32_t version; /* second kbyte, word 0 */
60 uint32_t last_page; /* 1 */
61 uint32_t nr_badpages; /* 2 */
62 char sws_uuid[16]; /* 3,4,5,6 */
63 char sws_volume[16]; /* 7,8,9,10 */
64 uint32_t padding[117]; /* 11..127 */
65 uint32_t badpages[1]; /* 128 */
66 /* total 129 32-bit words in 2nd kilobyte */
67 } FIX_ALIASING;
69 #define NWORDS 129
70 #define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
72 struct BUG_sizes {
73 char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1];
74 char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
77 /* Stored without terminating NUL */
78 static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
80 int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
81 int mkswap_main(int argc UNUSED_PARAM, char **argv)
83 int fd;
84 unsigned pagesize;
85 off_t len;
86 const char *label = "";
88 opt_complementary = "-1"; /* at least one param */
89 /* TODO: -p PAGESZ, -U UUID */
90 getopt32(argv, "L:", &label);
91 argv += optind;
93 fd = xopen(argv[0], O_WRONLY);
95 /* Figure out how big the device is */
96 len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1);
97 pagesize = getpagesize();
98 len -= pagesize;
100 /* Announce our intentions */
101 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
102 mkswap_selinux_setcontext(fd, argv[0]);
104 /* hdr is zero-filled so far. Clear the first kbyte, or else
105 * mkswap-ing former FAT partition does NOT erase its signature.
107 * util-linux-ng 2.17.2 claims to erase it only if it does not see
108 * a partition table and is not run on whole disk. -f forces it.
110 xwrite(fd, hdr, 1024);
112 /* Fill the header. */
113 hdr->version = 1;
114 hdr->last_page = (uoff_t)len / pagesize;
116 if (ENABLE_FEATURE_MKSWAP_UUID) {
117 char uuid_string[37];
118 generate_uuid((void*)hdr->sws_uuid);
119 printf("UUID=%s\n", unparse_uuid((uint8_t *)hdr->sws_uuid, uuid_string));
121 safe_strncpy(hdr->sws_volume, label, 16);
123 /* Write the header. Sync to disk because some kernel versions check
124 * signature on disk (not in cache) during swapon. */
125 xwrite(fd, hdr, NWORDS * 4);
126 xlseek(fd, pagesize - 10, SEEK_SET);
127 xwrite(fd, SWAPSPACE2, 10);
128 fsync(fd);
130 if (ENABLE_FEATURE_CLEAN_UP)
131 close(fd);
133 return 0;