]>
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); |
54e377b3 | 348 | if (blkdev_get_size(fd, &size) == 0) |
de3822c3 | 349 | size /= ctl->pagesize; |
54e377b3 | 350 | |
6dbe3af9 KZ |
351 | close(fd); |
352 | return size; | |
353 | } | |
354 | ||
979f1dd5 | 355 | #ifdef HAVE_LIBBLKID |
fabf29f4 | 356 | static blkid_probe new_prober(const struct mkswap_control *ctl) |
979f1dd5 KZ |
357 | { |
358 | blkid_probe pr = blkid_new_probe(); | |
359 | if (!pr) | |
360 | errx(EXIT_FAILURE, _("unable to alloc new libblkid probe")); | |
6af18227 | 361 | if (blkid_probe_set_device(pr, ctl->fd, 0, 0)) |
979f1dd5 KZ |
362 | errx(EXIT_FAILURE, _("unable to assign device to libblkid probe")); |
363 | return pr; | |
364 | } | |
365 | #endif | |
366 | ||
fabf29f4 KZ |
367 | static void open_device(struct mkswap_control *ctl) |
368 | { | |
369 | assert(ctl); | |
370 | assert(ctl->devname); | |
371 | ||
372 | if (stat(ctl->devname, &ctl->devstat) < 0) | |
fc14ceba | 373 | err(EXIT_FAILURE, _("stat of %s failed"), ctl->devname); |
80c320fa | 374 | ctl->fd = open_blkdev_or_file(&ctl->devstat, ctl->devname, O_RDWR); |
fabf29f4 KZ |
375 | if (ctl->fd < 0) |
376 | err(EXIT_FAILURE, _("cannot open %s"), ctl->devname); | |
b8671fe7 KZ |
377 | |
378 | if (blkdev_lock(ctl->fd, ctl->devname, ctl->lockmode) != 0) | |
379 | exit(EXIT_FAILURE); | |
380 | ||
80c320fa SK |
381 | if (ctl->check && S_ISREG(ctl->devstat.st_mode)) { |
382 | ctl->check = 0; | |
1f17eefb KZ |
383 | if (!ctl->quiet) |
384 | warnx(_("warning: checking bad blocks from swap file is not supported: %s"), | |
385 | ctl->devname); | |
80c320fa | 386 | } |
fabf29f4 KZ |
387 | } |
388 | ||
389 | static void wipe_device(struct mkswap_control *ctl) | |
ff3bed80 | 390 | { |
566f35bc | 391 | char *type = NULL; |
ff3bed80 | 392 | int zap = 1; |
9206b238 | 393 | #ifdef HAVE_LIBBLKID |
979f1dd5 | 394 | blkid_probe pr = NULL; |
3773bb15 | 395 | const char *v = NULL; |
9206b238 | 396 | #endif |
6af18227 SK |
397 | if (!ctl->force) { |
398 | if (lseek(ctl->fd, 0, SEEK_SET) != 0) | |
00a7d0d2 | 399 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); |
ff3bed80 | 400 | |
64d15476 | 401 | #ifdef HAVE_LIBBLKID |
6af18227 | 402 | pr = new_prober(ctl); |
c1f1b301 MB |
403 | blkid_probe_enable_partitions(pr, 1); |
404 | blkid_probe_enable_superblocks(pr, 0); | |
405 | ||
406 | if (blkid_do_fullprobe(pr) == 0 && | |
f11157e8 KZ |
407 | blkid_probe_lookup_value(pr, "PTTYPE", &v, NULL) == 0 && v) { |
408 | type = xstrdup(v); | |
ff3bed80 | 409 | zap = 0; |
566f35bc | 410 | } |
c1f1b301 MB |
411 | #else |
412 | /* don't zap if compiled without libblkid */ | |
413 | zap = 0; | |
414 | #endif | |
ff3bed80 KZ |
415 | } |
416 | ||
417 | if (zap) { | |
979f1dd5 | 418 | /* |
d68f402c | 419 | * Wipe bootbits |
979f1dd5 | 420 | */ |
d68f402c | 421 | char buf[1024] = { '\0' }; |
ff3bed80 | 422 | |
6af18227 | 423 | if (lseek(ctl->fd, 0, SEEK_SET) != 0) |
00a7d0d2 | 424 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); |
ff3bed80 | 425 | |
6af18227 | 426 | if (write_all(ctl->fd, buf, sizeof(buf))) |
00a7d0d2 | 427 | errx(EXIT_FAILURE, _("unable to erase bootbits sectors")); |
979f1dd5 KZ |
428 | #ifdef HAVE_LIBBLKID |
429 | /* | |
430 | * Wipe rest of the device | |
431 | */ | |
432 | if (!pr) | |
6af18227 | 433 | pr = new_prober(ctl); |
979f1dd5 KZ |
434 | |
435 | blkid_probe_enable_superblocks(pr, 1); | |
436 | blkid_probe_enable_partitions(pr, 0); | |
c1f1b301 | 437 | blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_TYPE); |
979f1dd5 | 438 | |
c1f1b301 | 439 | while (blkid_do_probe(pr) == 0) { |
a0b42dc3 KZ |
440 | const char *data = NULL; |
441 | ||
1f17eefb KZ |
442 | if (!ctl->quiet |
443 | && blkid_probe_lookup_value(pr, "TYPE", &data, NULL) == 0 && data) | |
fabf29f4 | 444 | warnx(_("%s: warning: wiping old %s signature."), ctl->devname, data); |
979f1dd5 | 445 | blkid_do_wipe(pr, 0); |
c1f1b301 | 446 | } |
979f1dd5 | 447 | #endif |
1f17eefb | 448 | } else if (!ctl->quiet) { |
979f1dd5 | 449 | warnx(_("%s: warning: don't erase bootbits sectors"), |
fabf29f4 | 450 | ctl->devname); |
979f1dd5 KZ |
451 | if (type) |
452 | fprintf(stderr, _(" (%s partition table detected). "), type); | |
979f1dd5 KZ |
453 | else |
454 | fprintf(stderr, _(" (compiled without libblkid). ")); | |
8c219bf4 | 455 | fprintf(stderr, _("Use -f to force.\n")); |
ff3bed80 | 456 | } |
acec6eec | 457 | free(type); |
979f1dd5 KZ |
458 | #ifdef HAVE_LIBBLKID |
459 | blkid_free_probe(pr); | |
460 | #endif | |
ff3bed80 KZ |
461 | } |
462 | ||
3ba01c14 KZ |
463 | #define SIGNATURE_OFFSET 1024 |
464 | ||
465 | static void write_header_to_device(struct mkswap_control *ctl) | |
466 | { | |
467 | assert(ctl); | |
468 | assert(ctl->fd > -1); | |
469 | assert(ctl->signature_page); | |
470 | ||
471 | if (lseek(ctl->fd, SIGNATURE_OFFSET, SEEK_SET) != SIGNATURE_OFFSET) | |
472 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); | |
473 | ||
474 | if (write_all(ctl->fd, (char *) ctl->signature_page + SIGNATURE_OFFSET, | |
475 | ctl->pagesize - SIGNATURE_OFFSET) == -1) | |
476 | err(EXIT_FAILURE, | |
477 | _("%s: unable to write signature page"), | |
478 | ctl->devname); | |
479 | } | |
480 | ||
9a83b03c KZ |
481 | int main(int argc, char **argv) |
482 | { | |
2a80192f | 483 | struct mkswap_control ctl = { .fd = -1, .endianness = ENDIANNESS_NATIVE }; |
cc706d9f | 484 | int c, permMask; |
9a83b03c | 485 | uint64_t sz; |
8a101b14 | 486 | int version = SWAP_VERSION; |
9a83b03c | 487 | char *block_count = NULL, *strsz = NULL; |
766dd757 | 488 | #ifdef HAVE_LIBUUID |
7b241808 | 489 | const char *opt_uuid = NULL; |
756bfd01 KZ |
490 | uuid_t uuid_dat; |
491 | #endif | |
b8671fe7 KZ |
492 | enum { |
493 | OPT_LOCK = CHAR_MAX + 1, | |
701f0385 | 494 | OPT_VERBOSE |
b8671fe7 | 495 | }; |
6c7d5ae9 | 496 | static const struct option longopts[] = { |
87918040 SK |
497 | { "check", no_argument, NULL, 'c' }, |
498 | { "force", no_argument, NULL, 'f' }, | |
1f17eefb | 499 | { "quiet", no_argument, NULL, 'q' }, |
87918040 SK |
500 | { "pagesize", required_argument, NULL, 'p' }, |
501 | { "label", required_argument, NULL, 'L' }, | |
502 | { "swapversion", required_argument, NULL, 'v' }, | |
503 | { "uuid", required_argument, NULL, 'U' }, | |
2a80192f | 504 | { "endianness", required_argument, NULL, 'e' }, |
87918040 SK |
505 | { "version", no_argument, NULL, 'V' }, |
506 | { "help", no_argument, NULL, 'h' }, | |
b8671fe7 | 507 | { "lock", optional_argument, NULL, OPT_LOCK }, |
701f0385 | 508 | { "verbose", no_argument, NULL, OPT_VERBOSE }, |
87918040 | 509 | { NULL, 0, NULL, 0 } |
e079c4e6 | 510 | }; |
eb63b9b8 | 511 | |
1f17eefb KZ |
512 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
513 | { 'c', 'q' }, | |
514 | { 0 } | |
515 | }; | |
516 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
517 | ||
7eda085c KZ |
518 | setlocale(LC_ALL, ""); |
519 | bindtextdomain(PACKAGE, LOCALEDIR); | |
520 | textdomain(PACKAGE); | |
2c308875 | 521 | close_stdout_atexit(); |
5c36a0eb | 522 | |
2a80192f | 523 | while((c = getopt_long(argc, argv, "cfp:qL:v:U:e:Vh", longopts, NULL)) != -1) { |
1f17eefb KZ |
524 | |
525 | err_exclusive_options(c, longopts, excl, excl_st); | |
526 | ||
e079c4e6 SK |
527 | switch (c) { |
528 | case 'c': | |
de3822c3 | 529 | ctl.check = 1; |
e079c4e6 SK |
530 | break; |
531 | case 'f': | |
de3822c3 | 532 | ctl.force = 1; |
e079c4e6 SK |
533 | break; |
534 | case 'p': | |
de3822c3 | 535 | ctl.user_pagesize = strtou32_or_err(optarg, _("parsing page size failed")); |
e079c4e6 | 536 | break; |
1f17eefb KZ |
537 | case 'q': |
538 | ctl.quiet = 1; | |
539 | break; | |
e079c4e6 | 540 | case 'L': |
de3822c3 | 541 | ctl.opt_label = optarg; |
e079c4e6 SK |
542 | break; |
543 | case 'v': | |
8c219bf4 | 544 | version = strtos32_or_err(optarg, _("parsing version number failed")); |
de3822c3 SK |
545 | if (version != SWAP_VERSION) |
546 | errx(EXIT_FAILURE, | |
547 | _("swapspace version %d is not supported"), version); | |
e079c4e6 SK |
548 | break; |
549 | case 'U': | |
93bf0f28 | 550 | #ifdef HAVE_LIBUUID |
e079c4e6 | 551 | opt_uuid = optarg; |
93bf0f28 | 552 | #else |
4e096801 | 553 | warnx(_("warning: ignoring -U (UUIDs are unsupported by %s)"), |
e079c4e6 | 554 | program_invocation_short_name); |
93bf0f28 | 555 | #endif |
e079c4e6 | 556 | break; |
2a80192f TW |
557 | case 'e': |
558 | if (strcmp(optarg, "native") == 0) { | |
559 | ctl.endianness = ENDIANNESS_NATIVE; | |
560 | } else if (strcmp(optarg, "little") == 0) { | |
561 | ctl.endianness = ENDIANNESS_LITTLE; | |
562 | } else if (strcmp(optarg, "big") == 0) { | |
563 | ctl.endianness = ENDIANNESS_BIG; | |
564 | } else { | |
565 | errx(EXIT_FAILURE, | |
566 | _("invalid endianness %s is not supported"), optarg); | |
567 | } | |
568 | break; | |
e079c4e6 | 569 | case 'V': |
2c308875 | 570 | print_version(EXIT_SUCCESS); |
b8671fe7 KZ |
571 | break; |
572 | case OPT_LOCK: | |
573 | ctl.lockmode = "1"; | |
574 | if (optarg) { | |
575 | if (*optarg == '=') | |
576 | optarg++; | |
577 | ctl.lockmode = optarg; | |
578 | } | |
579 | break; | |
701f0385 KZ |
580 | case OPT_VERBOSE: |
581 | ctl.verbose = 1; | |
582 | break; | |
e079c4e6 | 583 | case 'h': |
6e1eda6f | 584 | usage(); |
e079c4e6 | 585 | default: |
677ec86c | 586 | errtryhelp(EXIT_FAILURE); |
e079c4e6 SK |
587 | } |
588 | } | |
3ba01c14 | 589 | |
e079c4e6 | 590 | if (optind < argc) |
fabf29f4 | 591 | ctl.devname = argv[optind++]; |
e079c4e6 SK |
592 | if (optind < argc) |
593 | block_count = argv[optind++]; | |
594 | if (optind != argc) { | |
8c219bf4 | 595 | warnx(_("only one device argument is currently supported")); |
6e1eda6f | 596 | errtryhelp(EXIT_FAILURE); |
6dbe3af9 | 597 | } |
eb63b9b8 | 598 | |
766dd757 | 599 | #ifdef HAVE_LIBUUID |
93bf0f28 | 600 | if(opt_uuid) { |
54ef08ed KZ |
601 | if (strcmp(opt_uuid, "clear") == 0) |
602 | uuid_clear(uuid_dat); | |
603 | else if (strcmp(opt_uuid, "random") == 0) | |
604 | uuid_generate_random(uuid_dat); | |
605 | else if (strcmp(opt_uuid, "time") == 0) | |
606 | uuid_generate_time(uuid_dat); | |
607 | else if (uuid_parse(opt_uuid, uuid_dat) != 0) | |
8c219bf4 | 608 | errx(EXIT_FAILURE, _("error: parsing UUID failed")); |
93bf0f28 MS |
609 | } else |
610 | uuid_generate(uuid_dat); | |
de3822c3 | 611 | ctl.uuid = uuid_dat; |
756bfd01 KZ |
612 | #endif |
613 | ||
de3822c3 | 614 | init_signature_page(&ctl); /* get pagesize and allocate signature page */ |
eb63b9b8 | 615 | |
fabf29f4 | 616 | if (!ctl.devname) { |
00a7d0d2 | 617 | warnx(_("error: Nowhere to set up swap on?")); |
6e1eda6f | 618 | errtryhelp(EXIT_FAILURE); |
fd6b7a7f | 619 | } |
eb63b9b8 | 620 | if (block_count) { |
20543e61 | 621 | /* this silly user specified the number of blocks explicitly */ |
33fb5cfd KZ |
622 | uint64_t blks = strtou64_or_err(block_count, |
623 | _("invalid block count argument")); | |
8591859c | 624 | ctl.npages = blks / (ctl.pagesize / 1024); |
eb63b9b8 | 625 | } |
3ba01c14 | 626 | |
de3822c3 | 627 | sz = get_size(&ctl); |
8591859c KZ |
628 | if (!ctl.npages) |
629 | ctl.npages = sz; | |
630 | else if (ctl.npages > sz && !ctl.force) | |
00a7d0d2 SK |
631 | errx(EXIT_FAILURE, |
632 | _("error: " | |
fdbd7bb9 | 633 | "size %llu KiB is larger than device size %"PRIu64" KiB"), |
8591859c | 634 | ctl.npages * (ctl.pagesize / 1024), sz * (ctl.pagesize / 1024)); |
5c36a0eb | 635 | |
8591859c | 636 | if (ctl.npages < MIN_GOODPAGES) |
793a05f8 SK |
637 | errx(EXIT_FAILURE, |
638 | _("error: swap area needs to be at least %ld KiB"), | |
de3822c3 | 639 | (long)(MIN_GOODPAGES * ctl.pagesize / 1024)); |
8591859c | 640 | if (ctl.npages > UINT32_MAX) { |
a1466ab2 | 641 | /* true when swap is bigger than 17.59 terabytes */ |
8591859c | 642 | ctl.npages = UINT32_MAX; |
1f17eefb KZ |
643 | if (!ctl.quiet) |
644 | warnx(_("warning: truncating swap area to %llu KiB"), | |
645 | ctl.npages * ctl.pagesize / 1024); | |
726f69e2 | 646 | } |
5c36a0eb | 647 | |
fabf29f4 | 648 | if (is_mounted(ctl.devname)) |
dceb1f22 | 649 | errx(EXIT_FAILURE, _("error: " |
4e096801 | 650 | "%s is mounted; will not make swapspace"), |
fabf29f4 KZ |
651 | ctl.devname); |
652 | ||
653 | open_device(&ctl); | |
cc706d9f | 654 | permMask = S_ISBLK(ctl.devstat.st_mode) ? 07007 : 07077; |
1f17eefb | 655 | if (!ctl.quiet && (ctl.devstat.st_mode & permMask) != 0) |
8d687723 | 656 | warnx(_("%s: insecure permissions %04o, fix with: chmod %04o %s"), |
cc706d9f | 657 | ctl.devname, ctl.devstat.st_mode & 07777, |
8d687723 | 658 | ~permMask & 0666, ctl.devname); |
1f17eefb KZ |
659 | if (!ctl.quiet |
660 | && getuid() == 0 && S_ISREG(ctl.devstat.st_mode) && ctl.devstat.st_uid != 0) | |
8d687723 SK |
661 | warnx(_("%s: insecure file owner %d, fix with: chown 0:0 %s"), |
662 | ctl.devname, ctl.devstat.st_uid, ctl.devname); | |
cc706d9f | 663 | |
dceb1f22 | 664 | |
de3822c3 SK |
665 | if (ctl.check) |
666 | check_blocks(&ctl); | |
195025c2 | 667 | #ifdef HAVE_LINUX_FIEMAP_H |
1f17eefb | 668 | if (!ctl.quiet && S_ISREG(ctl.devstat.st_mode)) |
195025c2 KZ |
669 | check_extents(&ctl); |
670 | #endif | |
4c85aa3a | 671 | |
6af18227 | 672 | wipe_device(&ctl); |
ff3bed80 | 673 | |
99f78758 | 674 | assert(ctl.hdr); |
2a80192f TW |
675 | ctl.hdr->version = cpu32_to_endianness(version, ctl.endianness); |
676 | ctl.hdr->last_page = cpu32_to_endianness(ctl.npages - 1, ctl.endianness); | |
677 | ctl.hdr->nr_badpages = cpu32_to_endianness(ctl.nbadpages, ctl.endianness); | |
5c36a0eb | 678 | |
8591859c | 679 | if ((ctl.npages - MIN_GOODPAGES) < ctl.nbadpages) |
00a7d0d2 | 680 | errx(EXIT_FAILURE, _("Unable to set up swap-space: unreadable")); |
4c85aa3a | 681 | |
9a83b03c KZ |
682 | sz = (ctl.npages - ctl.nbadpages - 1) * ctl.pagesize; |
683 | strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, sz); | |
684 | ||
1f17eefb KZ |
685 | if (!ctl.quiet) |
686 | printf(_("Setting up swapspace version %d, size = %s (%"PRIu64" bytes)\n"), | |
687 | version, strsz, sz); | |
acec6eec | 688 | free(strsz); |
5c36a0eb | 689 | |
3ba01c14 KZ |
690 | set_signature(&ctl); |
691 | set_uuid_and_label(&ctl); | |
756bfd01 | 692 | |
3ba01c14 | 693 | write_header_to_device(&ctl); |
99f78758 KZ |
694 | |
695 | deinit_signature_page(&ctl); | |
696 | ||
3e18b040 | 697 | #ifdef HAVE_LIBSELINUX |
fabf29f4 | 698 | if (S_ISREG(ctl.devstat.st_mode) && is_selinux_enabled() > 0) { |
ca27216a | 699 | char *context_string, *oldcontext; |
3e18b040 KZ |
700 | context_t newcontext; |
701 | ||
de3822c3 | 702 | if (fgetfilecon(ctl.fd, &oldcontext) < 0) { |
00a7d0d2 SK |
703 | if (errno != ENODATA) |
704 | err(EXIT_FAILURE, | |
8d1b0fe2 | 705 | _("%s: unable to obtain selinux file label"), |
fabf29f4 | 706 | ctl.devname); |
b105446e KZ |
707 | if (ul_selinux_get_default_context(ctl.devname, |
708 | ctl.devstat.st_mode, &oldcontext)) | |
709 | errx(EXIT_FAILURE, | |
710 | _("%s: unable to obtain default selinux file label"), | |
711 | ctl.devname); | |
3e18b040 KZ |
712 | } |
713 | if (!(newcontext = context_new(oldcontext))) | |
00a7d0d2 | 714 | errx(EXIT_FAILURE, _("unable to create new selinux context")); |
3e18b040 | 715 | if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE)) |
00a7d0d2 | 716 | errx(EXIT_FAILURE, _("couldn't compute selinux context")); |
3e18b040 KZ |
717 | |
718 | context_string = context_str(newcontext); | |
719 | ||
720 | if (strcmp(context_string, oldcontext)!=0) { | |
d97dc0ee | 721 | if (fsetfilecon(ctl.fd, context_string) && errno != ENOTSUP) |
00a7d0d2 | 722 | err(EXIT_FAILURE, _("unable to relabel %s to %s"), |
fabf29f4 | 723 | ctl.devname, context_string); |
3e18b040 KZ |
724 | } |
725 | context_free(newcontext); | |
726 | freecon(oldcontext); | |
727 | } | |
728 | #endif | |
833b7e0d SK |
729 | /* |
730 | * A subsequent swapon() will fail if the signature | |
731 | * is not actually on disk. (This is a kernel bug.) | |
732 | * The fsync() in close_fd() will take care of writing. | |
733 | */ | |
de3822c3 | 734 | if (close_fd(ctl.fd) != 0) |
833b7e0d | 735 | err(EXIT_FAILURE, _("write failed")); |
a4d3e778 | 736 | return EXIT_SUCCESS; |
6dbe3af9 | 737 | } |