blob: bf0d7b0749358a92cf8e2c963c60397365156e1e [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 */
8
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000010
Denis Vlasenko85ff8622007-10-19 21:49:48 +000011#if ENABLE_SELINUX
12static void mkswap_selinux_setcontext(int fd, const char *path)
13{
14 struct stat stbuf;
15
16 if (!is_selinux_enabled())
17 return;
18
19 if (fstat(fd, &stbuf) < 0)
20 bb_perror_msg_and_die("fstat failed");
21 if (S_ISREG(stbuf.st_mode)) {
22 security_context_t newcon;
23 security_context_t oldcon = NULL;
24 context_t context;
25
26 if (fgetfilecon_raw(fd, &oldcon) < 0) {
27 if (errno != ENODATA)
28 goto error;
29 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0)
30 goto error;
31 }
32 context = context_new(oldcon);
33 if (!context || context_type_set(context, "swapfile_t"))
34 goto error;
35 newcon = context_str(context);
36 if (!newcon)
37 goto error;
38 if (strcmp(oldcon, newcon) != 0 && fsetfilecon_raw(fd, newcon) < 0)
39 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
50#define mkswap_selinux_setcontext(fd, path) ((void)0)
51#endif
52
Denis Vlasenko9d96af22008-02-13 15:35:52 +000053#if 0 /* from Linux 2.6.23 */
54/*
55 * Magic header for a swap area. The first part of the union is
56 * what the swap magic looks like for the old (limited to 128MB)
57 * swap area format, the second part of the union adds - in the
58 * old reserved area - some extra information. Note that the first
59 * kilobyte is reserved for boot loader or disk label stuff...
60 */
61union swap_header {
62 struct {
63 char reserved[PAGE_SIZE - 10];
64 char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */
65 } magic;
66 struct {
67 char bootbits[1024]; /* Space for disklabel etc. */
68 __u32 version; /* second kbyte, word 0 */
69 __u32 last_page; /* 1 */
70 __u32 nr_badpages; /* 2 */
71 unsigned char sws_uuid[16]; /* 3,4,5,6 */
72 unsigned char sws_volume[16]; /* 7,8,9,10 */
73 __u32 padding[117]; /* 11..127 */
74 __u32 badpages[1]; /* 128, total 129 32-bit words */
75 } info;
76};
77#endif
78
79#define NWORDS 129
80#define hdr ((uint32_t*)(&bb_common_bufsiz1))
81
82struct BUG_bufsiz1_is_too_small {
83 char BUG_bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
84};
85
86/* Stored without terminating NUL */
87static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
88
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000089int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000090int mkswap_main(int argc, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000091{
Rob Landleyd893b122006-07-16 08:17:03 +000092 int fd, pagesize;
93 off_t len;
Rob Landleyd893b122006-07-16 08:17:03 +000094
95 // No options supported.
96
Denis Vlasenkod398eca2006-11-24 15:38:03 +000097 if (argc != 2) bb_show_usage();
Rob Landleyd893b122006-07-16 08:17:03 +000098
99 // Figure out how big the device is and announce our intentions.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000100
Denis Vlasenkod398eca2006-11-24 15:38:03 +0000101 fd = xopen(argv[1], O_RDWR);
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000102 /* fdlength was reported to be unreliable - use seek */
103 len = xlseek(fd, 0, SEEK_END);
Denis Vlasenko32d49bc2008-02-03 23:52:41 +0000104#if ENABLE_SELINUX
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000105 xlseek(fd, 0, SEEK_SET);
Denis Vlasenko32d49bc2008-02-03 23:52:41 +0000106#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +0000107 pagesize = getpagesize();
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +0000108 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n",
Denis Vlasenkod398eca2006-11-24 15:38:03 +0000109 len - pagesize);
Denis Vlasenko85ff8622007-10-19 21:49:48 +0000110 mkswap_selinux_setcontext(fd, argv[1]);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000111
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000112 // Make a header. hdr is zero-filled so far...
Rob Landleyd893b122006-07-16 08:17:03 +0000113 hdr[0] = 1;
114 hdr[1] = (len / pagesize) - 1;
Eric Andersene77ae3a1999-10-19 20:03:34 +0000115
Rob Landleyd893b122006-07-16 08:17:03 +0000116 // Write the header. Sync to disk because some kernel versions check
117 // signature on disk (not in cache) during swapon.
Eric Andersene77ae3a1999-10-19 20:03:34 +0000118
Rob Landleyd893b122006-07-16 08:17:03 +0000119 xlseek(fd, 1024, SEEK_SET);
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000120 xwrite(fd, hdr, NWORDS * 4);
Denis Vlasenko85ff8622007-10-19 21:49:48 +0000121 xlseek(fd, pagesize - 10, SEEK_SET);
Denis Vlasenko9d96af22008-02-13 15:35:52 +0000122 xwrite(fd, SWAPSPACE2, 10);
Rob Landleyd893b122006-07-16 08:17:03 +0000123 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000124
Rob Landleyd893b122006-07-16 08:17:03 +0000125 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +0000126
Rob Landleyd893b122006-07-16 08:17:03 +0000127 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +0000128}