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