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.
9 //config: bool "mkswap"
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.
21 //config:config FEATURE_MKSWAP_UUID
22 //config: bool "UUID support"
24 //config: depends on MKSWAP
26 //config: Generate swap spaces with universally unique identifiers.
28 //applet:IF_MKSWAP(APPLET(mkswap, BB_DIR_SBIN, BB_SUID_DROP))
30 //kbuild:lib-$(CONFIG_MKSWAP) += mkswap.o
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"
36 //usage: "\n -L LBL Label"
39 #include "common_bufsiz.h"
42 static void mkswap_selinux_setcontext(int fd, const char *path)
46 if (!is_selinux_enabled())
49 xfstat(fd, &stbuf, path);
50 if (S_ISREG(stbuf.st_mode)) {
51 security_context_t newcon;
52 security_context_t oldcon = NULL;
55 if (fgetfilecon(fd, &oldcon) < 0) {
58 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
61 context = context_new(oldcon);
62 if (!context || context_type_set(context, "swapfile_t"))
64 newcon = context_str(context);
67 /* fsetfilecon_raw is hidden */
68 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
70 if (ENABLE_FEATURE_CLEAN_UP) {
71 context_free(context);
77 bb_perror_msg_and_die("SELinux relabeling failed");
80 # define mkswap_selinux_setcontext(fd, path) ((void)0)
83 /* from Linux 2.6.23 */
85 * Magic header for a swap area. ... Note that the first
86 * kilobyte is reserved for boot loader or disk label stuff.
88 struct 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 */
101 #define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
102 #define INIT_G() do { setup_common_bufsiz(); } while (0)
105 char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1];
106 char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
109 /* Stored without terminating NUL */
110 static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
112 int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
113 int mkswap_main(int argc UNUSED_PARAM, char **argv)
118 const char *label = "";
122 opt_complementary = "-1"; /* at least one param */
123 /* TODO: -p PAGESZ, -U UUID */
124 getopt32(argv, "L:", &label);
127 fd = xopen(argv[0], O_WRONLY);
129 /* Figure out how big the device is */
130 len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1);
131 pagesize = getpagesize();
134 /* Announce our intentions */
135 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
136 mkswap_selinux_setcontext(fd, argv[0]);
138 /* hdr is zero-filled so far. Clear the first kbyte, or else
139 * mkswap-ing former FAT partition does NOT erase its signature.
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.
144 xwrite(fd, hdr, 1024);
146 /* Fill the header. */
148 hdr->last_page = (uoff_t)len / pagesize;
150 if (ENABLE_FEATURE_MKSWAP_UUID) {
151 char uuid_string[32];
152 generate_uuid((void*)hdr->sws_uuid);
153 bin2hex(uuid_string, hdr->sws_uuid, 16);
154 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
155 printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n",
163 safe_strncpy(hdr->sws_volume, label, 16);
165 /* Write the header. Sync to disk because some kernel versions check
166 * signature on disk (not in cache) during swapon. */
167 xwrite(fd, hdr, NWORDS * 4);
168 xlseek(fd, pagesize - 10, SEEK_SET);
169 xwrite(fd, SWAPSPACE2, 10);
172 if (ENABLE_FEATURE_CLEAN_UP)