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.
11 static void mkswap_selinux_setcontext(int fd
, const char *path
)
15 if (!is_selinux_enabled())
18 xfstat(fd
, &stbuf
, path
);
19 if (S_ISREG(stbuf
.st_mode
)) {
20 security_context_t newcon
;
21 security_context_t oldcon
= NULL
;
24 if (fgetfilecon(fd
, &oldcon
) < 0) {
27 if (matchpathcon(path
, stbuf
.st_mode
, &oldcon
) < 0)
30 context
= context_new(oldcon
);
31 if (!context
|| context_type_set(context
, "swapfile_t"))
33 newcon
= context_str(context
);
36 /* fsetfilecon_raw is hidden */
37 if (strcmp(oldcon
, newcon
) != 0 && fsetfilecon(fd
, newcon
) < 0)
39 if (ENABLE_FEATURE_CLEAN_UP
) {
40 context_free(context
);
46 bb_perror_msg_and_die("SELinux relabeling failed");
49 # define mkswap_selinux_setcontext(fd, path) ((void)0)
52 /* from Linux 2.6.23 */
54 * Magic header for a swap area. ... Note that the first
55 * kilobyte is reserved for boot loader or disk label stuff.
57 struct 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 */
70 #define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
73 char swap_header_v1_wrong
[sizeof(*hdr
) != (NWORDS
* 4) ? -1 : 1];
74 char bufsiz1_is_too_small
[COMMON_BUFSIZE
< (NWORDS
* 4) ? -1 : 1];
77 /* Stored without terminating NUL */
78 static const char SWAPSPACE2
[sizeof("SWAPSPACE2")-1] ALIGN1
= "SWAPSPACE2";
80 int mkswap_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
81 int mkswap_main(int argc UNUSED_PARAM
, char **argv
)
86 const char *label
= "";
88 opt_complementary
= "-1"; /* at least one param */
89 /* TODO: -p PAGESZ, -U UUID */
90 getopt32(argv
, "L:", &label
);
93 fd
= xopen(argv
[0], O_WRONLY
);
95 /* Figure out how big the device is */
96 len
= get_volume_size_in_bytes(fd
, argv
[1], 1024, /*extend:*/ 1);
97 pagesize
= getpagesize();
100 /* Announce our intentions */
101 printf("Setting up swapspace version 1, size = %"OFF_FMT
"u bytes\n", len
);
102 mkswap_selinux_setcontext(fd
, argv
[0]);
104 /* hdr is zero-filled so far. Clear the first kbyte, or else
105 * mkswap-ing former FAT partition does NOT erase its signature.
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.
110 xwrite(fd
, hdr
, 1024);
112 /* Fill the header. */
114 hdr
->last_page
= (uoff_t
)len
/ pagesize
;
116 if (ENABLE_FEATURE_MKSWAP_UUID
) {
117 char uuid_string
[37];
118 generate_uuid((void*)hdr
->sws_uuid
);
119 printf("UUID=%s\n", unparse_uuid((uint8_t *)hdr
->sws_uuid
, uuid_string
));
121 safe_strncpy(hdr
->sws_volume
, label
, 16);
123 /* Write the header. Sync to disk because some kernel versions check
124 * signature on disk (not in cache) during swapon. */
125 xwrite(fd
, hdr
, NWORDS
* 4);
126 xlseek(fd
, pagesize
- 10, SEEK_SET
);
127 xwrite(fd
, SWAPSPACE2
, 10);
130 if (ENABLE_FEATURE_CLEAN_UP
)