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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | */ |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 8 | |
| 9 | //usage:#define mkswap_trivial_usage |
| 10 | //usage: "[-L LBL] BLOCKDEV [KBYTES]" |
| 11 | //usage:#define mkswap_full_usage "\n\n" |
| 12 | //usage: "Prepare BLOCKDEV to be used as swap partition\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 13 | //usage: "\n -L LBL Label" |
| 14 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 16 | #include "common_bufsiz.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 17 | |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 18 | #if ENABLE_SELINUX |
| 19 | static void mkswap_selinux_setcontext(int fd, const char *path) |
| 20 | { |
| 21 | struct stat stbuf; |
| 22 | |
| 23 | if (!is_selinux_enabled()) |
| 24 | return; |
| 25 | |
Denys Vlasenko | e66a212 | 2011-01-05 11:45:44 +0100 | [diff] [blame] | 26 | xfstat(fd, &stbuf, path); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 27 | if (S_ISREG(stbuf.st_mode)) { |
| 28 | security_context_t newcon; |
| 29 | security_context_t oldcon = NULL; |
| 30 | context_t context; |
| 31 | |
Denis Vlasenko | 95842fb | 2008-04-25 08:43:01 +0000 | [diff] [blame] | 32 | if (fgetfilecon(fd, &oldcon) < 0) { |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 33 | if (errno != ENODATA) |
| 34 | goto error; |
| 35 | if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0) |
| 36 | goto error; |
| 37 | } |
| 38 | context = context_new(oldcon); |
| 39 | if (!context || context_type_set(context, "swapfile_t")) |
| 40 | goto error; |
| 41 | newcon = context_str(context); |
| 42 | if (!newcon) |
| 43 | goto error; |
Bernhard Reutner-Fischer | c6191e9 | 2008-04-24 10:44:31 +0000 | [diff] [blame] | 44 | /* fsetfilecon_raw is hidden */ |
| 45 | if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0) |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 46 | goto error; |
| 47 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 48 | context_free(context); |
| 49 | freecon(oldcon); |
| 50 | } |
| 51 | } |
| 52 | return; |
| 53 | error: |
| 54 | bb_perror_msg_and_die("SELinux relabeling failed"); |
| 55 | } |
| 56 | #else |
Denys Vlasenko | c9f280c | 2009-06-18 21:55:47 +0200 | [diff] [blame] | 57 | # define mkswap_selinux_setcontext(fd, path) ((void)0) |
| 58 | #endif |
| 59 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 60 | /* from Linux 2.6.23 */ |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 61 | /* |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 62 | * Magic header for a swap area. ... Note that the first |
| 63 | * kilobyte is reserved for boot loader or disk label stuff. |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 64 | */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 65 | struct swap_header_v1 { |
| 66 | /* char bootbits[1024]; Space for disklabel etc. */ |
| 67 | uint32_t version; /* second kbyte, word 0 */ |
| 68 | uint32_t last_page; /* 1 */ |
| 69 | uint32_t nr_badpages; /* 2 */ |
| 70 | char sws_uuid[16]; /* 3,4,5,6 */ |
| 71 | char sws_volume[16]; /* 7,8,9,10 */ |
| 72 | uint32_t padding[117]; /* 11..127 */ |
| 73 | uint32_t badpages[1]; /* 128 */ |
| 74 | /* total 129 32-bit words in 2nd kilobyte */ |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 75 | } FIX_ALIASING; |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 76 | |
| 77 | #define NWORDS 129 |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 78 | #define hdr ((struct swap_header_v1*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 79 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 80 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 81 | struct BUG_sizes { |
| 82 | char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1]; |
| 83 | char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1]; |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 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; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 90 | int mkswap_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | { |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 92 | int fd; |
| 93 | unsigned pagesize; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 94 | off_t len; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 95 | const char *label = ""; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 96 | |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 97 | INIT_G(); |
| 98 | |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 99 | opt_complementary = "-1"; /* at least one param */ |
| 100 | /* TODO: -p PAGESZ, -U UUID */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 101 | getopt32(argv, "L:", &label); |
| 102 | argv += optind; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 103 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 104 | fd = xopen(argv[0], O_WRONLY); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 105 | |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 106 | /* Figure out how big the device is */ |
| 107 | len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1); |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 108 | pagesize = getpagesize(); |
| 109 | len -= pagesize; |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 110 | |
| 111 | /* Announce our intentions */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 112 | printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len); |
| 113 | mkswap_selinux_setcontext(fd, argv[0]); |
| 114 | |
Denys Vlasenko | 4e7dd3c | 2010-08-31 01:50:03 +0200 | [diff] [blame] | 115 | /* hdr is zero-filled so far. Clear the first kbyte, or else |
| 116 | * mkswap-ing former FAT partition does NOT erase its signature. |
| 117 | * |
| 118 | * util-linux-ng 2.17.2 claims to erase it only if it does not see |
| 119 | * a partition table and is not run on whole disk. -f forces it. |
| 120 | */ |
| 121 | xwrite(fd, hdr, 1024); |
| 122 | |
| 123 | /* Fill the header. */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 124 | hdr->version = 1; |
| 125 | hdr->last_page = (uoff_t)len / pagesize; |
| 126 | |
Denys Vlasenko | f5a295d | 2009-10-15 22:43:07 +0200 | [diff] [blame] | 127 | if (ENABLE_FEATURE_MKSWAP_UUID) { |
| 128 | char uuid_string[32]; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 129 | generate_uuid((void*)hdr->sws_uuid); |
| 130 | bin2hex(uuid_string, hdr->sws_uuid, 16); |
Denys Vlasenko | f5a295d | 2009-10-15 22:43:07 +0200 | [diff] [blame] | 131 | /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ |
| 132 | printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n", |
| 133 | uuid_string, |
| 134 | uuid_string+8, |
| 135 | uuid_string+8+4, |
| 136 | uuid_string+8+4+4, |
| 137 | uuid_string+8+4+4+4 |
| 138 | ); |
| 139 | } |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 140 | safe_strncpy(hdr->sws_volume, label, 16); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 141 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 142 | /* Write the header. Sync to disk because some kernel versions check |
| 143 | * signature on disk (not in cache) during swapon. */ |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 144 | xwrite(fd, hdr, NWORDS * 4); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 145 | xlseek(fd, pagesize - 10, SEEK_SET); |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 146 | xwrite(fd, SWAPSPACE2, 10); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 147 | fsync(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 148 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 149 | if (ENABLE_FEATURE_CLEAN_UP) |
| 150 | close(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 151 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 152 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 153 | } |