mkswap.c (4035B)
1 /* see LICENSE file for copyright and license details */ 2 3 #include <fcntl.h> 4 #include <stdint.h> 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <unistd.h> 9 10 #include "diskutil.h" 11 #include "util.h" 12 13 #define SWAP_UUID_LENGTH 16 14 #define SWAP_LABEL_LENGTH 16 15 #define SWAP_MIN_PAGES 10 16 17 struct swap_hdr { 18 char bootbits[1024]; 19 unsigned int version; 20 unsigned int last_page; 21 unsigned int nr_badpages; 22 unsigned char uuid[SWAP_UUID_LENGTH]; 23 char volume_name[SWAP_LABEL_LENGTH]; 24 unsigned int padding[117]; 25 unsigned int badpages[1]; 26 }; 27 28 static void 29 usage(void) 30 { 31 eprintf("usage: %s [-L label] [-U uuid] device\n", argv0); 32 } 33 34 // ?man mkswap: set up a swap area 35 // ?man arguments: [-L label] [-U uuid] device 36 // ?man initialize a linux swap area on a device or file 37 int 38 main(int argc, char *argv[]) 39 { 40 int fd; 41 struct BlockDev dev; 42 unsigned int pages; 43 long pagesize; 44 char *buf; 45 struct swap_hdr *hdr; 46 uint64_t bytes = 0; 47 char *label = NULL; 48 char *uuid_str = NULL; 49 char uuid_out[37] = {0}; 50 51 ARGBEGIN 52 { 53 case 'L': 54 label = EARGF(usage()); 55 break; 56 case 'U': 57 uuid_str = EARGF(usage()); 58 break; 59 default: 60 usage(); 61 } 62 ARGEND; 63 64 if (argc < 1) 65 usage(); 66 67 pagesize = sysconf(_SC_PAGESIZE); 68 if (pagesize <= 0) { 69 pagesize = sysconf(_SC_PAGE_SIZE); 70 if (pagesize <= 0) 71 eprintf("can't determine pagesize\n"); 72 } 73 74 if (blockdev_open(&dev, argv[0], 1) < 0) 75 eprintf("open %s:", argv[0]); 76 fd = dev.fd; 77 bytes = dev.size; 78 if (bytes == 0) { 79 off_t end = lseek(fd, 0, SEEK_END); 80 if (end != (off_t)-1) 81 bytes = (uint64_t)end; 82 lseek(fd, 0, SEEK_SET); 83 } 84 85 if (bytes == 0) 86 eprintf("unable to determine size of %s\n", argv[0]); 87 88 pages = bytes / pagesize; 89 if (pages < SWAP_MIN_PAGES) 90 eprintf("swap space needs to be at least %ldKiB\n", SWAP_MIN_PAGES * pagesize / 1024); 91 92 buf = ecalloc(1, pagesize); 93 94 hdr = (struct swap_hdr *)buf; 95 hdr->version = 1; 96 hdr->last_page = pages - 1; 97 98 if (uuid_str) { 99 unsigned int u[16]; 100 if (sscanf( 101 uuid_str, 102 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", 103 &u[0], 104 &u[1], 105 &u[2], 106 &u[3], 107 &u[4], 108 &u[5], 109 &u[6], 110 &u[7], 111 &u[8], 112 &u[9], 113 &u[10], 114 &u[11], 115 &u[12], 116 &u[13], 117 &u[14], 118 &u[15] 119 ) 120 == 16) { 121 int i; 122 for (i = 0; i < 16; i++) 123 hdr->uuid[i] = (unsigned char)u[i]; 124 } 125 } else { 126 int rfd = open("/dev/urandom", O_RDONLY); 127 if (rfd >= 0) { 128 if (read(rfd, hdr->uuid, SWAP_UUID_LENGTH) == SWAP_UUID_LENGTH) { 129 hdr->uuid[6] = (hdr->uuid[6] & 0x0f) | 0x40; 130 hdr->uuid[8] = (hdr->uuid[8] & 0x3f) | 0x80; 131 } 132 close(rfd); 133 } 134 } 135 136 if (label) 137 estrlcpy(hdr->volume_name, label, sizeof(hdr->volume_name)); 138 139 memcpy(buf + pagesize - 10, "SWAPSPACE2", 10); 140 141 snprintf( 142 uuid_out, 143 sizeof(uuid_out), 144 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", 145 hdr->uuid[0], 146 hdr->uuid[1], 147 hdr->uuid[2], 148 hdr->uuid[3], 149 hdr->uuid[4], 150 hdr->uuid[5], 151 hdr->uuid[6], 152 hdr->uuid[7], 153 hdr->uuid[8], 154 hdr->uuid[9], 155 hdr->uuid[10], 156 hdr->uuid[11], 157 hdr->uuid[12], 158 hdr->uuid[13], 159 hdr->uuid[14], 160 hdr->uuid[15] 161 ); 162 163 printf( 164 "Setting up swapspace version 1, size = %luKiB\n", 165 (unsigned long)(pages - 1) * pagesize / 1024 166 ); 167 if (hdr->volume_name[0]) 168 printf("LABEL=%s, ", hdr->volume_name); 169 printf("UUID=%s\n", uuid_out); 170 171 if (lseek(fd, 0, SEEK_SET) != 0 || write(fd, buf, pagesize) != pagesize) 172 eprintf("unable to write signature page\n"); 173 174 fsync(fd); 175 close(fd); 176 free(buf); 177 178 return 0; 179 }