blob: b109f5ce14d192aa184f8c1c449812792196b559 [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 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +00007 */
8
Rob Landleyd893b122006-07-16 08:17: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
19 if (argc!=2) bb_show_usage();
20
21 // Figure out how big the device is and announce our intentions.
22
Rob Landleyd921b2e2006-08-03 15:41:12 +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();
Rob Landleyd893b122006-07-16 08:17:03 +000026 printf("Setting up swapspace version 1, size = %ld bytes\n", (long)(len-pagesize));
Eric Andersene77ae3a1999-10-19 20:03:34 +000027
Rob Landleyd893b122006-07-16 08:17:03 +000028 // Make a header.
Eric Andersene77ae3a1999-10-19 20:03:34 +000029
Rob Landleyd893b122006-07-16 08:17:03 +000030 memset(hdr, 0, 129 * sizeof(unsigned int));
31 hdr[0] = 1;
32 hdr[1] = (len / pagesize) - 1;
Eric Andersene77ae3a1999-10-19 20:03:34 +000033
Rob Landleyd893b122006-07-16 08:17:03 +000034 // Write the header. Sync to disk because some kernel versions check
35 // signature on disk (not in cache) during swapon.
Eric Andersene77ae3a1999-10-19 20:03:34 +000036
Rob Landleyd893b122006-07-16 08:17:03 +000037 xlseek(fd, 1024, SEEK_SET);
38 xwrite(fd, hdr, 129 * sizeof(unsigned int));
39 xlseek(fd, pagesize-10, SEEK_SET);
40 xwrite(fd, "SWAPSPACE2", 10);
41 fsync(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000042
Rob Landleyd893b122006-07-16 08:17:03 +000043 if (ENABLE_FEATURE_CLEAN_UP) close(fd);
Eric Andersene77ae3a1999-10-19 20:03:34 +000044
Rob Landleyd893b122006-07-16 08:17:03 +000045 return 0;
Eric Andersencc8ed391999-10-05 16:24:54 +000046}