]>
Commit | Line | Data |
---|---|---|
6dbe3af9 KZ |
1 | /* |
2 | * mkswap.c - set up a linux swap device | |
3 | * | |
8591859c KZ |
4 | * Copyright (C) 1991 Linus Torvalds |
5 | * 20.12.91 - time began. Got VM working yesterday by doing this by hand. | |
3e18b040 | 6 | * |
8591859c KZ |
7 | * Copyright (C) 1999 Jakub Jelinek <jj@ultra.linux.cz> |
8 | * Copyright (C) 2007-2014 Karel Zak <kzak@redhat.com> | |
6dbe3af9 KZ |
9 | */ |
10 | ||
11 | #include <stdio.h> | |
12 | #include <unistd.h> | |
13 | #include <string.h> | |
14 | #include <fcntl.h> | |
15 | #include <stdlib.h> | |
99e6d525 | 16 | #include <limits.h> |
5c36a0eb | 17 | #include <sys/utsname.h> |
6dbe3af9 | 18 | #include <sys/stat.h> |
195025c2 | 19 | #include <sys/ioctl.h> |
3e18b040 | 20 | #include <errno.h> |
e079c4e6 | 21 | #include <getopt.h> |
99f78758 | 22 | #include <assert.h> |
3e18b040 | 23 | #ifdef HAVE_LIBSELINUX |
195025c2 KZ |
24 | # include <selinux/selinux.h> |
25 | # include <selinux/context.h> | |
b105446e | 26 | # include "selinux-utils.h" |
195025c2 KZ |
27 | #endif |
28 | #ifdef HAVE_LINUX_FIEMAP_H | |
29 | # include <linux/fs.h> | |
30 | # include <linux/fiemap.h> | |
3e18b040 KZ |
31 | #endif |
32 | ||
8023c83b | 33 | #include "linux_version.h" |
756bfd01 | 34 | #include "swapheader.h" |
8abcf290 | 35 | #include "strutils.h" |
66ee8158 | 36 | #include "nls.h" |
54e377b3 | 37 | #include "blkdev.h" |
fc68cd49 | 38 | #include "pathnames.h" |
e12c9866 | 39 | #include "all-io.h" |
94a50e28 | 40 | #include "xalloc.h" |
08675263 | 41 | #include "c.h" |
45ca68ec | 42 | #include "closestream.h" |
def478cf | 43 | #include "ismounted.h" |
1f17eefb | 44 | #include "optutils.h" |
2a80192f | 45 | #include "bitops.h" |
66ee8158 | 46 | |
766dd757 | 47 | #ifdef HAVE_LIBUUID |
7ee96990 | 48 | # include <uuid.h> |
756bfd01 KZ |
49 | #endif |
50 | ||
64d15476 | 51 | #ifdef HAVE_LIBBLKID |
566f35bc KZ |
52 | # include <blkid.h> |
53 | #endif | |
54 | ||
de3822c3 | 55 | #define MIN_GOODPAGES 10 |
5c36a0eb | 56 | |
3e18b040 KZ |
57 | #define SELINUX_SWAPFILE_TYPE "swapfile_t" |
58 | ||
2a80192f TW |
59 | enum ENDIANNESS { |
60 | ENDIANNESS_NATIVE, | |
61 | ENDIANNESS_LITTLE, | |
62 | ENDIANNESS_BIG, | |
63 | }; | |
64 | ||
de3822c3 | 65 | struct mkswap_control { |
99f78758 | 66 | struct swap_header_v1_2 *hdr; /* swap header */ |
8591859c KZ |
67 | void *signature_page;/* buffer with swap header */ |
68 | ||
fabf29f4 | 69 | char *devname; /* device or file name */ |
b8671fe7 | 70 | const char *lockmode; /* as specified by --lock */ |
fabf29f4 KZ |
71 | struct stat devstat; /* stat() result */ |
72 | int fd; /* swap file descriptor */ | |
8591859c KZ |
73 | |
74 | unsigned long long npages; /* number of pages */ | |
75 | unsigned long nbadpages; /* number of bad pages */ | |
76 | ||
77 | int user_pagesize; /* --pagesize */ | |
78 | int pagesize; /* final pagesize used for the header */ | |
79 | ||
80 | char *opt_label; /* LABEL as specified on command line */ | |
81 | unsigned char *uuid; /* UUID parsed by libbuuid */ | |
82 | ||
701f0385 KZ |
83 | size_t nbad_extents; |
84 | ||
2a80192f TW |
85 | enum ENDIANNESS endianness; |
86 | ||
8591859c | 87 | unsigned int check:1, /* --check */ |
701f0385 | 88 | verbose:1, /* --verbose */ |
1f17eefb | 89 | quiet:1, /* --quiet */ |
8591859c | 90 | force:1; /* --force */ |
de3822c3 | 91 | }; |
5c36a0eb | 92 | |
2a80192f TW |
93 | static uint32_t cpu32_to_endianness(uint32_t v, enum ENDIANNESS e) |
94 | { | |
95 | switch (e) { | |
96 | case ENDIANNESS_NATIVE: return v; | |
97 | case ENDIANNESS_LITTLE: return cpu_to_le32(v); | |
98 | case ENDIANNESS_BIG: return cpu_to_be32(v); | |
99 | } | |
100 | abort(); | |
101 | } | |
102 | ||
99f78758 | 103 | static void init_signature_page(struct mkswap_control *ctl) |
f2704664 | 104 | { |
076ba5a6 | 105 | const int kernel_pagesize = getpagesize(); |
eb63b9b8 | 106 | |
de3822c3 SK |
107 | if (ctl->user_pagesize) { |
108 | if (ctl->user_pagesize < 0 || !is_power_of_2(ctl->user_pagesize) || | |
109 | (size_t) ctl->user_pagesize < sizeof(struct swap_header_v1_2) + 10) | |
00a7d0d2 | 110 | errx(EXIT_FAILURE, |
076ba5a6 SK |
111 | _("Bad user-specified page size %u"), |
112 | ctl->user_pagesize); | |
1f17eefb | 113 | if (!ctl->quiet && ctl->user_pagesize != kernel_pagesize) |
076ba5a6 | 114 | warnx(_("Using user-specified page size %d, " |
00a7d0d2 | 115 | "instead of the system value %d"), |
4850e972 | 116 | ctl->user_pagesize, kernel_pagesize); |
076ba5a6 SK |
117 | ctl->pagesize = ctl->user_pagesize; |
118 | } else | |
119 | ctl->pagesize = kernel_pagesize; | |
99f78758 | 120 | |
fea1cbf7 | 121 | ctl->signature_page = xcalloc(1, ctl->pagesize); |
99f78758 KZ |
122 | ctl->hdr = (struct swap_header_v1_2 *) ctl->signature_page; |
123 | } | |
124 | ||
125 | static void deinit_signature_page(struct mkswap_control *ctl) | |
126 | { | |
127 | free(ctl->signature_page); | |
128 | ||
129 | ctl->hdr = NULL; | |
130 | ctl->signature_page = NULL; | |
5c36a0eb KZ |
131 | } |
132 | ||
3ba01c14 | 133 | static void set_signature(const struct mkswap_control *ctl) |
f2704664 | 134 | { |
de3822c3 | 135 | char *sp = (char *) ctl->signature_page; |
5c36a0eb | 136 | |
fabf29f4 | 137 | assert(sp); |
d2f265d6 | 138 | memcpy(sp + ctl->pagesize - SWAP_SIGNATURE_SZ, SWAP_SIGNATURE, SWAP_SIGNATURE_SZ); |
5c36a0eb KZ |
139 | } |
140 | ||
3ba01c14 | 141 | static void set_uuid_and_label(const struct mkswap_control *ctl) |
f2704664 | 142 | { |
99f78758 KZ |
143 | assert(ctl); |
144 | assert(ctl->hdr); | |
756bfd01 | 145 | |
99f78758 | 146 | /* set UUID */ |
de3822c3 | 147 | if (ctl->uuid) |
99f78758 KZ |
148 | memcpy(ctl->hdr->uuid, ctl->uuid, sizeof(ctl->hdr->uuid)); |
149 | ||
150 | /* set LABEL */ | |
de3822c3 | 151 | if (ctl->opt_label) { |
99f78758 KZ |
152 | xstrncpy(ctl->hdr->volume_name, |
153 | ctl->opt_label, sizeof(ctl->hdr->volume_name)); | |
1f17eefb KZ |
154 | if (!ctl->quiet |
155 | && strlen(ctl->opt_label) > strlen(ctl->hdr->volume_name)) | |
00a7d0d2 | 156 | warnx(_("Label was truncated.")); |
756bfd01 | 157 | } |
99f78758 | 158 | |
9e930041 | 159 | /* report results */ |
1f17eefb | 160 | if (!ctl->quiet && (ctl->uuid || ctl->opt_label)) { |
de3822c3 | 161 | if (ctl->opt_label) |
99f78758 | 162 | printf("LABEL=%s, ", ctl->hdr->volume_name); |
756bfd01 KZ |
163 | else |
164 | printf(_("no label, ")); | |
766dd757 | 165 | #ifdef HAVE_LIBUUID |
de3822c3 | 166 | if (ctl->uuid) { |
b443c177 | 167 | char uuid_string[UUID_STR_LEN]; |
de3822c3 | 168 | uuid_unparse(ctl->uuid, uuid_string); |
756bfd01 KZ |
169 | printf("UUID=%s\n", uuid_string); |
170 | } else | |
171 | #endif | |
172 | printf(_("no uuid\n")); | |
173 | } | |
174 | } | |
175 | ||
6e1eda6f | 176 | static void __attribute__((__noreturn__)) usage(void) |
e079c4e6 | 177 | { |
6e1eda6f | 178 | FILE *out = stdout; |
e270d980 KZ |
179 | |
180 | fputs(USAGE_HEADER, out); | |
181 | fprintf(out, _(" %s [options] device [size]\n"), program_invocation_short_name); | |
e079c4e6 | 182 | |
451dbcfa BS |
183 | fputs(USAGE_SEPARATOR, out); |
184 | fputs(_("Set up a Linux swap area.\n"), out); | |
185 | ||
e270d980 KZ |
186 | fputs(USAGE_OPTIONS, out); |
187 | fputs(_(" -c, --check check bad blocks before creating the swap area\n"), out); | |
188 | fputs(_(" -f, --force allow swap size area be larger than device\n"), out); | |
1f17eefb | 189 | fputs(_(" -q, --quiet suppress output and warning messages\n"), out); |
e270d980 KZ |
190 | fputs(_(" -p, --pagesize SIZE specify page size in bytes\n"), out); |
191 | fputs(_(" -L, --label LABEL specify label\n"), out); | |
192 | fputs(_(" -v, --swapversion NUM specify swap-space version number\n"), out); | |
193 | fputs(_(" -U, --uuid UUID specify the uuid to use\n"), out); | |
2a80192f TW |
194 | fprintf(out, |
195 | _(" -e, --endianness=<value> specify the endianness to use " | |
196 | "(%s, %s or %s)\n"), "native", "little", "big"); | |
701f0385 | 197 | fputs(_(" --verbose verbose output\n"), out); |
e270d980 | 198 | |
b8671fe7 KZ |
199 | fprintf(out, |
200 | _(" --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock"); | |
54ef08ed | 201 | |
f45f3ec3 | 202 | printf(USAGE_HELP_OPTIONS(27)); |
e079c4e6 | 203 | |
b3054454 | 204 | printf(USAGE_MAN_TAIL("mkswap(8)")); |
6e1eda6f | 205 | exit(EXIT_SUCCESS); |
eb63b9b8 | 206 | } |
6dbe3af9 | 207 | |
99f78758 | 208 | static void page_bad(struct mkswap_control *ctl, unsigned int page) |
f2704664 | 209 | { |
99f78758 KZ |
210 | const unsigned long max_badpages = |
211 | (ctl->pagesize - 1024 - 128 * sizeof(int) - 10) / sizeof(int); | |
3e16599a | 212 | |
8591859c | 213 | if (ctl->nbadpages == max_badpages) |
d68f402c | 214 | errx(EXIT_FAILURE, _("too many bad pages: %lu"), max_badpages); |
99f78758 KZ |
215 | |
216 | ctl->hdr->badpages[ctl->nbadpages] = page; | |
8591859c | 217 | ctl->nbadpages++; |
5c36a0eb KZ |
218 | } |
219 | ||
99f78758 | 220 | static void check_blocks(struct mkswap_control *ctl) |
f2704664 | 221 | { |
d68f402c | 222 | unsigned int current_page = 0; |
6dbe3af9 | 223 | int do_seek = 1; |
5c36a0eb | 224 | char *buffer; |
6dbe3af9 | 225 | |
99f78758 KZ |
226 | assert(ctl); |
227 | assert(ctl->fd > -1); | |
228 | ||
de3822c3 | 229 | buffer = xmalloc(ctl->pagesize); |
8591859c | 230 | while (current_page < ctl->npages) { |
f3b16286 | 231 | ssize_t rc; |
a6a24f18 | 232 | off_t offset = (off_t) current_page * ctl->pagesize; |
f3b16286 | 233 | |
a6a24f18 | 234 | if (do_seek && lseek(ctl->fd, offset, SEEK_SET) != offset) |
00a7d0d2 | 235 | errx(EXIT_FAILURE, _("seek failed in check_blocks")); |
f3b16286 | 236 | |
de3822c3 SK |
237 | rc = read(ctl->fd, buffer, ctl->pagesize); |
238 | do_seek = (rc < 0 || rc != ctl->pagesize); | |
f3b16286 | 239 | if (do_seek) |
de3822c3 | 240 | page_bad(ctl, current_page); |
adda7f7e | 241 | current_page++; |
6dbe3af9 | 242 | } |
1f17eefb KZ |
243 | |
244 | if (!ctl->quiet) | |
245 | printf(P_("%lu bad page\n", "%lu bad pages\n", ctl->nbadpages), ctl->nbadpages); | |
3216beb0 | 246 | free(buffer); |
6dbe3af9 KZ |
247 | } |
248 | ||
195025c2 KZ |
249 | |
250 | #ifdef HAVE_LINUX_FIEMAP_H | |
701f0385 | 251 | static void warn_extent(struct mkswap_control *ctl, const char *msg, uint64_t off) |
195025c2 | 252 | { |
701f0385 KZ |
253 | if (ctl->nbad_extents == 0) { |
254 | fputc('\n', stderr); | |
255 | fprintf(stderr, _( | |
256 | ||
257 | "mkswap: %s contains holes or other unsupported extents.\n" | |
258 | " This swap file can be rejected by kernel on swap activation!\n"), | |
259 | ctl->devname); | |
260 | ||
261 | if (ctl->verbose) | |
262 | fputc('\n', stderr); | |
263 | else | |
264 | fprintf(stderr, _( | |
265 | " Use --verbose for more details.\n")); | |
266 | ||
267 | } | |
268 | if (ctl->verbose) { | |
269 | fputs(" - ", stderr); | |
270 | fprintf(stderr, msg, off); | |
195025c2 | 271 | fputc('\n', stderr); |
195025c2 | 272 | } |
701f0385 | 273 | ctl->nbad_extents++; |
195025c2 KZ |
274 | } |
275 | ||
276 | static void check_extents(struct mkswap_control *ctl) | |
277 | { | |
278 | char buf[BUFSIZ] = { 0 }; | |
279 | struct fiemap *fiemap = (struct fiemap *) buf; | |
701f0385 | 280 | int last = 0; |
195025c2 KZ |
281 | uint64_t last_logical = 0; |
282 | ||
283 | memset(fiemap, 0, sizeof(struct fiemap)); | |
284 | ||
285 | do { | |
286 | int rc; | |
287 | size_t n, i; | |
288 | ||
289 | fiemap->fm_length = ~0ULL; | |
29b68b9c | 290 | fiemap->fm_flags = FIEMAP_FLAG_SYNC; |
195025c2 KZ |
291 | fiemap->fm_extent_count = |
292 | (sizeof(buf) - sizeof(*fiemap)) / sizeof(struct fiemap_extent); | |
293 | ||
294 | rc = ioctl(ctl->fd, FS_IOC_FIEMAP, (unsigned long) fiemap); | |
199cd674 | 295 | if (rc < 0) |
195025c2 | 296 | return; |
195025c2 KZ |
297 | |
298 | n = fiemap->fm_mapped_extents; | |
8a3a7416 KZ |
299 | if (n == 0) |
300 | break; | |
195025c2 KZ |
301 | |
302 | for (i = 0; i < n; i++) { | |
303 | struct fiemap_extent *e = &fiemap->fm_extents[i]; | |
304 | ||
701f0385 KZ |
305 | if (e->fe_logical > last_logical) |
306 | warn_extent(ctl, _("hole detected at offset %ju"), | |
307 | (uintmax_t) last_logical); | |
195025c2 KZ |
308 | |
309 | last_logical = (e->fe_logical + e->fe_length); | |
310 | ||
311 | if (e->fe_flags & FIEMAP_EXTENT_LAST) | |
312 | last = 1; | |
701f0385 KZ |
313 | if (e->fe_flags & FIEMAP_EXTENT_DATA_INLINE) |
314 | warn_extent(ctl, _("data inline extent at offset %ju"), | |
29b68b9c | 315 | (uintmax_t) e->fe_logical); |
701f0385 | 316 | if (e->fe_flags & FIEMAP_EXTENT_SHARED) |
1f17eefb | 317 | warn_extent(ctl, _("shared extent at offset %ju"), |
29b68b9c | 318 | (uintmax_t) e->fe_logical); |
701f0385 KZ |
319 | if (e->fe_flags & FIEMAP_EXTENT_DELALLOC) |
320 | warn_extent(ctl, _("unallocated extent at offset %ju"), | |
29b68b9c | 321 | (uintmax_t) e->fe_logical); |
195025c2 | 322 | |
701f0385 KZ |
323 | if (!ctl->verbose && ctl->nbad_extents) |
324 | goto done; | |
195025c2 KZ |
325 | } |
326 | fiemap->fm_start = fiemap->fm_extents[n - 1].fe_logical | |
327 | + fiemap->fm_extents[n - 1].fe_length; | |
328 | } while (last == 0); | |
329 | ||
701f0385 KZ |
330 | if (last_logical < (uint64_t) ctl->devstat.st_size) |
331 | warn_extent(ctl, _("hole detected at offset %ju"), | |
332 | (uintmax_t) last_logical); | |
333 | done: | |
334 | if (ctl->nbad_extents) | |
335 | fputc('\n', stderr); | |
195025c2 KZ |
336 | } |
337 | #endif /* HAVE_LINUX_FIEMAP_H */ | |
338 | ||
99e6d525 | 339 | /* return size in pages */ |
fabf29f4 | 340 | static unsigned long long get_size(const struct mkswap_control *ctl) |
f2704664 SK |
341 | { |
342 | int fd; | |
54e377b3 | 343 | unsigned long long size; |
6dbe3af9 | 344 | |
fabf29f4 | 345 | fd = open(ctl->devname, O_RDONLY); |
3d5c8ba1 | 346 | if (fd < 0) |
fabf29f4 | 347 | err(EXIT_FAILURE, _("cannot open %s"), ctl->devname); |
6c88722c SN |
348 | if (blkdev_get_size(fd, &size) < 0) |
349 | err(EXIT_FAILURE, _("cannot determine size of %s"), ctl->devname); | |
350 | size /= ctl->pagesize; | |
54e377b3 | 351 | |
6dbe3af9 KZ |
352 | close(fd); |
353 | return size; | |
354 | } | |
355 | ||
979f1dd5 | 356 | #ifdef HAVE_LIBBLKID |
fabf29f4 | 357 | static blkid_probe new_prober(const struct mkswap_control *ctl) |
979f1dd5 KZ |
358 | { |
359 | blkid_probe pr = blkid_new_probe(); | |
360 | if (!pr) | |
361 | errx(EXIT_FAILURE, _("unable to alloc new libblkid probe")); | |
6af18227 | 362 | if (blkid_probe_set_device(pr, ctl->fd, 0, 0)) |
979f1dd5 KZ |
363 | errx(EXIT_FAILURE, _("unable to assign device to libblkid probe")); |
364 | return pr; | |
365 | } | |
366 | #endif | |
367 | ||
fabf29f4 KZ |
368 | static void open_device(struct mkswap_control *ctl) |
369 | { | |
370 | assert(ctl); | |
371 | assert(ctl->devname); | |
372 | ||
373 | if (stat(ctl->devname, &ctl->devstat) < 0) | |
fc14ceba | 374 | err(EXIT_FAILURE, _("stat of %s failed"), ctl->devname); |
80c320fa | 375 | ctl->fd = open_blkdev_or_file(&ctl->devstat, ctl->devname, O_RDWR); |
fabf29f4 KZ |
376 | if (ctl->fd < 0) |
377 | err(EXIT_FAILURE, _("cannot open %s"), ctl->devname); | |
b8671fe7 KZ |
378 | |
379 | if (blkdev_lock(ctl->fd, ctl->devname, ctl->lockmode) != 0) | |
380 | exit(EXIT_FAILURE); | |
381 | ||
80c320fa SK |
382 | if (ctl->check && S_ISREG(ctl->devstat.st_mode)) { |
383 | ctl->check = 0; | |
1f17eefb KZ |
384 | if (!ctl->quiet) |
385 | warnx(_("warning: checking bad blocks from swap file is not supported: %s"), | |
386 | ctl->devname); | |
80c320fa | 387 | } |
fabf29f4 KZ |
388 | } |
389 | ||
390 | static void wipe_device(struct mkswap_control *ctl) | |
ff3bed80 | 391 | { |
566f35bc | 392 | char *type = NULL; |
ff3bed80 | 393 | int zap = 1; |
9206b238 | 394 | #ifdef HAVE_LIBBLKID |
979f1dd5 | 395 | blkid_probe pr = NULL; |
3773bb15 | 396 | const char *v = NULL; |
9206b238 | 397 | #endif |
6af18227 SK |
398 | if (!ctl->force) { |
399 | if (lseek(ctl->fd, 0, SEEK_SET) != 0) | |
00a7d0d2 | 400 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); |
ff3bed80 | 401 | |
64d15476 | 402 | #ifdef HAVE_LIBBLKID |
6af18227 | 403 | pr = new_prober(ctl); |
c1f1b301 MB |
404 | blkid_probe_enable_partitions(pr, 1); |
405 | blkid_probe_enable_superblocks(pr, 0); | |
406 | ||
407 | if (blkid_do_fullprobe(pr) == 0 && | |
f11157e8 KZ |
408 | blkid_probe_lookup_value(pr, "PTTYPE", &v, NULL) == 0 && v) { |
409 | type = xstrdup(v); | |
ff3bed80 | 410 | zap = 0; |
566f35bc | 411 | } |
c1f1b301 MB |
412 | #else |
413 | /* don't zap if compiled without libblkid */ | |
414 | zap = 0; | |
415 | #endif | |
ff3bed80 KZ |
416 | } |
417 | ||
418 | if (zap) { | |
979f1dd5 | 419 | /* |
d68f402c | 420 | * Wipe bootbits |
979f1dd5 | 421 | */ |
d68f402c | 422 | char buf[1024] = { '\0' }; |
ff3bed80 | 423 | |
6af18227 | 424 | if (lseek(ctl->fd, 0, SEEK_SET) != 0) |
00a7d0d2 | 425 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); |
ff3bed80 | 426 | |
6af18227 | 427 | if (write_all(ctl->fd, buf, sizeof(buf))) |
00a7d0d2 | 428 | errx(EXIT_FAILURE, _("unable to erase bootbits sectors")); |
979f1dd5 KZ |
429 | #ifdef HAVE_LIBBLKID |
430 | /* | |
431 | * Wipe rest of the device | |
432 | */ | |
433 | if (!pr) | |
6af18227 | 434 | pr = new_prober(ctl); |
979f1dd5 KZ |
435 | |
436 | blkid_probe_enable_superblocks(pr, 1); | |
437 | blkid_probe_enable_partitions(pr, 0); | |
c1f1b301 | 438 | blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_TYPE); |
979f1dd5 | 439 | |
c1f1b301 | 440 | while (blkid_do_probe(pr) == 0) { |
a0b42dc3 KZ |
441 | const char *data = NULL; |
442 | ||
1f17eefb KZ |
443 | if (!ctl->quiet |
444 | && blkid_probe_lookup_value(pr, "TYPE", &data, NULL) == 0 && data) | |
fabf29f4 | 445 | warnx(_("%s: warning: wiping old %s signature."), ctl->devname, data); |
979f1dd5 | 446 | blkid_do_wipe(pr, 0); |
c1f1b301 | 447 | } |
979f1dd5 | 448 | #endif |
1f17eefb | 449 | } else if (!ctl->quiet) { |
979f1dd5 | 450 | warnx(_("%s: warning: don't erase bootbits sectors"), |
fabf29f4 | 451 | ctl->devname); |
979f1dd5 KZ |
452 | if (type) |
453 | fprintf(stderr, _(" (%s partition table detected). "), type); | |
979f1dd5 KZ |
454 | else |
455 | fprintf(stderr, _(" (compiled without libblkid). ")); | |
8c219bf4 | 456 | fprintf(stderr, _("Use -f to force.\n")); |
ff3bed80 | 457 | } |
acec6eec | 458 | free(type); |
979f1dd5 KZ |
459 | #ifdef HAVE_LIBBLKID |
460 | blkid_free_probe(pr); | |
461 | #endif | |
ff3bed80 KZ |
462 | } |
463 | ||
3ba01c14 KZ |
464 | #define SIGNATURE_OFFSET 1024 |
465 | ||
466 | static void write_header_to_device(struct mkswap_control *ctl) | |
467 | { | |
468 | assert(ctl); | |
469 | assert(ctl->fd > -1); | |
470 | assert(ctl->signature_page); | |
471 | ||
472 | if (lseek(ctl->fd, SIGNATURE_OFFSET, SEEK_SET) != SIGNATURE_OFFSET) | |
473 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); | |
474 | ||
475 | if (write_all(ctl->fd, (char *) ctl->signature_page + SIGNATURE_OFFSET, | |
476 | ctl->pagesize - SIGNATURE_OFFSET) == -1) | |
477 | err(EXIT_FAILURE, | |
478 | _("%s: unable to write signature page"), | |
479 | ctl->devname); | |
480 | } | |
481 | ||
9a83b03c KZ |
482 | int main(int argc, char **argv) |
483 | { | |
2a80192f | 484 | struct mkswap_control ctl = { .fd = -1, .endianness = ENDIANNESS_NATIVE }; |
cc706d9f | 485 | int c, permMask; |
9a83b03c | 486 | uint64_t sz; |
8a101b14 | 487 | int version = SWAP_VERSION; |
9a83b03c | 488 | char *block_count = NULL, *strsz = NULL; |
766dd757 | 489 | #ifdef HAVE_LIBUUID |
7b241808 | 490 | const char *opt_uuid = NULL; |
756bfd01 KZ |
491 | uuid_t uuid_dat; |
492 | #endif | |
b8671fe7 KZ |
493 | enum { |
494 | OPT_LOCK = CHAR_MAX + 1, | |
701f0385 | 495 | OPT_VERBOSE |
b8671fe7 | 496 | }; |
6c7d5ae9 | 497 | static const struct option longopts[] = { |
87918040 SK |
498 | { "check", no_argument, NULL, 'c' }, |
499 | { "force", no_argument, NULL, 'f' }, | |
1f17eefb | 500 | { "quiet", no_argument, NULL, 'q' }, |
87918040 SK |
501 | { "pagesize", required_argument, NULL, 'p' }, |
502 | { "label", required_argument, NULL, 'L' }, | |
503 | { "swapversion", required_argument, NULL, 'v' }, | |
504 | { "uuid", required_argument, NULL, 'U' }, | |
2a80192f | 505 | { "endianness", required_argument, NULL, 'e' }, |
87918040 SK |
506 | { "version", no_argument, NULL, 'V' }, |
507 | { "help", no_argument, NULL, 'h' }, | |
b8671fe7 | 508 | { "lock", optional_argument, NULL, OPT_LOCK }, |
701f0385 | 509 | { "verbose", no_argument, NULL, OPT_VERBOSE }, |
87918040 | 510 | { NULL, 0, NULL, 0 } |
e079c4e6 | 511 | }; |
eb63b9b8 | 512 | |
1f17eefb KZ |
513 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
514 | { 'c', 'q' }, | |
515 | { 0 } | |
516 | }; | |
517 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
518 | ||
7eda085c KZ |
519 | setlocale(LC_ALL, ""); |
520 | bindtextdomain(PACKAGE, LOCALEDIR); | |
521 | textdomain(PACKAGE); | |
2c308875 | 522 | close_stdout_atexit(); |
5c36a0eb | 523 | |
2a80192f | 524 | while((c = getopt_long(argc, argv, "cfp:qL:v:U:e:Vh", longopts, NULL)) != -1) { |
1f17eefb KZ |
525 | |
526 | err_exclusive_options(c, longopts, excl, excl_st); | |
527 | ||
e079c4e6 SK |
528 | switch (c) { |
529 | case 'c': | |
de3822c3 | 530 | ctl.check = 1; |
e079c4e6 SK |
531 | break; |
532 | case 'f': | |
de3822c3 | 533 | ctl.force = 1; |
e079c4e6 SK |
534 | break; |
535 | case 'p': | |
de3822c3 | 536 | ctl.user_pagesize = strtou32_or_err(optarg, _("parsing page size failed")); |
e079c4e6 | 537 | break; |
1f17eefb KZ |
538 | case 'q': |
539 | ctl.quiet = 1; | |
540 | break; | |
e079c4e6 | 541 | case 'L': |
de3822c3 | 542 | ctl.opt_label = optarg; |
e079c4e6 SK |
543 | break; |
544 | case 'v': | |
8c219bf4 | 545 | version = strtos32_or_err(optarg, _("parsing version number failed")); |
de3822c3 SK |
546 | if (version != SWAP_VERSION) |
547 | errx(EXIT_FAILURE, | |
548 | _("swapspace version %d is not supported"), version); | |
e079c4e6 SK |
549 | break; |
550 | case 'U': | |
93bf0f28 | 551 | #ifdef HAVE_LIBUUID |
e079c4e6 | 552 | opt_uuid = optarg; |
93bf0f28 | 553 | #else |
4e096801 | 554 | warnx(_("warning: ignoring -U (UUIDs are unsupported by %s)"), |
e079c4e6 | 555 | program_invocation_short_name); |
93bf0f28 | 556 | #endif |
e079c4e6 | 557 | break; |
2a80192f TW |
558 | case 'e': |
559 | if (strcmp(optarg, "native") == 0) { | |
560 | ctl.endianness = ENDIANNESS_NATIVE; | |
561 | } else if (strcmp(optarg, "little") == 0) { | |
562 | ctl.endianness = ENDIANNESS_LITTLE; | |
563 | } else if (strcmp(optarg, "big") == 0) { | |
564 | ctl.endianness = ENDIANNESS_BIG; | |
565 | } else { | |
566 | errx(EXIT_FAILURE, | |
567 | _("invalid endianness %s is not supported"), optarg); | |
568 | } | |
569 | break; | |
e079c4e6 | 570 | case 'V': |
2c308875 | 571 | print_version(EXIT_SUCCESS); |
b8671fe7 KZ |
572 | break; |
573 | case OPT_LOCK: | |
574 | ctl.lockmode = "1"; | |
575 | if (optarg) { | |
576 | if (*optarg == '=') | |
577 | optarg++; | |
578 | ctl.lockmode = optarg; | |
579 | } | |
580 | break; | |
701f0385 KZ |
581 | case OPT_VERBOSE: |
582 | ctl.verbose = 1; | |
583 | break; | |
e079c4e6 | 584 | case 'h': |
6e1eda6f | 585 | usage(); |
e079c4e6 | 586 | default: |
677ec86c | 587 | errtryhelp(EXIT_FAILURE); |
e079c4e6 SK |
588 | } |
589 | } | |
3ba01c14 | 590 | |
e079c4e6 | 591 | if (optind < argc) |
fabf29f4 | 592 | ctl.devname = argv[optind++]; |
e079c4e6 SK |
593 | if (optind < argc) |
594 | block_count = argv[optind++]; | |
595 | if (optind != argc) { | |
8c219bf4 | 596 | warnx(_("only one device argument is currently supported")); |
6e1eda6f | 597 | errtryhelp(EXIT_FAILURE); |
6dbe3af9 | 598 | } |
eb63b9b8 | 599 | |
766dd757 | 600 | #ifdef HAVE_LIBUUID |
93bf0f28 | 601 | if(opt_uuid) { |
54ef08ed KZ |
602 | if (strcmp(opt_uuid, "clear") == 0) |
603 | uuid_clear(uuid_dat); | |
604 | else if (strcmp(opt_uuid, "random") == 0) | |
605 | uuid_generate_random(uuid_dat); | |
606 | else if (strcmp(opt_uuid, "time") == 0) | |
607 | uuid_generate_time(uuid_dat); | |
608 | else if (uuid_parse(opt_uuid, uuid_dat) != 0) | |
8c219bf4 | 609 | errx(EXIT_FAILURE, _("error: parsing UUID failed")); |
93bf0f28 MS |
610 | } else |
611 | uuid_generate(uuid_dat); | |
de3822c3 | 612 | ctl.uuid = uuid_dat; |
756bfd01 KZ |
613 | #endif |
614 | ||
de3822c3 | 615 | init_signature_page(&ctl); /* get pagesize and allocate signature page */ |
eb63b9b8 | 616 | |
fabf29f4 | 617 | if (!ctl.devname) { |
00a7d0d2 | 618 | warnx(_("error: Nowhere to set up swap on?")); |
6e1eda6f | 619 | errtryhelp(EXIT_FAILURE); |
fd6b7a7f | 620 | } |
eb63b9b8 | 621 | if (block_count) { |
20543e61 | 622 | /* this silly user specified the number of blocks explicitly */ |
33fb5cfd KZ |
623 | uint64_t blks = strtou64_or_err(block_count, |
624 | _("invalid block count argument")); | |
8591859c | 625 | ctl.npages = blks / (ctl.pagesize / 1024); |
eb63b9b8 | 626 | } |
3ba01c14 | 627 | |
de3822c3 | 628 | sz = get_size(&ctl); |
8591859c KZ |
629 | if (!ctl.npages) |
630 | ctl.npages = sz; | |
631 | else if (ctl.npages > sz && !ctl.force) | |
00a7d0d2 SK |
632 | errx(EXIT_FAILURE, |
633 | _("error: " | |
fdbd7bb9 | 634 | "size %llu KiB is larger than device size %"PRIu64" KiB"), |
8591859c | 635 | ctl.npages * (ctl.pagesize / 1024), sz * (ctl.pagesize / 1024)); |
5c36a0eb | 636 | |
8591859c | 637 | if (ctl.npages < MIN_GOODPAGES) |
793a05f8 SK |
638 | errx(EXIT_FAILURE, |
639 | _("error: swap area needs to be at least %ld KiB"), | |
de3822c3 | 640 | (long)(MIN_GOODPAGES * ctl.pagesize / 1024)); |
8591859c | 641 | if (ctl.npages > UINT32_MAX) { |
a1466ab2 | 642 | /* true when swap is bigger than 17.59 terabytes */ |
8591859c | 643 | ctl.npages = UINT32_MAX; |
1f17eefb KZ |
644 | if (!ctl.quiet) |
645 | warnx(_("warning: truncating swap area to %llu KiB"), | |
646 | ctl.npages * ctl.pagesize / 1024); | |
726f69e2 | 647 | } |
5c36a0eb | 648 | |
fabf29f4 | 649 | if (is_mounted(ctl.devname)) |
dceb1f22 | 650 | errx(EXIT_FAILURE, _("error: " |
4e096801 | 651 | "%s is mounted; will not make swapspace"), |
fabf29f4 KZ |
652 | ctl.devname); |
653 | ||
654 | open_device(&ctl); | |
cc706d9f | 655 | permMask = S_ISBLK(ctl.devstat.st_mode) ? 07007 : 07077; |
1f17eefb | 656 | if (!ctl.quiet && (ctl.devstat.st_mode & permMask) != 0) |
8d687723 | 657 | warnx(_("%s: insecure permissions %04o, fix with: chmod %04o %s"), |
cc706d9f | 658 | ctl.devname, ctl.devstat.st_mode & 07777, |
8d687723 | 659 | ~permMask & 0666, ctl.devname); |
1f17eefb KZ |
660 | if (!ctl.quiet |
661 | && getuid() == 0 && S_ISREG(ctl.devstat.st_mode) && ctl.devstat.st_uid != 0) | |
8d687723 SK |
662 | warnx(_("%s: insecure file owner %d, fix with: chown 0:0 %s"), |
663 | ctl.devname, ctl.devstat.st_uid, ctl.devname); | |
cc706d9f | 664 | |
dceb1f22 | 665 | |
de3822c3 SK |
666 | if (ctl.check) |
667 | check_blocks(&ctl); | |
195025c2 | 668 | #ifdef HAVE_LINUX_FIEMAP_H |
1f17eefb | 669 | if (!ctl.quiet && S_ISREG(ctl.devstat.st_mode)) |
195025c2 KZ |
670 | check_extents(&ctl); |
671 | #endif | |
4c85aa3a | 672 | |
6af18227 | 673 | wipe_device(&ctl); |
ff3bed80 | 674 | |
99f78758 | 675 | assert(ctl.hdr); |
2a80192f TW |
676 | ctl.hdr->version = cpu32_to_endianness(version, ctl.endianness); |
677 | ctl.hdr->last_page = cpu32_to_endianness(ctl.npages - 1, ctl.endianness); | |
678 | ctl.hdr->nr_badpages = cpu32_to_endianness(ctl.nbadpages, ctl.endianness); | |
5c36a0eb | 679 | |
8591859c | 680 | if ((ctl.npages - MIN_GOODPAGES) < ctl.nbadpages) |
00a7d0d2 | 681 | errx(EXIT_FAILURE, _("Unable to set up swap-space: unreadable")); |
4c85aa3a | 682 | |
9a83b03c KZ |
683 | sz = (ctl.npages - ctl.nbadpages - 1) * ctl.pagesize; |
684 | strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, sz); | |
685 | ||
1f17eefb KZ |
686 | if (!ctl.quiet) |
687 | printf(_("Setting up swapspace version %d, size = %s (%"PRIu64" bytes)\n"), | |
688 | version, strsz, sz); | |
acec6eec | 689 | free(strsz); |
5c36a0eb | 690 | |
3ba01c14 KZ |
691 | set_signature(&ctl); |
692 | set_uuid_and_label(&ctl); | |
756bfd01 | 693 | |
3ba01c14 | 694 | write_header_to_device(&ctl); |
99f78758 KZ |
695 | |
696 | deinit_signature_page(&ctl); | |
697 | ||
3e18b040 | 698 | #ifdef HAVE_LIBSELINUX |
fabf29f4 | 699 | if (S_ISREG(ctl.devstat.st_mode) && is_selinux_enabled() > 0) { |
4bbc219b TW |
700 | const char *context_string; |
701 | char *oldcontext; | |
3e18b040 KZ |
702 | context_t newcontext; |
703 | ||
de3822c3 | 704 | if (fgetfilecon(ctl.fd, &oldcontext) < 0) { |
00a7d0d2 SK |
705 | if (errno != ENODATA) |
706 | err(EXIT_FAILURE, | |
8d1b0fe2 | 707 | _("%s: unable to obtain selinux file label"), |
fabf29f4 | 708 | ctl.devname); |
b105446e KZ |
709 | if (ul_selinux_get_default_context(ctl.devname, |
710 | ctl.devstat.st_mode, &oldcontext)) | |
711 | errx(EXIT_FAILURE, | |
712 | _("%s: unable to obtain default selinux file label"), | |
713 | ctl.devname); | |
3e18b040 KZ |
714 | } |
715 | if (!(newcontext = context_new(oldcontext))) | |
00a7d0d2 | 716 | errx(EXIT_FAILURE, _("unable to create new selinux context")); |
3e18b040 | 717 | if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE)) |
00a7d0d2 | 718 | errx(EXIT_FAILURE, _("couldn't compute selinux context")); |
3e18b040 KZ |
719 | |
720 | context_string = context_str(newcontext); | |
721 | ||
722 | if (strcmp(context_string, oldcontext)!=0) { | |
d97dc0ee | 723 | if (fsetfilecon(ctl.fd, context_string) && errno != ENOTSUP) |
00a7d0d2 | 724 | err(EXIT_FAILURE, _("unable to relabel %s to %s"), |
fabf29f4 | 725 | ctl.devname, context_string); |
3e18b040 KZ |
726 | } |
727 | context_free(newcontext); | |
728 | freecon(oldcontext); | |
729 | } | |
730 | #endif | |
833b7e0d SK |
731 | /* |
732 | * A subsequent swapon() will fail if the signature | |
733 | * is not actually on disk. (This is a kernel bug.) | |
734 | * The fsync() in close_fd() will take care of writing. | |
735 | */ | |
de3822c3 | 736 | if (close_fd(ctl.fd) != 0) |
833b7e0d | 737 | err(EXIT_FAILURE, _("write failed")); |
a4d3e778 | 738 | return EXIT_SUCCESS; |
6dbe3af9 | 739 | } |