blob: 954a1948dc4b3eb46ef812ef703c2bce99d03fdb [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 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +01008//config:config MKSWAP
9//config: bool "mkswap"
10//config: default y
11//config: help
12//config: The mkswap utility is used to configure a file or disk partition as
13//config: Linux swap space. This allows Linux to use the entire file or
14//config: partition as if it were additional RAM, which can greatly increase
15//config: the capability of low-memory machines. This additional memory is
16//config: much slower than real RAM, but can be very helpful at preventing your
17//config: applications being killed by the Linux out of memory (OOM) killer.
18//config: Once you have created swap space using 'mkswap' you need to enable
19//config: the swap space using the 'swapon' utility.
20//config:
21//config:config FEATURE_MKSWAP_UUID
22//config: bool "UUID support"
23//config: default y
24//config: depends on MKSWAP
25//config: help
26//config: Generate swap spaces with universally unique identifiers.
27
28//applet:IF_MKSWAP(APPLET(mkswap, BB_DIR_SBIN, BB_SUID_DROP))
29
30//kbuild:lib-$(CONFIG_MKSWAP) += mkswap.o
Pere Orga5bc8c002011-04-11 03:29:49 +020031
32//usage:#define mkswap_trivial_usage
33//usage: "[-L LBL] BLOCKDEV [KBYTES]"
34//usage:#define mkswap_full_usage "\n\n"
35//usage: "Prepare BLOCKDEV to be used as swap partition\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020036//usage: "\n -L LBL Label"
37
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000038#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020039#include "common_bufsiz.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000040
Denis Vlasenko85ff8622007-10-19 21:49:48 +000041#if ENABLE_SELINUX
42static void mkswap_selinux_setcontext(int fd, const char *path)
43{
44 struct stat stbuf;
45
46 if (!is_selinux_enabled())
47 return;
48
Denys Vlasenkoe66a2122011-01-05 11:45:44 +010049 xfstat(fd, &stbuf, path);
Denis Vlasenko85ff8622007-10-19 21:49:48 +000050 if (S_ISREG(stbuf.st_mode)) {
51 security_context_t newcon;
52 security_context_t oldcon = NULL;
53 context_t context;
54
Denis Vlasenko95842fb2008-04-25 08:43:01 +000055 if (fgetfilecon(fd, &oldcon) < 0) {
Denis Vlasenko85ff8622007-10-19 21:49:48 +000056 if (errno != ENODATA)
57 goto error;
58 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
59 goto error;
60 }
61 context = context_new(oldcon);
62 if (!context || context_type_set(context, "swapfile_t"))
63 goto error;
64 newcon = context_str(context);
65 if (!newcon)
66 goto error;
Bernhard Reutner-Fischerc6191e92008-04-24 10:44:31 +000067 /* fsetfilecon_raw is hidden */
68 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
Denis Vlasenko85ff8622007-10-19 21:49:48 +000069 goto error;
70 if (ENABLE_FEATURE_CLEAN_UP) {
71 context_free(context);
72 freecon(oldcon);
73 }
74 }
75 return;
76 error:
77 bb_perror_msg_and_die("SELinux relabeling failed");
78}
79#else
Denys Vlasenkoc9f280c2009-06-18 21:55:47 +020080# define mkswap_selinux_setcontext(fd, path) ((void)0)
81#endif
82
Denys Vlasenko68c67462009-11-03 05:51:20 +010083/* from Linux 2.6.23 */
Denis Vlasenko9d96af22008-02-13 15:35:52 +000084/*
Denys Vlasenko68c67462009-11-03 05:51:20 +010085 * Magic header for a swap area. ... Note that the first
86 * kilobyte is reserved for boot loader or disk label stuff.
Denis Vlasenko9d96af22008-02-13 15:35:52 +000087 */
Denys Vlasenko68c67462009-11-03 05:51:20 +010088struct swap_header_v1 {
89/* char bootbits[1024]; Space for disklabel etc. */
90 uint32_t version; /* second kbyte, word 0 */
91 uint32_t last_page; /* 1 */
92 uint32_t nr_badpages; /* 2 */
93 char sws_uuid[16]; /* 3,4,5,6 */
94 char sws_volume[16]; /* 7,8,9,10 */
95 uint32_t padding[117]; /* 11..127 */
96 uint32_t badpages[1]; /* 128 */
97 /* total 129 32-bit words in 2nd kilobyte */
Denys Vlasenko12ca0802010-02-04 18:41:18 +010098} FIX_ALIASING;
Denis Vlasenko9d96af22008-02-13 15:35:52 +000099
100#define NWORDS 129
Denys Vlasenko68c67462009-11-03 05:51:20 +0100101#define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200102#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000103
Denys Vlasenko68c67462009-11-03 05:51:20 +0100104struct BUG_sizes {
105 char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1];
106 char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000107};
108
109/* Stored without terminating NUL */
110static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
111
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000112int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko68c67462009-11-03 05:51:20 +0100113int mkswap_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000114{
Denys Vlasenko68c67462009-11-03 05:51:20 +0100115 int fd;
116 unsigned pagesize;
Rob Landleyd893b122006-07-16 08:17:03 +0000117 off_t len;
Denys Vlasenko68c67462009-11-03 05:51:20 +0100118 const char *label = "";
Rob Landleyd893b122006-07-16 08:17:03 +0000119
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200120 INIT_G();
121
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100122 opt_complementary = "-1"; /* at least one param */
123 /* TODO: -p PAGESZ, -U UUID */
Denys Vlasenko68c67462009-11-03 05:51:20 +0100124 getopt32(argv, "L:", &label);
125 argv += optind;
Rob Landleyd893b122006-07-16 08:17:03 +0000126
Denys Vlasenko68c67462009-11-03 05:51:20 +0100127 fd = xopen(argv[0], O_WRONLY);
Rob Landleyd893b122006-07-16 08:17:03 +0000128
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100129 /* Figure out how big the device is */
130 len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1);
Denys Vlasenko68c67462009-11-03 05:51:20 +0100131 pagesize = getpagesize();
132 len -= pagesize;
Denys Vlasenko40e7d252010-02-01 23:48:27 +0100133
134 /* Announce our intentions */
Denys Vlasenko68c67462009-11-03 05:51:20 +0100135 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
136 mkswap_selinux_setcontext(fd, argv[0]);
137
Denys Vlasenko4e7dd3c2010-08-31 01:50:03 +0200138 /* hdr is zero-filled so far. Clear the first kbyte, or else
139 * mkswap-ing former FAT partition does NOT erase its signature.
140 *
141 * util-linux-ng 2.17.2 claims to erase it only if it does not see
142 * a partition table and is not run on whole disk. -f forces it.
143 */
144 xwrite(fd, hdr, 1024);
145
146 /* Fill the header. */
Denys Vlasenko68c67462009-11-03 05:51:20 +0100147 hdr->version = 1;
148 hdr->last_page = (uoff_t)len / pagesize;
149
Denys Vlasenkof5a295d2009-10-15 22:43:07 +0200150 if (ENABLE_FEATURE_MKSWAP_UUID) {
151 char uuid_string[32];
Denys Vlasenko68c67462009-11-03 05:51:20 +0100152 generate_uuid((void*)hdr->sws_uuid);
153 bin2hex(uuid_string, hdr->sws_uuid, 16);
Denys Vlasenkof5a295d2009-10-15 22:43:07 +0200154 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
155 printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n",
156 uuid_string,
157 uuid_string+8,
158 uuid_string+8+4,
159 uuid_string+8+4+4,
160 uuid_string+8+4+4+4
161 );
162 }
Denys Vlasenko68c67462009-11-03 05:51:20 +0100163 safe_strncpy(hdr->sws_volume, label, 16);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000164
Denys Vlasenko68c67462009-11-03 05:51:20 +0100165 /* Write the header. Sync to disk because some kernel versions check
166 * signature on disk (not in cache) during swapon. */
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000167 xwrite(fd, hdr, NWORDS * 4);
Denis Vlasenko85ff8622007-10-19 21:49:48 +0000168 xlseek(fd, pagesize - 10, SEEK_SET);
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000169 xwrite(fd, SWAPSPACE2, 10);
Rob Landleyd893b122006-07-16 08:17:03 +0000170 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000171
Denys Vlasenko68c67462009-11-03 05:51:20 +0100172 if (ENABLE_FEATURE_CLEAN_UP)
173 close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000174
Rob Landleyd893b122006-07-16 08:17:03 +0000175 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000176}