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 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 8 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 9 | |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 10 | #if ENABLE_SELINUX |
| 11 | static void mkswap_selinux_setcontext(int fd, const char *path) |
| 12 | { |
| 13 | struct stat stbuf; |
| 14 | |
| 15 | if (!is_selinux_enabled()) |
| 16 | return; |
| 17 | |
| 18 | if (fstat(fd, &stbuf) < 0) |
| 19 | bb_perror_msg_and_die("fstat failed"); |
| 20 | if (S_ISREG(stbuf.st_mode)) { |
| 21 | security_context_t newcon; |
| 22 | security_context_t oldcon = NULL; |
| 23 | context_t context; |
| 24 | |
Denis Vlasenko | 95842fb | 2008-04-25 08:43:01 +0000 | [diff] [blame] | 25 | if (fgetfilecon(fd, &oldcon) < 0) { |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 26 | if (errno != ENODATA) |
| 27 | goto error; |
| 28 | if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0) |
| 29 | goto error; |
| 30 | } |
| 31 | context = context_new(oldcon); |
| 32 | if (!context || context_type_set(context, "swapfile_t")) |
| 33 | goto error; |
| 34 | newcon = context_str(context); |
| 35 | if (!newcon) |
| 36 | goto error; |
Bernhard Reutner-Fischer | c6191e9 | 2008-04-24 10:44:31 +0000 | [diff] [blame] | 37 | /* fsetfilecon_raw is hidden */ |
| 38 | if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0) |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 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 |
Denys Vlasenko | c9f280c | 2009-06-18 21:55:47 +0200 | [diff] [blame] | 50 | # define mkswap_selinux_setcontext(fd, path) ((void)0) |
| 51 | #endif |
| 52 | |
Denys Vlasenko | beca96f | 2009-06-19 13:14:48 +0200 | [diff] [blame] | 53 | #if ENABLE_FEATURE_MKSWAP_UUID |
Denys Vlasenko | c9f280c | 2009-06-18 21:55:47 +0200 | [diff] [blame] | 54 | static void mkswap_generate_uuid(uint8_t *buf) |
| 55 | { |
Denys Vlasenko | e1e2273 | 2009-06-19 09:49:01 +0200 | [diff] [blame] | 56 | /* http://www.ietf.org/rfc/rfc4122.txt |
| 57 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 58 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 59 | * | time_low | |
| 60 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 61 | * | time_mid | time_hi_and_version | |
| 62 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Denys Vlasenko | b96159d | 2009-06-19 11:00:52 +0200 | [diff] [blame] | 63 | * |clk_seq_and_variant | node (0-1) | |
Denys Vlasenko | e1e2273 | 2009-06-19 09:49:01 +0200 | [diff] [blame] | 64 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 65 | * | node (2-5) | |
| 66 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 67 | * IOW, uuid has this layout: |
| 68 | * uint32_t time_low (big endian) |
| 69 | * uint16_t time_mid (big endian) |
| 70 | * uint16_t time_hi_and_version (big endian) |
| 71 | * version is a 4-bit field: |
Denys Vlasenko | b96159d | 2009-06-19 11:00:52 +0200 | [diff] [blame] | 72 | * 1 Time-based |
| 73 | * 2 DCE Security, with embedded POSIX UIDs |
| 74 | * 3 Name-based (MD5) |
| 75 | * 4 Randomly generated |
| 76 | * 5 Name-based (SHA-1) |
Denys Vlasenko | e1e2273 | 2009-06-19 09:49:01 +0200 | [diff] [blame] | 77 | * uint16_t clk_seq_and_variant (big endian) |
| 78 | * variant is a 3-bit field: |
| 79 | * 0xx Reserved, NCS backward compatibility |
| 80 | * 10x The variant specified in rfc4122 |
| 81 | * 110 Reserved, Microsoft backward compatibility |
| 82 | * 111 Reserved for future definition |
| 83 | * uint8_t node[6] |
| 84 | * |
| 85 | * For version 4, these bits are set/cleared: |
| 86 | * time_hi_and_version & 0x0fff | 0x4000 |
| 87 | * clk_seq_and_variant & 0x3fff | 0x8000 |
| 88 | */ |
Denys Vlasenko | b96159d | 2009-06-19 11:00:52 +0200 | [diff] [blame] | 89 | pid_t pid; |
| 90 | int i; |
| 91 | char uuid_string[32]; |
Denys Vlasenko | e1e2273 | 2009-06-19 09:49:01 +0200 | [diff] [blame] | 92 | |
| 93 | i = open("/dev/urandom", O_RDONLY); |
| 94 | if (i >= 0) { |
| 95 | read(i, buf, 16); |
| 96 | close(i); |
| 97 | } |
| 98 | /* Paranoia. /dev/urandom may be missing. |
| 99 | * rand() is guaranteed to generate at least [0, 2^15) range, |
Denys Vlasenko | c9f280c | 2009-06-18 21:55:47 +0200 | [diff] [blame] | 100 | * but lowest bits in some libc are not so "random". */ |
Denys Vlasenko | e1e2273 | 2009-06-19 09:49:01 +0200 | [diff] [blame] | 101 | srand(monotonic_us()); |
| 102 | pid = getpid(); |
| 103 | while (1) { |
| 104 | for (i = 0; i < 16; i++) |
| 105 | buf[i] ^= rand() >> 5; |
| 106 | if (pid == 0) |
| 107 | break; |
| 108 | srand(pid); |
| 109 | pid = 0; |
| 110 | } |
| 111 | |
Denys Vlasenko | b96159d | 2009-06-19 11:00:52 +0200 | [diff] [blame] | 112 | /* version = 4 */ |
| 113 | buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40; |
| 114 | /* variant = 10x */ |
| 115 | buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80; |
Denys Vlasenko | c9f280c | 2009-06-18 21:55:47 +0200 | [diff] [blame] | 116 | |
| 117 | bin2hex(uuid_string, (void*) buf, 16); |
| 118 | /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ |
| 119 | printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n", |
| 120 | uuid_string, |
| 121 | uuid_string+8, |
| 122 | uuid_string+8+4, |
| 123 | uuid_string+8+4+4, |
| 124 | uuid_string+8+4+4+4 |
| 125 | ); |
| 126 | } |
| 127 | #else |
| 128 | # define mkswap_generate_uuid(buf) ((void)0) |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 129 | #endif |
| 130 | |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 131 | #if 0 /* from Linux 2.6.23 */ |
| 132 | /* |
| 133 | * Magic header for a swap area. The first part of the union is |
| 134 | * what the swap magic looks like for the old (limited to 128MB) |
| 135 | * swap area format, the second part of the union adds - in the |
| 136 | * old reserved area - some extra information. Note that the first |
| 137 | * kilobyte is reserved for boot loader or disk label stuff... |
| 138 | */ |
| 139 | union swap_header { |
| 140 | struct { |
| 141 | char reserved[PAGE_SIZE - 10]; |
| 142 | char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */ |
| 143 | } magic; |
| 144 | struct { |
| 145 | char bootbits[1024]; /* Space for disklabel etc. */ |
| 146 | __u32 version; /* second kbyte, word 0 */ |
| 147 | __u32 last_page; /* 1 */ |
| 148 | __u32 nr_badpages; /* 2 */ |
| 149 | unsigned char sws_uuid[16]; /* 3,4,5,6 */ |
| 150 | unsigned char sws_volume[16]; /* 7,8,9,10 */ |
| 151 | __u32 padding[117]; /* 11..127 */ |
| 152 | __u32 badpages[1]; /* 128, total 129 32-bit words */ |
| 153 | } info; |
| 154 | }; |
| 155 | #endif |
| 156 | |
| 157 | #define NWORDS 129 |
| 158 | #define hdr ((uint32_t*)(&bb_common_bufsiz1)) |
| 159 | |
| 160 | struct BUG_bufsiz1_is_too_small { |
| 161 | char BUG_bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1]; |
| 162 | }; |
| 163 | |
| 164 | /* Stored without terminating NUL */ |
| 165 | static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2"; |
| 166 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 167 | int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Bernhard Reutner-Fischer | febe3c4 | 2007-04-04 20:52:03 +0000 | [diff] [blame] | 168 | int mkswap_main(int argc, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 169 | { |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 170 | int fd, pagesize; |
| 171 | off_t len; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 172 | |
| 173 | // No options supported. |
| 174 | |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 175 | if (argc != 2) bb_show_usage(); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 176 | |
| 177 | // Figure out how big the device is and announce our intentions. |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 178 | |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 179 | fd = xopen(argv[1], O_RDWR); |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 180 | /* fdlength was reported to be unreliable - use seek */ |
| 181 | len = xlseek(fd, 0, SEEK_END); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 182 | #if ENABLE_SELINUX |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 183 | xlseek(fd, 0, SEEK_SET); |
Denis Vlasenko | 32d49bc | 2008-02-03 23:52:41 +0000 | [diff] [blame] | 184 | #endif |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 185 | pagesize = getpagesize(); |
Denis Vlasenko | b5c60fc | 2008-01-27 23:41:34 +0000 | [diff] [blame] | 186 | printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", |
Denis Vlasenko | d398eca | 2006-11-24 15:38:03 +0000 | [diff] [blame] | 187 | len - pagesize); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 188 | mkswap_selinux_setcontext(fd, argv[1]); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 189 | |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 190 | // Make a header. hdr is zero-filled so far... |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 191 | hdr[0] = 1; |
| 192 | hdr[1] = (len / pagesize) - 1; |
Denys Vlasenko | c9f280c | 2009-06-18 21:55:47 +0200 | [diff] [blame] | 193 | mkswap_generate_uuid((void*) &hdr[3]); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 194 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 195 | // Write the header. Sync to disk because some kernel versions check |
| 196 | // signature on disk (not in cache) during swapon. |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 197 | xlseek(fd, 1024, SEEK_SET); |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 198 | xwrite(fd, hdr, NWORDS * 4); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 199 | xlseek(fd, pagesize - 10, SEEK_SET); |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 200 | xwrite(fd, SWAPSPACE2, 10); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 201 | fsync(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 202 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 203 | if (ENABLE_FEATURE_CLEAN_UP) close(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 204 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 205 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 206 | } |