blob: 31d577315c38331bf51965c693ae7dbcbcea2f6f [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 Vlasenko9b49a5e2007-10-11 10:05:36 +000053int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000054int mkswap_main(int argc, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000055{
Rob Landleyd893b122006-07-16 08:17:03 +000056 int fd, pagesize;
57 off_t len;
58 unsigned int hdr[129];
59
60 // No options supported.
61
Denis Vlasenkod398eca2006-11-24 15:38:03 +000062 if (argc != 2) bb_show_usage();
Rob Landleyd893b122006-07-16 08:17:03 +000063
64 // Figure out how big the device is and announce our intentions.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000065
Denis Vlasenkod398eca2006-11-24 15:38:03 +000066 fd = xopen(argv[1], O_RDWR);
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000067 /* fdlength was reported to be unreliable - use seek */
68 len = xlseek(fd, 0, SEEK_END);
Denis Vlasenko32d49bc2008-02-03 23:52:41 +000069#if ENABLE_SELINUX
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000070 xlseek(fd, 0, SEEK_SET);
Denis Vlasenko32d49bc2008-02-03 23:52:41 +000071#endif
Eric Andersene77ae3a1999-10-19 20:03:34 +000072 pagesize = getpagesize();
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000073 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n",
Denis Vlasenkod398eca2006-11-24 15:38:03 +000074 len - pagesize);
Denis Vlasenko85ff8622007-10-19 21:49:48 +000075 mkswap_selinux_setcontext(fd, argv[1]);
Eric Andersene77ae3a1999-10-19 20:03:34 +000076
Rob Landleyd893b122006-07-16 08:17:03 +000077 // Make a header.
Eric Andersene77ae3a1999-10-19 20:03:34 +000078
Denis Vlasenkod398eca2006-11-24 15:38:03 +000079 memset(hdr, 0, sizeof(hdr));
Rob Landleyd893b122006-07-16 08:17:03 +000080 hdr[0] = 1;
81 hdr[1] = (len / pagesize) - 1;
Eric Andersene77ae3a1999-10-19 20:03:34 +000082
Rob Landleyd893b122006-07-16 08:17:03 +000083 // Write the header. Sync to disk because some kernel versions check
84 // signature on disk (not in cache) during swapon.
Eric Andersene77ae3a1999-10-19 20:03:34 +000085
Rob Landleyd893b122006-07-16 08:17:03 +000086 xlseek(fd, 1024, SEEK_SET);
Denis Vlasenkod398eca2006-11-24 15:38:03 +000087 xwrite(fd, hdr, sizeof(hdr));
Denis Vlasenko85ff8622007-10-19 21:49:48 +000088 xlseek(fd, pagesize - 10, SEEK_SET);
Rob Landleyd893b122006-07-16 08:17:03 +000089 xwrite(fd, "SWAPSPACE2", 10);
90 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000091
Rob Landleyd893b122006-07-16 08:17:03 +000092 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000093
Rob Landleyd893b122006-07-16 08:17:03 +000094 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000095}