Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 2 | /* mkswap.c - format swap device (Linux v1 only) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | * |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 4 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 5 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 6 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 11 | #if ENABLE_SELINUX |
| 12 | static 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 Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 53 | #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 | */ |
| 61 | union 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 | |
| 82 | struct 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 */ |
| 87 | static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2"; |
| 88 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 89 | int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 90 | int mkswap_main(int argc, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | { |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 92 | int fd, pagesize; |
| 93 | off_t len; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 94 | |
| 95 | // No options supported. |
| 96 | |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 97 | if (argc != 2) bb_show_usage(); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 98 | |
| 99 | // Figure out how big the device is and announce our intentions. |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 100 | |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 101 | fd = xopen(argv[1], O_RDWR); |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 102 | /* fdlength was reported to be unreliable - use seek */ |
| 103 | len = xlseek(fd, 0, SEEK_END); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 104 | #if ENABLE_SELINUX |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 105 | xlseek(fd, 0, SEEK_SET); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 106 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 107 | pagesize = getpagesize(); |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 108 | printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 109 | len - pagesize); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 110 | mkswap_selinux_setcontext(fd, argv[1]); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 112 | // Make a header. hdr is zero-filled so far... |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 113 | hdr[0] = 1; |
| 114 | hdr[1] = (len / pagesize) - 1; |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 115 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 116 | // Write the header. Sync to disk because some kernel versions check |
| 117 | // signature on disk (not in cache) during swapon. |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 118 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 119 | xlseek(fd, 1024, SEEK_SET); |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 120 | xwrite(fd, hdr, NWORDS * 4); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 121 | xlseek(fd, pagesize - 10, SEEK_SET); |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 122 | xwrite(fd, SWAPSPACE2, 10); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 123 | fsync(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 124 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 125 | if (ENABLE_FEATURE_CLEAN_UP) close(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 126 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 127 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 128 | } |