Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | 2ab9403 | 2017-10-05 15:33:28 +0200 | [diff] [blame] | 2 | /* |
| 3 | * mkswap.c - format swap device (Linux v1 only) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 4 | * |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 5 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 9 | //config:config MKSWAP |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 10 | //config: bool "mkswap (6.3 kb)" |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: The mkswap utility is used to configure a file or disk partition as |
| 14 | //config: Linux swap space. This allows Linux to use the entire file or |
| 15 | //config: partition as if it were additional RAM, which can greatly increase |
| 16 | //config: the capability of low-memory machines. This additional memory is |
| 17 | //config: much slower than real RAM, but can be very helpful at preventing your |
| 18 | //config: applications being killed by the Linux out of memory (OOM) killer. |
| 19 | //config: Once you have created swap space using 'mkswap' you need to enable |
| 20 | //config: the swap space using the 'swapon' utility. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 21 | //config: |
| 22 | //config:config FEATURE_MKSWAP_UUID |
| 23 | //config: bool "UUID support" |
| 24 | //config: default y |
| 25 | //config: depends on MKSWAP |
| 26 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 27 | //config: Generate swap spaces with universally unique identifiers. |
Denys Vlasenko | dd898c9 | 2016-11-23 11:46:32 +0100 | [diff] [blame] | 28 | |
| 29 | //applet:IF_MKSWAP(APPLET(mkswap, BB_DIR_SBIN, BB_SUID_DROP)) |
| 30 | |
| 31 | //kbuild:lib-$(CONFIG_MKSWAP) += mkswap.o |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 32 | |
| 33 | //usage:#define mkswap_trivial_usage |
| 34 | //usage: "[-L LBL] BLOCKDEV [KBYTES]" |
| 35 | //usage:#define mkswap_full_usage "\n\n" |
| 36 | //usage: "Prepare BLOCKDEV to be used as swap partition\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 37 | //usage: "\n -L LBL Label" |
| 38 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 39 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 40 | #include "common_bufsiz.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 42 | #if ENABLE_SELINUX |
| 43 | static void mkswap_selinux_setcontext(int fd, const char *path) |
| 44 | { |
| 45 | struct stat stbuf; |
| 46 | |
| 47 | if (!is_selinux_enabled()) |
| 48 | return; |
| 49 | |
Denys Vlasenko | e66a212 | 2011-01-05 11:45:44 +0100 | [diff] [blame] | 50 | xfstat(fd, &stbuf, path); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 51 | if (S_ISREG(stbuf.st_mode)) { |
| 52 | security_context_t newcon; |
| 53 | security_context_t oldcon = NULL; |
| 54 | context_t context; |
| 55 | |
Denis Vlasenko | 95842fb | 2008-04-25 08:43:01 +0000 | [diff] [blame] | 56 | if (fgetfilecon(fd, &oldcon) < 0) { |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 57 | if (errno != ENODATA) |
| 58 | goto error; |
| 59 | if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0) |
| 60 | goto error; |
| 61 | } |
| 62 | context = context_new(oldcon); |
| 63 | if (!context || context_type_set(context, "swapfile_t")) |
| 64 | goto error; |
| 65 | newcon = context_str(context); |
| 66 | if (!newcon) |
| 67 | goto error; |
Bernhard Reutner-Fischer | c6191e9 | 2008-04-24 10:44:31 +0000 | [diff] [blame] | 68 | /* fsetfilecon_raw is hidden */ |
| 69 | if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0) |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 70 | goto error; |
| 71 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 72 | context_free(context); |
| 73 | freecon(oldcon); |
| 74 | } |
| 75 | } |
| 76 | return; |
| 77 | error: |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 78 | bb_simple_perror_msg_and_die("SELinux relabeling failed"); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 79 | } |
| 80 | #else |
Denys Vlasenko | c9f280c | 2009-06-18 21:55:47 +0200 | [diff] [blame] | 81 | # define mkswap_selinux_setcontext(fd, path) ((void)0) |
| 82 | #endif |
| 83 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 84 | /* from Linux 2.6.23 */ |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 85 | /* |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 86 | * Magic header for a swap area. ... Note that the first |
| 87 | * kilobyte is reserved for boot loader or disk label stuff. |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 88 | */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 89 | struct swap_header_v1 { |
| 90 | /* char bootbits[1024]; Space for disklabel etc. */ |
| 91 | uint32_t version; /* second kbyte, word 0 */ |
| 92 | uint32_t last_page; /* 1 */ |
| 93 | uint32_t nr_badpages; /* 2 */ |
| 94 | char sws_uuid[16]; /* 3,4,5,6 */ |
| 95 | char sws_volume[16]; /* 7,8,9,10 */ |
| 96 | uint32_t padding[117]; /* 11..127 */ |
| 97 | uint32_t badpages[1]; /* 128 */ |
| 98 | /* total 129 32-bit words in 2nd kilobyte */ |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame] | 99 | } FIX_ALIASING; |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 100 | |
| 101 | #define NWORDS 129 |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 102 | #define hdr ((struct swap_header_v1*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 103 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 104 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 105 | struct BUG_sizes { |
| 106 | char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1]; |
| 107 | char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1]; |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /* Stored without terminating NUL */ |
| 111 | static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2"; |
| 112 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 113 | int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 114 | int mkswap_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | { |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 116 | int fd; |
| 117 | unsigned pagesize; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 118 | off_t len; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 119 | const char *label = ""; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 120 | |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 121 | INIT_G(); |
| 122 | |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 123 | /* TODO: -p PAGESZ, -U UUID */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 124 | getopt32(argv, "^" "L:" "\0" "-1"/*at least one arg*/, &label); |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 125 | argv += optind; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 126 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 127 | fd = xopen(argv[0], O_WRONLY); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 128 | |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 129 | /* Figure out how big the device is */ |
| 130 | len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1); |
Denys Vlasenko | c7b858f | 2020-12-14 18:49:23 +0100 | [diff] [blame] | 131 | pagesize = bb_getpagesize(); |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 132 | len -= pagesize; |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 133 | |
| 134 | /* Announce our intentions */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 135 | printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len); |
| 136 | mkswap_selinux_setcontext(fd, argv[0]); |
| 137 | |
Denys Vlasenko | 4e7dd3c | 2010-08-31 01:50:03 +0200 | [diff] [blame] | 138 | /* hdr is zero-filled so far. Clear the first kbyte, or else |
| 139 | * mkswap-ing former FAT partition does NOT erase its signature. |
| 140 | * |
| 141 | * util-linux-ng 2.17.2 claims to erase it only if it does not see |
| 142 | * a partition table and is not run on whole disk. -f forces it. |
| 143 | */ |
| 144 | xwrite(fd, hdr, 1024); |
| 145 | |
| 146 | /* Fill the header. */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 147 | hdr->version = 1; |
| 148 | hdr->last_page = (uoff_t)len / pagesize; |
| 149 | |
Denys Vlasenko | f5a295d | 2009-10-15 22:43:07 +0200 | [diff] [blame] | 150 | if (ENABLE_FEATURE_MKSWAP_UUID) { |
| 151 | char uuid_string[32]; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 152 | generate_uuid((void*)hdr->sws_uuid); |
| 153 | bin2hex(uuid_string, hdr->sws_uuid, 16); |
Denys Vlasenko | f5a295d | 2009-10-15 22:43:07 +0200 | [diff] [blame] | 154 | /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ |
| 155 | printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n", |
| 156 | uuid_string, |
| 157 | uuid_string+8, |
| 158 | uuid_string+8+4, |
| 159 | uuid_string+8+4+4, |
| 160 | uuid_string+8+4+4+4 |
| 161 | ); |
| 162 | } |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 163 | safe_strncpy(hdr->sws_volume, label, 16); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 164 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 165 | /* Write the header. Sync to disk because some kernel versions check |
| 166 | * signature on disk (not in cache) during swapon. */ |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 167 | xwrite(fd, hdr, NWORDS * 4); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 168 | xlseek(fd, pagesize - 10, SEEK_SET); |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 169 | xwrite(fd, SWAPSPACE2, 10); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 170 | fsync(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 171 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 172 | if (ENABLE_FEATURE_CLEAN_UP) |
| 173 | close(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 174 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 175 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 176 | } |