]>
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> |
8abb4c2e | 17 | #include <mntent.h> |
5c36a0eb | 18 | #include <sys/utsname.h> |
6dbe3af9 | 19 | #include <sys/stat.h> |
3e18b040 | 20 | #include <errno.h> |
e079c4e6 | 21 | #include <getopt.h> |
99f78758 | 22 | #include <assert.h> |
3e18b040 KZ |
23 | #ifdef HAVE_LIBSELINUX |
24 | #include <selinux/selinux.h> | |
25 | #include <selinux/context.h> | |
26 | #endif | |
27 | ||
8023c83b | 28 | #include "linux_version.h" |
756bfd01 | 29 | #include "swapheader.h" |
8abcf290 | 30 | #include "strutils.h" |
66ee8158 | 31 | #include "nls.h" |
54e377b3 | 32 | #include "blkdev.h" |
fc68cd49 | 33 | #include "pathnames.h" |
e12c9866 | 34 | #include "all-io.h" |
94a50e28 | 35 | #include "xalloc.h" |
08675263 | 36 | #include "c.h" |
45ca68ec | 37 | #include "closestream.h" |
def478cf | 38 | #include "ismounted.h" |
66ee8158 | 39 | |
766dd757 | 40 | #ifdef HAVE_LIBUUID |
7ee96990 | 41 | # include <uuid.h> |
756bfd01 KZ |
42 | #endif |
43 | ||
64d15476 | 44 | #ifdef HAVE_LIBBLKID |
566f35bc KZ |
45 | # include <blkid.h> |
46 | #endif | |
47 | ||
de3822c3 | 48 | #define MIN_GOODPAGES 10 |
5c36a0eb | 49 | |
3e18b040 KZ |
50 | #define SELINUX_SWAPFILE_TYPE "swapfile_t" |
51 | ||
de3822c3 | 52 | struct mkswap_control { |
99f78758 | 53 | struct swap_header_v1_2 *hdr; /* swap header */ |
8591859c KZ |
54 | void *signature_page;/* buffer with swap header */ |
55 | ||
fabf29f4 KZ |
56 | char *devname; /* device or file name */ |
57 | struct stat devstat; /* stat() result */ | |
58 | int fd; /* swap file descriptor */ | |
8591859c KZ |
59 | |
60 | unsigned long long npages; /* number of pages */ | |
61 | unsigned long nbadpages; /* number of bad pages */ | |
62 | ||
63 | int user_pagesize; /* --pagesize */ | |
64 | int pagesize; /* final pagesize used for the header */ | |
65 | ||
66 | char *opt_label; /* LABEL as specified on command line */ | |
67 | unsigned char *uuid; /* UUID parsed by libbuuid */ | |
68 | ||
69 | unsigned int check:1, /* --check */ | |
70 | force:1; /* --force */ | |
de3822c3 | 71 | }; |
5c36a0eb | 72 | |
99f78758 | 73 | static void init_signature_page(struct mkswap_control *ctl) |
f2704664 | 74 | { |
076ba5a6 | 75 | const int kernel_pagesize = getpagesize(); |
eb63b9b8 | 76 | |
de3822c3 SK |
77 | if (ctl->user_pagesize) { |
78 | if (ctl->user_pagesize < 0 || !is_power_of_2(ctl->user_pagesize) || | |
79 | (size_t) ctl->user_pagesize < sizeof(struct swap_header_v1_2) + 10) | |
00a7d0d2 | 80 | errx(EXIT_FAILURE, |
076ba5a6 SK |
81 | _("Bad user-specified page size %u"), |
82 | ctl->user_pagesize); | |
83 | if (ctl->user_pagesize != kernel_pagesize) | |
84 | warnx(_("Using user-specified page size %d, " | |
00a7d0d2 | 85 | "instead of the system value %d"), |
de3822c3 | 86 | ctl->pagesize, kernel_pagesize); |
076ba5a6 SK |
87 | ctl->pagesize = ctl->user_pagesize; |
88 | } else | |
89 | ctl->pagesize = kernel_pagesize; | |
99f78758 | 90 | |
de3822c3 | 91 | ctl->signature_page = (unsigned long *) xcalloc(1, ctl->pagesize); |
99f78758 KZ |
92 | ctl->hdr = (struct swap_header_v1_2 *) ctl->signature_page; |
93 | } | |
94 | ||
95 | static void deinit_signature_page(struct mkswap_control *ctl) | |
96 | { | |
97 | free(ctl->signature_page); | |
98 | ||
99 | ctl->hdr = NULL; | |
100 | ctl->signature_page = NULL; | |
5c36a0eb KZ |
101 | } |
102 | ||
3ba01c14 | 103 | static void set_signature(const struct mkswap_control *ctl) |
f2704664 | 104 | { |
de3822c3 | 105 | char *sp = (char *) ctl->signature_page; |
5c36a0eb | 106 | |
fabf29f4 | 107 | assert(sp); |
de3822c3 | 108 | strncpy(sp + ctl->pagesize - SWAP_SIGNATURE_SZ, SWAP_SIGNATURE, SWAP_SIGNATURE_SZ); |
5c36a0eb KZ |
109 | } |
110 | ||
3ba01c14 | 111 | static void set_uuid_and_label(const struct mkswap_control *ctl) |
f2704664 | 112 | { |
99f78758 KZ |
113 | assert(ctl); |
114 | assert(ctl->hdr); | |
756bfd01 | 115 | |
99f78758 | 116 | /* set UUID */ |
de3822c3 | 117 | if (ctl->uuid) |
99f78758 KZ |
118 | memcpy(ctl->hdr->uuid, ctl->uuid, sizeof(ctl->hdr->uuid)); |
119 | ||
120 | /* set LABEL */ | |
de3822c3 | 121 | if (ctl->opt_label) { |
99f78758 KZ |
122 | xstrncpy(ctl->hdr->volume_name, |
123 | ctl->opt_label, sizeof(ctl->hdr->volume_name)); | |
124 | if (strlen(ctl->opt_label) > strlen(ctl->hdr->volume_name)) | |
00a7d0d2 | 125 | warnx(_("Label was truncated.")); |
756bfd01 | 126 | } |
99f78758 KZ |
127 | |
128 | /* report resuls */ | |
de3822c3 SK |
129 | if (ctl->uuid || ctl->opt_label) { |
130 | if (ctl->opt_label) | |
99f78758 | 131 | printf("LABEL=%s, ", ctl->hdr->volume_name); |
756bfd01 KZ |
132 | else |
133 | printf(_("no label, ")); | |
766dd757 | 134 | #ifdef HAVE_LIBUUID |
de3822c3 | 135 | if (ctl->uuid) { |
756bfd01 | 136 | char uuid_string[37]; |
de3822c3 | 137 | uuid_unparse(ctl->uuid, uuid_string); |
756bfd01 KZ |
138 | printf("UUID=%s\n", uuid_string); |
139 | } else | |
140 | #endif | |
141 | printf(_("no uuid\n")); | |
142 | } | |
143 | } | |
144 | ||
e079c4e6 SK |
145 | static void __attribute__ ((__noreturn__)) usage(FILE *out) |
146 | { | |
147 | fprintf(out, | |
148 | _("\nUsage:\n" | |
149 | " %s [options] device [size]\n"), | |
00a7d0d2 | 150 | program_invocation_short_name); |
e079c4e6 | 151 | |
451dbcfa BS |
152 | fputs(USAGE_SEPARATOR, out); |
153 | fputs(_("Set up a Linux swap area.\n"), out); | |
154 | ||
e079c4e6 SK |
155 | fprintf(out, _( |
156 | "\nOptions:\n" | |
157 | " -c, --check check bad blocks before creating the swap area\n" | |
158 | " -f, --force allow swap size area be larger than device\n" | |
159 | " -p, --pagesize SIZE specify page size in bytes\n" | |
160 | " -L, --label LABEL specify label\n" | |
161 | " -v, --swapversion NUM specify swap-space version number\n" | |
162 | " -U, --uuid UUID specify the uuid to use\n" | |
163 | " -V, --version output version information and exit\n" | |
164 | " -h, --help display this help and exit\n\n")); | |
165 | ||
166 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
eb63b9b8 | 167 | } |
6dbe3af9 | 168 | |
99f78758 | 169 | static void page_bad(struct mkswap_control *ctl, unsigned int page) |
f2704664 | 170 | { |
99f78758 KZ |
171 | const unsigned long max_badpages = |
172 | (ctl->pagesize - 1024 - 128 * sizeof(int) - 10) / sizeof(int); | |
3e16599a | 173 | |
8591859c | 174 | if (ctl->nbadpages == max_badpages) |
d68f402c | 175 | errx(EXIT_FAILURE, _("too many bad pages: %lu"), max_badpages); |
99f78758 KZ |
176 | |
177 | ctl->hdr->badpages[ctl->nbadpages] = page; | |
8591859c | 178 | ctl->nbadpages++; |
5c36a0eb KZ |
179 | } |
180 | ||
99f78758 | 181 | static void check_blocks(struct mkswap_control *ctl) |
f2704664 | 182 | { |
d68f402c | 183 | unsigned int current_page = 0; |
6dbe3af9 | 184 | int do_seek = 1; |
5c36a0eb | 185 | char *buffer; |
6dbe3af9 | 186 | |
99f78758 KZ |
187 | assert(ctl); |
188 | assert(ctl->fd > -1); | |
189 | ||
de3822c3 | 190 | buffer = xmalloc(ctl->pagesize); |
8591859c | 191 | while (current_page < ctl->npages) { |
f3b16286 KZ |
192 | ssize_t rc; |
193 | ||
de3822c3 SK |
194 | if (do_seek && lseek(ctl->fd, current_page * ctl->pagesize, SEEK_SET) != |
195 | current_page * ctl->pagesize) | |
00a7d0d2 | 196 | errx(EXIT_FAILURE, _("seek failed in check_blocks")); |
f3b16286 | 197 | |
de3822c3 SK |
198 | rc = read(ctl->fd, buffer, ctl->pagesize); |
199 | do_seek = (rc < 0 || rc != ctl->pagesize); | |
f3b16286 | 200 | if (do_seek) |
de3822c3 | 201 | page_bad(ctl, current_page); |
adda7f7e | 202 | current_page++; |
6dbe3af9 | 203 | } |
8591859c | 204 | printf(P_("%lu bad page\n", "%lu bad pages\n", ctl->nbadpages), ctl->nbadpages); |
3216beb0 | 205 | free(buffer); |
6dbe3af9 KZ |
206 | } |
207 | ||
99e6d525 | 208 | /* return size in pages */ |
fabf29f4 | 209 | static unsigned long long get_size(const struct mkswap_control *ctl) |
f2704664 SK |
210 | { |
211 | int fd; | |
54e377b3 | 212 | unsigned long long size; |
6dbe3af9 | 213 | |
fabf29f4 | 214 | fd = open(ctl->devname, O_RDONLY); |
3d5c8ba1 | 215 | if (fd < 0) |
fabf29f4 | 216 | err(EXIT_FAILURE, _("cannot open %s"), ctl->devname); |
54e377b3 | 217 | if (blkdev_get_size(fd, &size) == 0) |
de3822c3 | 218 | size /= ctl->pagesize; |
54e377b3 | 219 | |
6dbe3af9 KZ |
220 | close(fd); |
221 | return size; | |
222 | } | |
223 | ||
979f1dd5 | 224 | #ifdef HAVE_LIBBLKID |
fabf29f4 | 225 | static blkid_probe new_prober(const struct mkswap_control *ctl) |
979f1dd5 KZ |
226 | { |
227 | blkid_probe pr = blkid_new_probe(); | |
228 | if (!pr) | |
229 | errx(EXIT_FAILURE, _("unable to alloc new libblkid probe")); | |
6af18227 | 230 | if (blkid_probe_set_device(pr, ctl->fd, 0, 0)) |
979f1dd5 KZ |
231 | errx(EXIT_FAILURE, _("unable to assign device to libblkid probe")); |
232 | return pr; | |
233 | } | |
234 | #endif | |
235 | ||
fabf29f4 KZ |
236 | static void open_device(struct mkswap_control *ctl) |
237 | { | |
238 | assert(ctl); | |
239 | assert(ctl->devname); | |
240 | ||
241 | if (stat(ctl->devname, &ctl->devstat) < 0) | |
fc14ceba | 242 | err(EXIT_FAILURE, _("stat of %s failed"), ctl->devname); |
80c320fa | 243 | ctl->fd = open_blkdev_or_file(&ctl->devstat, ctl->devname, O_RDWR); |
fabf29f4 KZ |
244 | if (ctl->fd < 0) |
245 | err(EXIT_FAILURE, _("cannot open %s"), ctl->devname); | |
80c320fa SK |
246 | if (ctl->check && S_ISREG(ctl->devstat.st_mode)) { |
247 | ctl->check = 0; | |
248 | warnx(_("warning: checking bad blocks from swap file is not supported: %s"), | |
249 | ctl->devname); | |
250 | } | |
fabf29f4 KZ |
251 | } |
252 | ||
253 | static void wipe_device(struct mkswap_control *ctl) | |
ff3bed80 | 254 | { |
566f35bc | 255 | char *type = NULL; |
ff3bed80 | 256 | int zap = 1; |
9206b238 | 257 | #ifdef HAVE_LIBBLKID |
979f1dd5 | 258 | blkid_probe pr = NULL; |
9206b238 | 259 | #endif |
6af18227 SK |
260 | if (!ctl->force) { |
261 | if (lseek(ctl->fd, 0, SEEK_SET) != 0) | |
00a7d0d2 | 262 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); |
ff3bed80 | 263 | |
64d15476 | 264 | #ifdef HAVE_LIBBLKID |
6af18227 | 265 | pr = new_prober(ctl); |
c1f1b301 MB |
266 | blkid_probe_enable_partitions(pr, 1); |
267 | blkid_probe_enable_superblocks(pr, 0); | |
268 | ||
269 | if (blkid_do_fullprobe(pr) == 0 && | |
270 | blkid_probe_lookup_value(pr, "PTTYPE", | |
271 | (const char **) &type, NULL) == 0 && type) { | |
272 | type = xstrdup(type); | |
ff3bed80 | 273 | zap = 0; |
566f35bc | 274 | } |
c1f1b301 MB |
275 | #else |
276 | /* don't zap if compiled without libblkid */ | |
277 | zap = 0; | |
278 | #endif | |
ff3bed80 KZ |
279 | } |
280 | ||
281 | if (zap) { | |
979f1dd5 | 282 | /* |
d68f402c | 283 | * Wipe bootbits |
979f1dd5 | 284 | */ |
d68f402c | 285 | char buf[1024] = { '\0' }; |
ff3bed80 | 286 | |
6af18227 | 287 | if (lseek(ctl->fd, 0, SEEK_SET) != 0) |
00a7d0d2 | 288 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); |
ff3bed80 | 289 | |
6af18227 | 290 | if (write_all(ctl->fd, buf, sizeof(buf))) |
00a7d0d2 | 291 | errx(EXIT_FAILURE, _("unable to erase bootbits sectors")); |
979f1dd5 KZ |
292 | #ifdef HAVE_LIBBLKID |
293 | /* | |
294 | * Wipe rest of the device | |
295 | */ | |
296 | if (!pr) | |
6af18227 | 297 | pr = new_prober(ctl); |
979f1dd5 KZ |
298 | |
299 | blkid_probe_enable_superblocks(pr, 1); | |
300 | blkid_probe_enable_partitions(pr, 0); | |
c1f1b301 | 301 | blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC|BLKID_SUBLKS_TYPE); |
979f1dd5 | 302 | |
c1f1b301 | 303 | while (blkid_do_probe(pr) == 0) { |
a0b42dc3 KZ |
304 | const char *data = NULL; |
305 | ||
c1f1b301 | 306 | if (blkid_probe_lookup_value(pr, "TYPE", &data, NULL) == 0 && data) |
fabf29f4 | 307 | warnx(_("%s: warning: wiping old %s signature."), ctl->devname, data); |
979f1dd5 | 308 | blkid_do_wipe(pr, 0); |
c1f1b301 | 309 | } |
979f1dd5 KZ |
310 | #endif |
311 | } else { | |
312 | warnx(_("%s: warning: don't erase bootbits sectors"), | |
fabf29f4 | 313 | ctl->devname); |
979f1dd5 KZ |
314 | if (type) |
315 | fprintf(stderr, _(" (%s partition table detected). "), type); | |
979f1dd5 KZ |
316 | else |
317 | fprintf(stderr, _(" (compiled without libblkid). ")); | |
8c219bf4 | 318 | fprintf(stderr, _("Use -f to force.\n")); |
ff3bed80 | 319 | } |
acec6eec | 320 | free(type); |
979f1dd5 KZ |
321 | #ifdef HAVE_LIBBLKID |
322 | blkid_free_probe(pr); | |
323 | #endif | |
ff3bed80 KZ |
324 | } |
325 | ||
3ba01c14 KZ |
326 | #define SIGNATURE_OFFSET 1024 |
327 | ||
328 | static void write_header_to_device(struct mkswap_control *ctl) | |
329 | { | |
330 | assert(ctl); | |
331 | assert(ctl->fd > -1); | |
332 | assert(ctl->signature_page); | |
333 | ||
334 | if (lseek(ctl->fd, SIGNATURE_OFFSET, SEEK_SET) != SIGNATURE_OFFSET) | |
335 | errx(EXIT_FAILURE, _("unable to rewind swap-device")); | |
336 | ||
337 | if (write_all(ctl->fd, (char *) ctl->signature_page + SIGNATURE_OFFSET, | |
338 | ctl->pagesize - SIGNATURE_OFFSET) == -1) | |
339 | err(EXIT_FAILURE, | |
340 | _("%s: unable to write signature page"), | |
341 | ctl->devname); | |
342 | } | |
343 | ||
9a83b03c KZ |
344 | int main(int argc, char **argv) |
345 | { | |
99f78758 | 346 | struct mkswap_control ctl = { .fd = -1 }; |
e079c4e6 | 347 | int c; |
9a83b03c | 348 | uint64_t sz; |
8a101b14 | 349 | int version = SWAP_VERSION; |
9a83b03c | 350 | char *block_count = NULL, *strsz = NULL; |
766dd757 | 351 | #ifdef HAVE_LIBUUID |
7b241808 | 352 | const char *opt_uuid = NULL; |
756bfd01 KZ |
353 | uuid_t uuid_dat; |
354 | #endif | |
6c7d5ae9 | 355 | static const struct option longopts[] = { |
e079c4e6 SK |
356 | { "check", no_argument, 0, 'c' }, |
357 | { "force", no_argument, 0, 'f' }, | |
358 | { "pagesize", required_argument, 0, 'p' }, | |
359 | { "label", required_argument, 0, 'L' }, | |
360 | { "swapversion", required_argument, 0, 'v' }, | |
361 | { "uuid", required_argument, 0, 'U' }, | |
362 | { "version", no_argument, 0, 'V' }, | |
363 | { "help", no_argument, 0, 'h' }, | |
364 | { NULL, 0, 0, 0 } | |
365 | }; | |
eb63b9b8 | 366 | |
7eda085c KZ |
367 | setlocale(LC_ALL, ""); |
368 | bindtextdomain(PACKAGE, LOCALEDIR); | |
369 | textdomain(PACKAGE); | |
45ca68ec | 370 | atexit(close_stdout); |
5c36a0eb | 371 | |
e079c4e6 SK |
372 | while((c = getopt_long(argc, argv, "cfp:L:v:U:Vh", longopts, NULL)) != -1) { |
373 | switch (c) { | |
374 | case 'c': | |
de3822c3 | 375 | ctl.check = 1; |
e079c4e6 SK |
376 | break; |
377 | case 'f': | |
de3822c3 | 378 | ctl.force = 1; |
e079c4e6 SK |
379 | break; |
380 | case 'p': | |
de3822c3 | 381 | ctl.user_pagesize = strtou32_or_err(optarg, _("parsing page size failed")); |
e079c4e6 SK |
382 | break; |
383 | case 'L': | |
de3822c3 | 384 | ctl.opt_label = optarg; |
e079c4e6 SK |
385 | break; |
386 | case 'v': | |
8c219bf4 | 387 | version = strtos32_or_err(optarg, _("parsing version number failed")); |
de3822c3 SK |
388 | if (version != SWAP_VERSION) |
389 | errx(EXIT_FAILURE, | |
390 | _("swapspace version %d is not supported"), version); | |
e079c4e6 SK |
391 | break; |
392 | case 'U': | |
93bf0f28 | 393 | #ifdef HAVE_LIBUUID |
e079c4e6 | 394 | opt_uuid = optarg; |
93bf0f28 | 395 | #else |
4e096801 | 396 | warnx(_("warning: ignoring -U (UUIDs are unsupported by %s)"), |
e079c4e6 | 397 | program_invocation_short_name); |
93bf0f28 | 398 | #endif |
e079c4e6 SK |
399 | break; |
400 | case 'V': | |
33fb5cfd | 401 | printf(UTIL_LINUX_VERSION); |
e079c4e6 SK |
402 | exit(EXIT_SUCCESS); |
403 | case 'h': | |
404 | usage(stdout); | |
405 | default: | |
406 | usage(stderr); | |
407 | } | |
408 | } | |
3ba01c14 | 409 | |
e079c4e6 | 410 | if (optind < argc) |
fabf29f4 | 411 | ctl.devname = argv[optind++]; |
e079c4e6 SK |
412 | if (optind < argc) |
413 | block_count = argv[optind++]; | |
414 | if (optind != argc) { | |
8c219bf4 | 415 | warnx(_("only one device argument is currently supported")); |
e079c4e6 | 416 | usage(stderr); |
6dbe3af9 | 417 | } |
eb63b9b8 | 418 | |
766dd757 | 419 | #ifdef HAVE_LIBUUID |
93bf0f28 MS |
420 | if(opt_uuid) { |
421 | if (uuid_parse(opt_uuid, uuid_dat) != 0) | |
8c219bf4 | 422 | errx(EXIT_FAILURE, _("error: parsing UUID failed")); |
93bf0f28 MS |
423 | } else |
424 | uuid_generate(uuid_dat); | |
de3822c3 | 425 | ctl.uuid = uuid_dat; |
756bfd01 KZ |
426 | #endif |
427 | ||
de3822c3 | 428 | init_signature_page(&ctl); /* get pagesize and allocate signature page */ |
eb63b9b8 | 429 | |
fabf29f4 | 430 | if (!ctl.devname) { |
00a7d0d2 | 431 | warnx(_("error: Nowhere to set up swap on?")); |
e079c4e6 | 432 | usage(stderr); |
fd6b7a7f | 433 | } |
eb63b9b8 | 434 | if (block_count) { |
20543e61 | 435 | /* this silly user specified the number of blocks explicitly */ |
33fb5cfd KZ |
436 | uint64_t blks = strtou64_or_err(block_count, |
437 | _("invalid block count argument")); | |
8591859c | 438 | ctl.npages = blks / (ctl.pagesize / 1024); |
eb63b9b8 | 439 | } |
3ba01c14 | 440 | |
de3822c3 | 441 | sz = get_size(&ctl); |
8591859c KZ |
442 | if (!ctl.npages) |
443 | ctl.npages = sz; | |
444 | else if (ctl.npages > sz && !ctl.force) | |
00a7d0d2 SK |
445 | errx(EXIT_FAILURE, |
446 | _("error: " | |
9a83b03c | 447 | "size %llu KiB is larger than device size %ju KiB"), |
8591859c | 448 | ctl.npages * (ctl.pagesize / 1024), sz * (ctl.pagesize / 1024)); |
5c36a0eb | 449 | |
8591859c | 450 | if (ctl.npages < MIN_GOODPAGES) |
793a05f8 SK |
451 | errx(EXIT_FAILURE, |
452 | _("error: swap area needs to be at least %ld KiB"), | |
de3822c3 | 453 | (long)(MIN_GOODPAGES * ctl.pagesize / 1024)); |
8591859c | 454 | if (ctl.npages > UINT32_MAX) { |
a1466ab2 | 455 | /* true when swap is bigger than 17.59 terabytes */ |
8591859c | 456 | ctl.npages = UINT32_MAX; |
00a7d0d2 | 457 | warnx(_("warning: truncating swap area to %llu KiB"), |
8591859c | 458 | ctl.npages * ctl.pagesize / 1024); |
726f69e2 | 459 | } |
5c36a0eb | 460 | |
fabf29f4 | 461 | if (is_mounted(ctl.devname)) |
dceb1f22 | 462 | errx(EXIT_FAILURE, _("error: " |
4e096801 | 463 | "%s is mounted; will not make swapspace"), |
fabf29f4 KZ |
464 | ctl.devname); |
465 | ||
466 | open_device(&ctl); | |
dceb1f22 | 467 | |
de3822c3 SK |
468 | if (ctl.check) |
469 | check_blocks(&ctl); | |
4c85aa3a | 470 | |
6af18227 | 471 | wipe_device(&ctl); |
ff3bed80 | 472 | |
99f78758 | 473 | assert(ctl.hdr); |
de3822c3 | 474 | ctl.hdr->version = version; |
8591859c KZ |
475 | ctl.hdr->last_page = ctl.npages - 1; |
476 | ctl.hdr->nr_badpages = ctl.nbadpages; | |
5c36a0eb | 477 | |
8591859c | 478 | if ((ctl.npages - MIN_GOODPAGES) < ctl.nbadpages) |
00a7d0d2 | 479 | errx(EXIT_FAILURE, _("Unable to set up swap-space: unreadable")); |
4c85aa3a | 480 | |
9a83b03c KZ |
481 | sz = (ctl.npages - ctl.nbadpages - 1) * ctl.pagesize; |
482 | strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, sz); | |
483 | ||
484 | printf(_("Setting up swapspace version %d, size = %s (%ju bytes)\n"), | |
485 | version, strsz, sz); | |
acec6eec | 486 | free(strsz); |
5c36a0eb | 487 | |
3ba01c14 KZ |
488 | set_signature(&ctl); |
489 | set_uuid_and_label(&ctl); | |
756bfd01 | 490 | |
3ba01c14 | 491 | write_header_to_device(&ctl); |
99f78758 KZ |
492 | |
493 | deinit_signature_page(&ctl); | |
494 | ||
3e18b040 | 495 | #ifdef HAVE_LIBSELINUX |
fabf29f4 | 496 | if (S_ISREG(ctl.devstat.st_mode) && is_selinux_enabled() > 0) { |
3e18b040 KZ |
497 | security_context_t context_string; |
498 | security_context_t oldcontext; | |
499 | context_t newcontext; | |
500 | ||
de3822c3 | 501 | if (fgetfilecon(ctl.fd, &oldcontext) < 0) { |
00a7d0d2 SK |
502 | if (errno != ENODATA) |
503 | err(EXIT_FAILURE, | |
8d1b0fe2 | 504 | _("%s: unable to obtain selinux file label"), |
fabf29f4 KZ |
505 | ctl.devname); |
506 | if (matchpathcon(ctl.devname, ctl.devstat.st_mode, &oldcontext)) | |
00a7d0d2 | 507 | errx(EXIT_FAILURE, _("unable to matchpathcon()")); |
3e18b040 KZ |
508 | } |
509 | if (!(newcontext = context_new(oldcontext))) | |
00a7d0d2 | 510 | errx(EXIT_FAILURE, _("unable to create new selinux context")); |
3e18b040 | 511 | if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE)) |
00a7d0d2 | 512 | errx(EXIT_FAILURE, _("couldn't compute selinux context")); |
3e18b040 KZ |
513 | |
514 | context_string = context_str(newcontext); | |
515 | ||
516 | if (strcmp(context_string, oldcontext)!=0) { | |
de3822c3 | 517 | if (fsetfilecon(ctl.fd, context_string)) |
00a7d0d2 | 518 | err(EXIT_FAILURE, _("unable to relabel %s to %s"), |
fabf29f4 | 519 | ctl.devname, context_string); |
3e18b040 KZ |
520 | } |
521 | context_free(newcontext); | |
522 | freecon(oldcontext); | |
523 | } | |
524 | #endif | |
833b7e0d SK |
525 | /* |
526 | * A subsequent swapon() will fail if the signature | |
527 | * is not actually on disk. (This is a kernel bug.) | |
528 | * The fsync() in close_fd() will take care of writing. | |
529 | */ | |
de3822c3 | 530 | if (close_fd(ctl.fd) != 0) |
833b7e0d | 531 | err(EXIT_FAILURE, _("write failed")); |
a4d3e778 | 532 | return EXIT_SUCCESS; |
6dbe3af9 | 533 | } |