blob: 7baa3ecfb4ecef55b9ddd87eaa2f7d6ceffa5a5b [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 Vlasenkod398eca2006-11-24 15:38:03 +00009#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000010
Rob Landleyd893b122006-07-16 08:17:03 +000011int mkswap_main(int argc, char *argv[])
Erik Andersene49d5ec2000-02-08 19:58:47 +000012{
Rob Landleyd893b122006-07-16 08:17:03 +000013 int fd, pagesize;
14 off_t len;
15 unsigned int hdr[129];
16
17 // No options supported.
18
Denis Vlasenkod398eca2006-11-24 15:38:03 +000019 if (argc != 2) bb_show_usage();
Rob Landleyd893b122006-07-16 08:17:03 +000020
21 // Figure out how big the device is and announce our intentions.
Denis Vlasenko9213a9e2006-09-17 16:28:10 +000022
Denis Vlasenkod398eca2006-11-24 15:38:03 +000023 fd = xopen(argv[1], O_RDWR);
Rob Landleyd893b122006-07-16 08:17:03 +000024 len = fdlength(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000025 pagesize = getpagesize();
Denis Vlasenkod398eca2006-11-24 15:38:03 +000026 printf("Setting up swapspace version 1, size = %"OFF_FMT"d bytes\n",
27 len - pagesize);
Eric Andersene77ae3a1999-10-19 20:03:34 +000028
Rob Landleyd893b122006-07-16 08:17:03 +000029 // Make a header.
Eric Andersene77ae3a1999-10-19 20:03:34 +000030
Denis Vlasenkod398eca2006-11-24 15:38:03 +000031 memset(hdr, 0, sizeof(hdr));
Rob Landleyd893b122006-07-16 08:17:03 +000032 hdr[0] = 1;
33 hdr[1] = (len / pagesize) - 1;
Eric Andersene77ae3a1999-10-19 20:03:34 +000034
Rob Landleyd893b122006-07-16 08:17:03 +000035 // Write the header. Sync to disk because some kernel versions check
36 // signature on disk (not in cache) during swapon.
Eric Andersene77ae3a1999-10-19 20:03:34 +000037
Rob Landleyd893b122006-07-16 08:17:03 +000038 xlseek(fd, 1024, SEEK_SET);
Denis Vlasenkod398eca2006-11-24 15:38:03 +000039 xwrite(fd, hdr, sizeof(hdr));
Rob Landleyd893b122006-07-16 08:17:03 +000040 xlseek(fd, pagesize-10, SEEK_SET);
41 xwrite(fd, "SWAPSPACE2", 10);
42 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000043
Rob Landleyd893b122006-07-16 08:17:03 +000044 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000045
Rob Landleyd893b122006-07-16 08:17:03 +000046 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000047}