blob: af4cc10f74732453628c420673b7018558a4f6c5 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Rob Landleyd893b122006-07-16 08:17:03 +00002/* mkswap.c - format swap device (Linux v1 only)
Eric Andersencc8ed391999-10-05 16:24:54 +00003 *
Rob Landleyd893b122006-07-16 08:17:03 +00004 * Copyright 2006 Rob Landley <rob@landley.net>
5 *
Rob Landleye9a7a622006-09-22 02:52:41 +00006 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +00007 */
8
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000010
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000011int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Bernhard Reutner-Fischerfebe3c42007-04-04 20:52:03 +000012int mkswap_main(int argc, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000013{
Rob Landleyd893b122006-07-16 08:17:03 +000014 int fd, pagesize;
15 off_t len;
16 unsigned int hdr[129];
17
18 // No options supported.
19
Denis Vlasenkod398eca2006-11-24 15:38:03 +000020 if (argc != 2) bb_show_usage();
Rob Landleyd893b122006-07-16 08:17:03 +000021
22 // Figure out how big the device is and announce our intentions.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000023
Denis Vlasenkod398eca2006-11-24 15:38:03 +000024 fd = xopen(argv[1], O_RDWR);
Rob Landleyd893b122006-07-16 08:17:03 +000025 len = fdlength(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000026 pagesize = getpagesize();
Denis Vlasenkod398eca2006-11-24 15:38:03 +000027 printf("Setting up swapspace version 1, size = %"OFF_FMT"d bytes\n",
28 len - pagesize);
Eric Andersene77ae3a1999-10-19 20:03:34 +000029
Rob Landleyd893b122006-07-16 08:17:03 +000030 // Make a header.
Eric Andersene77ae3a1999-10-19 20:03:34 +000031
Denis Vlasenkod398eca2006-11-24 15:38:03 +000032 memset(hdr, 0, sizeof(hdr));
Rob Landleyd893b122006-07-16 08:17:03 +000033 hdr[0] = 1;
34 hdr[1] = (len / pagesize) - 1;
Eric Andersene77ae3a1999-10-19 20:03:34 +000035
Rob Landleyd893b122006-07-16 08:17:03 +000036 // Write the header. Sync to disk because some kernel versions check
37 // signature on disk (not in cache) during swapon.
Eric Andersene77ae3a1999-10-19 20:03:34 +000038
Rob Landleyd893b122006-07-16 08:17:03 +000039 xlseek(fd, 1024, SEEK_SET);
Denis Vlasenkod398eca2006-11-24 15:38:03 +000040 xwrite(fd, hdr, sizeof(hdr));
Rob Landleyd893b122006-07-16 08:17:03 +000041 xlseek(fd, pagesize-10, SEEK_SET);
42 xwrite(fd, "SWAPSPACE2", 10);
43 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000044
Rob Landleyd893b122006-07-16 08:17:03 +000045 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000046
Rob Landleyd893b122006-07-16 08:17:03 +000047 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000048}