blob: c4e30fd922db050441652ea4f8565f6ec05b9d8b [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Rob Landleyd893b122006-07-16 08:17:03 +00002/* mkswap.c - format swap device (Linux v1 only)
Eric Andersencc8ed391999-10-05 16:24:54 +00003 *
Rob Landleyd893b122006-07-16 08:17:03 +00004 * Copyright 2006 Rob Landley <rob@landley.net>
5 *
Rob Landleye9a7a622006-09-22 02:52:41 +00006 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +00007 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00008#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +00009
Denis Vlasenko85ff8622007-10-19 21:49:48 +000010#if ENABLE_SELINUX
11static void mkswap_selinux_setcontext(int fd, const char *path)
12{
13 struct stat stbuf;
14
15 if (!is_selinux_enabled())
16 return;
17
18 if (fstat(fd, &stbuf) < 0)
19 bb_perror_msg_and_die("fstat failed");
20 if (S_ISREG(stbuf.st_mode)) {
21 security_context_t newcon;
22 security_context_t oldcon = NULL;
23 context_t context;
24
Denis Vlasenko95842fb2008-04-25 08:43:01 +000025 if (fgetfilecon(fd, &oldcon) < 0) {
Denis Vlasenko85ff8622007-10-19 21:49:48 +000026 if (errno != ENODATA)
27 goto error;
28 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
29 goto error;
30 }
31 context = context_new(oldcon);
32 if (!context || context_type_set(context, "swapfile_t"))
33 goto error;
34 newcon = context_str(context);
35 if (!newcon)
36 goto error;
Bernhard Reutner-Fischerc6191e92008-04-24 10:44:31 +000037 /* fsetfilecon_raw is hidden */
38 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
Denis Vlasenko85ff8622007-10-19 21:49:48 +000039 goto error;
40 if (ENABLE_FEATURE_CLEAN_UP) {
41 context_free(context);
42 freecon(oldcon);
43 }
44 }
45 return;
46 error:
47 bb_perror_msg_and_die("SELinux relabeling failed");
48}
49#else
Denys Vlasenkoc9f280c2009-06-18 21:55:47 +020050# define mkswap_selinux_setcontext(fd, path) ((void)0)
51#endif
52
53#if ENABLE_DESKTOP
54static void mkswap_generate_uuid(uint8_t *buf)
55{
Denys Vlasenkoe1e22732009-06-19 09:49:01 +020056 pid_t pid;
Denys Vlasenkoc9f280c2009-06-18 21:55:47 +020057 unsigned i;
58 char uuid_string[32];
59
Denys Vlasenkoe1e22732009-06-19 09:49:01 +020060 /* http://www.ietf.org/rfc/rfc4122.txt
61 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
62 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 * | time_low |
64 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
65 * | time_mid | time_hi_and_version |
66 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67 * |clk_seq__and_variant | node (0-1) |
68 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
69 * | node (2-5) |
70 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71 * IOW, uuid has this layout:
72 * uint32_t time_low (big endian)
73 * uint16_t time_mid (big endian)
74 * uint16_t time_hi_and_version (big endian)
75 * version is a 4-bit field:
76 * 1 Time-based version
77 * 2 DCE Security version, with embedded POSIX UIDs
78 * 3 Name-based version (MD5)
79 * 4 Randomly generated version
80 * 5 Name-based version (SHA-1)
81 * uint16_t clk_seq_and_variant (big endian)
82 * variant is a 3-bit field:
83 * 0xx Reserved, NCS backward compatibility
84 * 10x The variant specified in rfc4122
85 * 110 Reserved, Microsoft backward compatibility
86 * 111 Reserved for future definition
87 * uint8_t node[6]
88 *
89 * For version 4, these bits are set/cleared:
90 * time_hi_and_version & 0x0fff | 0x4000
91 * clk_seq_and_variant & 0x3fff | 0x8000
92 */
93
94 i = open("/dev/urandom", O_RDONLY);
95 if (i >= 0) {
96 read(i, buf, 16);
97 close(i);
98 }
99 /* Paranoia. /dev/urandom may be missing.
100 * rand() is guaranteed to generate at least [0, 2^15) range,
Denys Vlasenkoc9f280c2009-06-18 21:55:47 +0200101 * but lowest bits in some libc are not so "random". */
Denys Vlasenkoe1e22732009-06-19 09:49:01 +0200102 srand(monotonic_us());
103 pid = getpid();
104 while (1) {
105 for (i = 0; i < 16; i++)
106 buf[i] ^= rand() >> 5;
107 if (pid == 0)
108 break;
109 srand(pid);
110 pid = 0;
111 }
112
113 buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40; /* time_hi_and_version */
114 buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; /* clk_seq_and_variant */
Denys Vlasenkoc9f280c2009-06-18 21:55:47 +0200115
116 bin2hex(uuid_string, (void*) buf, 16);
117 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
118 printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n",
119 uuid_string,
120 uuid_string+8,
121 uuid_string+8+4,
122 uuid_string+8+4+4,
123 uuid_string+8+4+4+4
124 );
125}
126#else
127# define mkswap_generate_uuid(buf) ((void)0)
Denis Vlasenko85ff8622007-10-19 21:49:48 +0000128#endif
129
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000130#if 0 /* from Linux 2.6.23 */
131/*
132 * Magic header for a swap area. The first part of the union is
133 * what the swap magic looks like for the old (limited to 128MB)
134 * swap area format, the second part of the union adds - in the
135 * old reserved area - some extra information. Note that the first
136 * kilobyte is reserved for boot loader or disk label stuff...
137 */
138union swap_header {
139 struct {
140 char reserved[PAGE_SIZE - 10];
141 char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */
142 } magic;
143 struct {
144 char bootbits[1024]; /* Space for disklabel etc. */
145 __u32 version; /* second kbyte, word 0 */
146 __u32 last_page; /* 1 */
147 __u32 nr_badpages; /* 2 */
148 unsigned char sws_uuid[16]; /* 3,4,5,6 */
149 unsigned char sws_volume[16]; /* 7,8,9,10 */
150 __u32 padding[117]; /* 11..127 */
151 __u32 badpages[1]; /* 128, total 129 32-bit words */
152 } info;
153};
154#endif
155
156#define NWORDS 129
157#define hdr ((uint32_t*)(&bb_common_bufsiz1))
158
159struct BUG_bufsiz1_is_too_small {
160 char BUG_bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
161};
162
163/* Stored without terminating NUL */
164static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
165
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000166int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +0000167int mkswap_main(int argc, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000168{
Rob Landleyd893b122006-07-16 08:17:03 +0000169 int fd, pagesize;
170 off_t len;
Rob Landleyd893b122006-07-16 08:17:03 +0000171
172 // No options supported.
173
Denis Vlasenkod398eca2006-11-24 15:38:03 +0000174 if (argc != 2) bb_show_usage();
Rob Landleyd893b122006-07-16 08:17:03 +0000175
176 // Figure out how big the device is and announce our intentions.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000177
Denis Vlasenkod398eca2006-11-24 15:38:03 +0000178 fd = xopen(argv[1], O_RDWR);
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000179 /* fdlength was reported to be unreliable - use seek */
180 len = xlseek(fd, 0, SEEK_END);
Denis Vlasenko32d49bc2008-02-03 23:52:41 +0000181#if ENABLE_SELINUX
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000182 xlseek(fd, 0, SEEK_SET);
Denis Vlasenko32d49bc2008-02-03 23:52:41 +0000183#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +0000184 pagesize = getpagesize();
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000185 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n",
Denis Vlasenkod398eca2006-11-24 15:38:03 +0000186 len - pagesize);
Denis Vlasenko85ff8622007-10-19 21:49:48 +0000187 mkswap_selinux_setcontext(fd, argv[1]);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000188
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000189 // Make a header. hdr is zero-filled so far...
Rob Landleyd893b122006-07-16 08:17:03 +0000190 hdr[0] = 1;
191 hdr[1] = (len / pagesize) - 1;
Denys Vlasenkoc9f280c2009-06-18 21:55:47 +0200192 mkswap_generate_uuid((void*) &hdr[3]);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000193
Rob Landleyd893b122006-07-16 08:17:03 +0000194 // Write the header. Sync to disk because some kernel versions check
195 // signature on disk (not in cache) during swapon.
Rob Landleyd893b122006-07-16 08:17:03 +0000196 xlseek(fd, 1024, SEEK_SET);
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000197 xwrite(fd, hdr, NWORDS * 4);
Denis Vlasenko85ff8622007-10-19 21:49:48 +0000198 xlseek(fd, pagesize - 10, SEEK_SET);
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000199 xwrite(fd, SWAPSPACE2, 10);
Rob Landleyd893b122006-07-16 08:17:03 +0000200 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000201
Rob Landleyd893b122006-07-16 08:17:03 +0000202 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000203
Rob Landleyd893b122006-07-16 08:17:03 +0000204 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000205}