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 GPLv2, see file LICENSE in this source tree.
|
---|
7 | */
|
---|
8 | #include "libbb.h"
|
---|
9 |
|
---|
10 | #if ENABLE_SELINUX
|
---|
11 | static void mkswap_selinux_setcontext(int fd, const char *path)
|
---|
12 | {
|
---|
13 | struct stat stbuf;
|
---|
14 |
|
---|
15 | if (!is_selinux_enabled())
|
---|
16 | return;
|
---|
17 |
|
---|
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;
|
---|
23 |
|
---|
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;
|
---|
29 | }
|
---|
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);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | return;
|
---|
45 | error:
|
---|
46 | bb_perror_msg_and_die("SELinux relabeling failed");
|
---|
47 | }
|
---|
48 | #else
|
---|
49 | # define mkswap_selinux_setcontext(fd, path) ((void)0)
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | /* from Linux 2.6.23 */
|
---|
53 | /*
|
---|
54 | * Magic header for a swap area. ... Note that the first
|
---|
55 | * kilobyte is reserved for boot loader or disk label stuff.
|
---|
56 | */
|
---|
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;
|
---|
68 |
|
---|
69 | #define NWORDS 129
|
---|
70 | #define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
|
---|
71 |
|
---|
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];
|
---|
75 | };
|
---|
76 |
|
---|
77 | /* Stored without terminating NUL */
|
---|
78 | static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
|
---|
79 |
|
---|
80 | int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
81 | int mkswap_main(int argc UNUSED_PARAM, char **argv)
|
---|
82 | {
|
---|
83 | int fd;
|
---|
84 | unsigned pagesize;
|
---|
85 | off_t len;
|
---|
86 | const char *label = "";
|
---|
87 |
|
---|
88 | opt_complementary = "-1"; /* at least one param */
|
---|
89 | /* TODO: -p PAGESZ, -U UUID */
|
---|
90 | getopt32(argv, "L:", &label);
|
---|
91 | argv += optind;
|
---|
92 |
|
---|
93 | fd = xopen(argv[0], O_WRONLY);
|
---|
94 |
|
---|
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;
|
---|
99 |
|
---|
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]);
|
---|
103 |
|
---|
104 | /* hdr is zero-filled so far. Clear the first kbyte, or else
|
---|
105 | * mkswap-ing former FAT partition does NOT erase its signature.
|
---|
106 | *
|
---|
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.
|
---|
109 | */
|
---|
110 | xwrite(fd, hdr, 1024);
|
---|
111 |
|
---|
112 | /* Fill the header. */
|
---|
113 | hdr->version = 1;
|
---|
114 | hdr->last_page = (uoff_t)len / pagesize;
|
---|
115 |
|
---|
116 | if (ENABLE_FEATURE_MKSWAP_UUID) {
|
---|
117 | char uuid_string[32];
|
---|
118 | generate_uuid((void*)hdr->sws_uuid);
|
---|
119 | bin2hex(uuid_string, hdr->sws_uuid, 16);
|
---|
120 | /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
|
---|
121 | printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n",
|
---|
122 | uuid_string,
|
---|
123 | uuid_string+8,
|
---|
124 | uuid_string+8+4,
|
---|
125 | uuid_string+8+4+4,
|
---|
126 | uuid_string+8+4+4+4
|
---|
127 | );
|
---|
128 | }
|
---|
129 | safe_strncpy(hdr->sws_volume, label, 16);
|
---|
130 |
|
---|
131 | /* Write the header. Sync to disk because some kernel versions check
|
---|
132 | * signature on disk (not in cache) during swapon. */
|
---|
133 | xwrite(fd, hdr, NWORDS * 4);
|
---|
134 | xlseek(fd, pagesize - 10, SEEK_SET);
|
---|
135 | xwrite(fd, SWAPSPACE2, 10);
|
---|
136 | fsync(fd);
|
---|
137 |
|
---|
138 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
139 | close(fd);
|
---|
140 |
|
---|
141 | return 0;
|
---|
142 | }
|
---|