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