blob: f047cce26a2761e275e923cebde7d6ecc079bbbb [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);
69 xlseek(fd, 0, SEEK_SET);
Eric Andersene77ae3a1999-10-19 20:03:34 +000070 pagesize = getpagesize();
Denis Vlasenkob5c60fc2008-01-27 23:41:34 +000071 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n",
Denis Vlasenkod398eca2006-11-24 15:38:03 +000072 len - pagesize);
Denis Vlasenko85ff8622007-10-19 21:49:48 +000073 mkswap_selinux_setcontext(fd, argv[1]);
Eric Andersene77ae3a1999-10-19 20:03:34 +000074
Rob Landleyd893b122006-07-16 08:17:03 +000075 // Make a header.
Eric Andersene77ae3a1999-10-19 20:03:34 +000076
Denis Vlasenkod398eca2006-11-24 15:38:03 +000077 memset(hdr, 0, sizeof(hdr));
Rob Landleyd893b122006-07-16 08:17:03 +000078 hdr[0] = 1;
79 hdr[1] = (len / pagesize) - 1;
Eric Andersene77ae3a1999-10-19 20:03:34 +000080
Rob Landleyd893b122006-07-16 08:17:03 +000081 // Write the header. Sync to disk because some kernel versions check
82 // signature on disk (not in cache) during swapon.
Eric Andersene77ae3a1999-10-19 20:03:34 +000083
Rob Landleyd893b122006-07-16 08:17:03 +000084 xlseek(fd, 1024, SEEK_SET);
Denis Vlasenkod398eca2006-11-24 15:38:03 +000085 xwrite(fd, hdr, sizeof(hdr));
Denis Vlasenko85ff8622007-10-19 21:49:48 +000086 xlseek(fd, pagesize - 10, SEEK_SET);
Rob Landleyd893b122006-07-16 08:17:03 +000087 xwrite(fd, "SWAPSPACE2", 10);
88 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000089
Rob Landleyd893b122006-07-16 08:17:03 +000090 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000091
Rob Landleyd893b122006-07-16 08:17:03 +000092 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000093}