blob: 53537fcd9dceb1e28742a3c9d572af964df50d93 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2, see file LICENSE in this source tree.
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
Denys Vlasenko8d3e2252010-08-31 12:42:06 +020018 xfstat(fd, &stbuf, argv[0]);
Denis Vlasenko85ff8622007-10-19 21:49:48 +000019 if (S_ISREG(stbuf.st_mode)) {
20 security_context_t newcon;
21 security_context_t oldcon = NULL;
22 context_t context;
23
Denis Vlasenko95842fb2008-04-25 08:43:01 +000024 if (fgetfilecon(fd, &oldcon) < 0) {
Denis Vlasenko85ff8622007-10-19 21:49:48 +000025 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;
Bernhard Reutner-Fischerc6191e92008-04-24 10:44:31 +000036 /* fsetfilecon_raw is hidden */
37 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
Denis Vlasenko85ff8622007-10-19 21:49:48 +000038 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
Denys Vlasenkoc9f280c2009-06-18 21:55:47 +020049# define mkswap_selinux_setcontext(fd, path) ((void)0)
50#endif
51
Denys Vlasenko68c67462009-11-03 05:51:20 +010052/* from Linux 2.6.23 */
Denis Vlasenko9d96af22008-02-13 15:35:52 +000053/*
Denys Vlasenko68c67462009-11-03 05:51:20 +010054 * Magic header for a swap area. ... Note that the first
55 * kilobyte is reserved for boot loader or disk label stuff.
Denis Vlasenko9d96af22008-02-13 15:35:52 +000056 */
Denys Vlasenko68c67462009-11-03 05:51:20 +010057struct 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 */
Denys Vlasenko12ca0802010-02-04 18:41:18 +010067} FIX_ALIASING;
Denis Vlasenko9d96af22008-02-13 15:35:52 +000068
69#define NWORDS 129
Denys Vlasenko68c67462009-11-03 05:51:20 +010070#define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
Denis Vlasenko9d96af22008-02-13 15:35:52 +000071
Denys Vlasenko68c67462009-11-03 05:51:20 +010072struct 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];
Denis Vlasenko9d96af22008-02-13 15:35:52 +000075};
76
77/* Stored without terminating NUL */
78static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
79
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000080int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko68c67462009-11-03 05:51:20 +010081int mkswap_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000082{
Denys Vlasenko68c67462009-11-03 05:51:20 +010083 int fd;
84 unsigned pagesize;
Rob Landleyd893b122006-07-16 08:17:03 +000085 off_t len;
Denys Vlasenko68c67462009-11-03 05:51:20 +010086 const char *label = "";
Rob Landleyd893b122006-07-16 08:17:03 +000087
Denys Vlasenko40e7d252010-02-01 23:48:27 +010088 opt_complementary = "-1"; /* at least one param */
89 /* TODO: -p PAGESZ, -U UUID */
Denys Vlasenko68c67462009-11-03 05:51:20 +010090 getopt32(argv, "L:", &label);
91 argv += optind;
Rob Landleyd893b122006-07-16 08:17:03 +000092
Denys Vlasenko68c67462009-11-03 05:51:20 +010093 fd = xopen(argv[0], O_WRONLY);
Rob Landleyd893b122006-07-16 08:17:03 +000094
Denys Vlasenko40e7d252010-02-01 23:48:27 +010095 /* Figure out how big the device is */
96 len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1);
Denys Vlasenko68c67462009-11-03 05:51:20 +010097 pagesize = getpagesize();
98 len -= pagesize;
Denys Vlasenko40e7d252010-02-01 23:48:27 +010099
100 /* Announce our intentions */
Denys Vlasenko68c67462009-11-03 05:51:20 +0100101 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
102 mkswap_selinux_setcontext(fd, argv[0]);
103
Denys Vlasenko4e7dd3c2010-08-31 01:50:03 +0200104 /* 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. */
Denys Vlasenko68c67462009-11-03 05:51:20 +0100113 hdr->version = 1;
114 hdr->last_page = (uoff_t)len / pagesize;
115
Denys Vlasenkof5a295d2009-10-15 22:43:07 +0200116 if (ENABLE_FEATURE_MKSWAP_UUID) {
117 char uuid_string[32];
Denys Vlasenko68c67462009-11-03 05:51:20 +0100118 generate_uuid((void*)hdr->sws_uuid);
119 bin2hex(uuid_string, hdr->sws_uuid, 16);
Denys Vlasenkof5a295d2009-10-15 22:43:07 +0200120 /* 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 }
Denys Vlasenko68c67462009-11-03 05:51:20 +0100129 safe_strncpy(hdr->sws_volume, label, 16);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000130
Denys Vlasenko68c67462009-11-03 05:51:20 +0100131 /* Write the header. Sync to disk because some kernel versions check
132 * signature on disk (not in cache) during swapon. */
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000133 xwrite(fd, hdr, NWORDS * 4);
Denis Vlasenko85ff8622007-10-19 21:49:48 +0000134 xlseek(fd, pagesize - 10, SEEK_SET);
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000135 xwrite(fd, SWAPSPACE2, 10);
Rob Landleyd893b122006-07-16 08:17:03 +0000136 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000137
Denys Vlasenko68c67462009-11-03 05:51:20 +0100138 if (ENABLE_FEATURE_CLEAN_UP)
139 close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000140
Rob Landleyd893b122006-07-16 08:17:03 +0000141 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000142}