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 | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 53 | /* from Linux 2.6.23 */ |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 54 | /* |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 55 | * Magic header for a swap area. ... Note that the first |
| 56 | * kilobyte is reserved for boot loader or disk label stuff. |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 57 | */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 58 | struct swap_header_v1 { |
| 59 | /* char bootbits[1024]; Space for disklabel etc. */ |
| 60 | uint32_t version; /* second kbyte, word 0 */ |
| 61 | uint32_t last_page; /* 1 */ |
| 62 | uint32_t nr_badpages; /* 2 */ |
| 63 | char sws_uuid[16]; /* 3,4,5,6 */ |
| 64 | char sws_volume[16]; /* 7,8,9,10 */ |
| 65 | uint32_t padding[117]; /* 11..127 */ |
| 66 | uint32_t badpages[1]; /* 128 */ |
| 67 | /* total 129 32-bit words in 2nd kilobyte */ |
Denys Vlasenko | 12ca080 | 2010-02-04 18:41:18 +0100 | [diff] [blame^] | 68 | } FIX_ALIASING; |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 69 | |
| 70 | #define NWORDS 129 |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 71 | #define hdr ((struct swap_header_v1*)bb_common_bufsiz1) |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 72 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 73 | struct BUG_sizes { |
| 74 | char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1]; |
| 75 | char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1]; |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* Stored without terminating NUL */ |
| 79 | static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2"; |
| 80 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 81 | int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 82 | int mkswap_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | { |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 84 | int fd; |
| 85 | unsigned pagesize; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 86 | off_t len; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 87 | const char *label = ""; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 88 | |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 89 | opt_complementary = "-1"; /* at least one param */ |
| 90 | /* TODO: -p PAGESZ, -U UUID */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 91 | getopt32(argv, "L:", &label); |
| 92 | argv += optind; |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 93 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 94 | fd = xopen(argv[0], O_WRONLY); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 95 | |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 96 | /* Figure out how big the device is */ |
| 97 | len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1); |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 98 | pagesize = getpagesize(); |
| 99 | len -= pagesize; |
Denys Vlasenko | 40e7d25 | 2010-02-01 23:48:27 +0100 | [diff] [blame] | 100 | |
| 101 | /* Announce our intentions */ |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 102 | printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len); |
| 103 | mkswap_selinux_setcontext(fd, argv[0]); |
| 104 | |
| 105 | /* Make a header. hdr is zero-filled so far... */ |
| 106 | hdr->version = 1; |
| 107 | hdr->last_page = (uoff_t)len / pagesize; |
| 108 | |
Denys Vlasenko | f5a295d | 2009-10-15 22:43:07 +0200 | [diff] [blame] | 109 | if (ENABLE_FEATURE_MKSWAP_UUID) { |
| 110 | char uuid_string[32]; |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 111 | generate_uuid((void*)hdr->sws_uuid); |
| 112 | bin2hex(uuid_string, hdr->sws_uuid, 16); |
Denys Vlasenko | f5a295d | 2009-10-15 22:43:07 +0200 | [diff] [blame] | 113 | /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ |
| 114 | printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n", |
| 115 | uuid_string, |
| 116 | uuid_string+8, |
| 117 | uuid_string+8+4, |
| 118 | uuid_string+8+4+4, |
| 119 | uuid_string+8+4+4+4 |
| 120 | ); |
| 121 | } |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 122 | safe_strncpy(hdr->sws_volume, label, 16); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 123 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 124 | /* Write the header. Sync to disk because some kernel versions check |
| 125 | * signature on disk (not in cache) during swapon. */ |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 126 | xlseek(fd, 1024, SEEK_SET); |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 127 | xwrite(fd, hdr, NWORDS * 4); |
Denis Vlasenko | 85ff862 | 2007-10-19 21:49:48 +0000 | [diff] [blame] | 128 | xlseek(fd, pagesize - 10, SEEK_SET); |
Denis Vlasenko | 9d96af2 | 2008-02-13 15:35:52 +0000 | [diff] [blame] | 129 | xwrite(fd, SWAPSPACE2, 10); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 130 | fsync(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 131 | |
Denys Vlasenko | 68c6746 | 2009-11-03 05:51:20 +0100 | [diff] [blame] | 132 | if (ENABLE_FEATURE_CLEAN_UP) |
| 133 | close(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 134 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 135 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 136 | } |