1/* vi: set sw=4 ts=4: */
2/* mkswap.c - format swap device (Linux v1 only)
3 *
4 * Copyright 2006 Rob Landley <rob@landley.net>
5 *
6 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
7 */
8
9#include "libbb.h"
10
11int mkswap_main(int argc, char **argv);
12int mkswap_main(int argc, char **argv)
13{
14	int fd, pagesize;
15	off_t len;
16	unsigned int hdr[129];
17
18	// No options supported.
19
20	if (argc != 2) bb_show_usage();
21
22	// Figure out how big the device is and announce our intentions.
23
24	fd = xopen(argv[1], O_RDWR);
25	len = fdlength(fd);
26	pagesize = getpagesize();
27	printf("Setting up swapspace version 1, size = %"OFF_FMT"d bytes\n",
28			len - pagesize);
29
30	// Make a header.
31
32	memset(hdr, 0, sizeof(hdr));
33	hdr[0] = 1;
34	hdr[1] = (len / pagesize) - 1;
35
36	// Write the header.  Sync to disk because some kernel versions check
37	// signature on disk (not in cache) during swapon.
38
39	xlseek(fd, 1024, SEEK_SET);
40	xwrite(fd, hdr, sizeof(hdr));
41	xlseek(fd, pagesize-10, SEEK_SET);
42	xwrite(fd, "SWAPSPACE2", 10);
43	fsync(fd);
44
45	if (ENABLE_FEATURE_CLEAN_UP) close(fd);
46
47	return 0;
48}
49