blob: 289692da3f245900e8689a92c3ea572c12ee4165 [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
Denys Vlasenko68c67462009-11-03 05:51:20 +010053/* from Linux 2.6.23 */
Denis Vlasenko9d96af22008-02-13 15:35:52 +000054/*
Denys Vlasenko68c67462009-11-03 05:51:20 +010055 * Magic header for a swap area. ... Note that the first
56 * kilobyte is reserved for boot loader or disk label stuff.
Denis Vlasenko9d96af22008-02-13 15:35:52 +000057 */
Denys Vlasenko68c67462009-11-03 05:51:20 +010058struct swap_header_v1 {
59/* char bootbits[1024]; Space for disklabel etc. */
60 uint32_t version; /* second kbyte, word 0 */
61 uint32_t last_page; /* 1 */
62 uint32_t nr_badpages; /* 2 */
63 char sws_uuid[16]; /* 3,4,5,6 */
64 char sws_volume[16]; /* 7,8,9,10 */
65 uint32_t padding[117]; /* 11..127 */
66 uint32_t badpages[1]; /* 128 */
67 /* total 129 32-bit words in 2nd kilobyte */
Denis Vlasenko9d96af22008-02-13 15:35:52 +000068};
Denis Vlasenko9d96af22008-02-13 15:35:52 +000069
70#define NWORDS 129
Denys Vlasenko68c67462009-11-03 05:51:20 +010071#define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
Denis Vlasenko9d96af22008-02-13 15:35:52 +000072
Denys Vlasenko68c67462009-11-03 05:51:20 +010073struct BUG_sizes {
74 char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1];
75 char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
Denis Vlasenko9d96af22008-02-13 15:35:52 +000076};
77
78/* Stored without terminating NUL */
79static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
80
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000081int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko68c67462009-11-03 05:51:20 +010082int mkswap_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000083{
Denys Vlasenko68c67462009-11-03 05:51:20 +010084 int fd;
85 unsigned pagesize;
Rob Landleyd893b122006-07-16 08:17:03 +000086 off_t len;
Denys Vlasenko68c67462009-11-03 05:51:20 +010087 const char *label = "";
Rob Landleyd893b122006-07-16 08:17:03 +000088
Denys Vlasenko68c67462009-11-03 05:51:20 +010089 opt_complementary = "=1";
90 /* TODO: -p PAGESZ, -U UUID,
91 * optional SIZE_IN_KB 2nd param
92 */
93 getopt32(argv, "L:", &label);
94 argv += optind;
Rob Landleyd893b122006-07-16 08:17:03 +000095
Denys Vlasenko68c67462009-11-03 05:51:20 +010096 fd = xopen(argv[0], O_WRONLY);
Rob Landleyd893b122006-07-16 08:17:03 +000097
Denys Vlasenko68c67462009-11-03 05:51:20 +010098 /* Figure out how big the device is and announce our intentions */
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000099 /* fdlength was reported to be unreliable - use seek */
100 len = xlseek(fd, 0, SEEK_END);
Denys Vlasenko68c67462009-11-03 05:51:20 +0100101 if (ENABLE_SELINUX)
102 xlseek(fd, 0, SEEK_SET);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000103
Denys Vlasenko68c67462009-11-03 05:51:20 +0100104 pagesize = getpagesize();
105 len -= pagesize;
106 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
107 mkswap_selinux_setcontext(fd, argv[0]);
108
109 /* Make a header. hdr is zero-filled so far... */
110 hdr->version = 1;
111 hdr->last_page = (uoff_t)len / pagesize;
112
Denys Vlasenkof5a295d2009-10-15 22:43:07 +0200113 if (ENABLE_FEATURE_MKSWAP_UUID) {
114 char uuid_string[32];
Denys Vlasenko68c67462009-11-03 05:51:20 +0100115 generate_uuid((void*)hdr->sws_uuid);
116 bin2hex(uuid_string, hdr->sws_uuid, 16);
Denys Vlasenkof5a295d2009-10-15 22:43:07 +0200117 /* 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 }
Denys Vlasenko68c67462009-11-03 05:51:20 +0100126 safe_strncpy(hdr->sws_volume, label, 16);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000127
Denys Vlasenko68c67462009-11-03 05:51:20 +0100128 /* Write the header. Sync to disk because some kernel versions check
129 * signature on disk (not in cache) during swapon. */
Rob Landleyd893b122006-07-16 08:17:03 +0000130 xlseek(fd, 1024, SEEK_SET);
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000131 xwrite(fd, hdr, NWORDS * 4);
Denis Vlasenko85ff8622007-10-19 21:49:48 +0000132 xlseek(fd, pagesize - 10, SEEK_SET);
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000133 xwrite(fd, SWAPSPACE2, 10);
Rob Landleyd893b122006-07-16 08:17:03 +0000134 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000135
Denys Vlasenko68c67462009-11-03 05:51:20 +0100136 if (ENABLE_FEATURE_CLEAN_UP)
137 close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000138
Rob Landleyd893b122006-07-16 08:17:03 +0000139 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000140}