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 GPL version 2, see file LICENSE in this tarball for details.
11 static void mkswap_selinux_setcontext(int fd, const char *path)
15 if (!is_selinux_enabled())
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;
25 if (fgetfilecon(fd, &oldcon) < 0) {
28 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
31 context = context_new(oldcon);
32 if (!context || context_type_set(context, "swapfile_t"))
34 newcon = context_str(context);
37 /* fsetfilecon_raw is hidden */
38 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0)
40 if (ENABLE_FEATURE_CLEAN_UP) {
41 context_free(context);
47 bb_perror_msg_and_die("SELinux relabeling failed");
50 # define mkswap_selinux_setcontext(fd, path) ((void)0)
53 /* from Linux 2.6.23 */
55 * Magic header for a swap area. ... Note that the first
56 * kilobyte is reserved for boot loader or disk label stuff.
58 struct 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 */
71 #define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
74 char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1];
75 char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
78 /* Stored without terminating NUL */
79 static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
81 int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
82 int mkswap_main(int argc UNUSED_PARAM, char **argv)
87 const char *label = "";
89 opt_complementary = "-1"; /* at least one param */
90 /* TODO: -p PAGESZ, -U UUID */
91 getopt32(argv, "L:", &label);
94 fd = xopen(argv[0], O_WRONLY);
96 /* Figure out how big the device is */
97 len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1);
98 pagesize = getpagesize();
101 /* Announce our intentions */
102 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
103 mkswap_selinux_setcontext(fd, argv[0]);
105 /* Make a header. hdr is zero-filled so far... */
107 hdr->last_page = (uoff_t)len / pagesize;
109 if (ENABLE_FEATURE_MKSWAP_UUID) {
110 char uuid_string[32];
111 generate_uuid((void*)hdr->sws_uuid);
112 bin2hex(uuid_string, hdr->sws_uuid, 16);
113 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
114 printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n",
122 safe_strncpy(hdr->sws_volume, label, 16);
124 /* Write the header. Sync to disk because some kernel versions check
125 * signature on disk (not in cache) during swapon. */
126 xlseek(fd, 1024, SEEK_SET);
127 xwrite(fd, hdr, NWORDS * 4);
128 xlseek(fd, pagesize - 10, SEEK_SET);
129 xwrite(fd, SWAPSPACE2, 10);
132 if (ENABLE_FEATURE_CLEAN_UP)