Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 1 | #include <fcntl.h> |
| 2 | #include <linux/fs.h> |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 3 | #include <stdio.h> |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 4 | #include <stdlib.h> |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 5 | #include <unistd.h> |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 6 | #include <sys/stat.h> |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 7 | #include <sys/swap.h> |
| 8 | #include <sys/types.h> |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 9 | |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 10 | /* This is not in a uapi header. */ |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 11 | struct linux_swap_header { |
| 12 | char bootbits[1024]; /* Space for disklabel etc. */ |
| 13 | uint32_t version; |
| 14 | uint32_t last_page; |
| 15 | uint32_t nr_badpages; |
| 16 | unsigned char sws_uuid[16]; |
| 17 | unsigned char sws_volume[16]; |
| 18 | uint32_t padding[117]; |
| 19 | uint32_t badpages[1]; |
| 20 | }; |
| 21 | |
| 22 | #define MAGIC_SWAP_HEADER "SWAPSPACE2" |
| 23 | #define MAGIC_SWAP_HEADER_LEN 10 |
| 24 | #define MIN_PAGES 10 |
| 25 | |
| 26 | int mkswap_main(int argc, char **argv) |
| 27 | { |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 28 | if (argc != 2) { |
| 29 | fprintf(stderr, "Usage: %s <filename>\n", argv[0]); |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 30 | return EXIT_FAILURE; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 33 | int fd = open(argv[1], O_RDWR); |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 34 | if (fd < 0) { |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 35 | fprintf(stderr, "Cannot open %s: %s\n", argv[1], strerror(errno)); |
| 36 | return EXIT_FAILURE; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 39 | /* Determine the length of the swap file */ |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 40 | off64_t swap_size; |
| 41 | struct stat sb; |
| 42 | if (fstat(fd, &sb)) { |
| 43 | fprintf(stderr, "Couldn't fstat file: %s\n", strerror(errno)); |
| 44 | return EXIT_FAILURE; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 45 | } |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 46 | if (S_ISBLK(sb.st_mode)) { |
| 47 | if (ioctl(fd, BLKGETSIZE64, &swap_size) < 0) { |
| 48 | fprintf(stderr, "Couldn't determine block device size: %s\n", strerror(errno)); |
| 49 | return EXIT_FAILURE; |
| 50 | } |
| 51 | } else { |
| 52 | swap_size = sb.st_size; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 55 | int pagesize = getpagesize(); |
| 56 | if (swap_size < MIN_PAGES * pagesize) { |
| 57 | fprintf(stderr, "Swap file needs to be at least %d KiB\n", (MIN_PAGES * pagesize) >> 10); |
| 58 | return EXIT_FAILURE; |
| 59 | } |
| 60 | |
| 61 | struct linux_swap_header sw_hdr; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 62 | memset(&sw_hdr, 0, sizeof(sw_hdr)); |
| 63 | sw_hdr.version = 1; |
| 64 | sw_hdr.last_page = (swap_size / pagesize) - 1; |
| 65 | |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 66 | ssize_t len = write(fd, &sw_hdr, sizeof(sw_hdr)); |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 67 | if (len != sizeof(sw_hdr)) { |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 68 | fprintf(stderr, "Failed to write swap header into %s: %s\n", argv[1], strerror(errno)); |
| 69 | return EXIT_FAILURE; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* Write the magic header */ |
| 73 | if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) { |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 74 | fprintf(stderr, "Failed to seek into %s: %s\n", argv[1], strerror(errno)); |
| 75 | return EXIT_FAILURE; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN); |
| 79 | if (len != MAGIC_SWAP_HEADER_LEN) { |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 80 | fprintf(stderr, "Failed to write magic swap header into %s: %s\n", argv[1], strerror(errno)); |
| 81 | return EXIT_FAILURE; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (fsync(fd) < 0) { |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 85 | fprintf(stderr, "Failed to sync %s: %s\n", argv[1], strerror(errno)); |
| 86 | return EXIT_FAILURE; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 87 | } |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 88 | |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 89 | close(fd); |
Andrew Boie | d90615c | 2014-01-23 17:16:54 -0800 | [diff] [blame] | 90 | return EXIT_SUCCESS; |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 91 | } |