Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 2 | /* mkswap.c - format swap device (Linux v1 only) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | * |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 4 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 5 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 9 | #include <busybox.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 10 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 11 | int mkswap_main(int argc, char *argv[]) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 12 | { |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 13 | 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. |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame^] | 22 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 23 | fd = xopen(argv[1],O_RDWR); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 24 | len = fdlength(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 25 | pagesize = getpagesize(); |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 26 | printf("Setting up swapspace version 1, size = %ld bytes\n", (long)(len-pagesize)); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 27 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 28 | // Make a header. |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 29 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 30 | memset(hdr, 0, 129 * sizeof(unsigned int)); |
| 31 | hdr[0] = 1; |
| 32 | hdr[1] = (len / pagesize) - 1; |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 33 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 34 | // Write the header. Sync to disk because some kernel versions check |
| 35 | // signature on disk (not in cache) during swapon. |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 36 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 37 | 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 Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 42 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 43 | if (ENABLE_FEATURE_CLEAN_UP) close(fd); |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 44 | |
Rob Landley | d893b12 | 2006-07-16 08:17:03 +0000 | [diff] [blame] | 45 | return 0; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | } |