Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/util-linux/mkswap.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/util-linux/mkswap.c
r1765 r2725 4 4 * Copyright 2006 Rob Landley <rob@landley.net> 5 5 * 6 * Licensed under GPL version 2, see file LICENSE in this tarball for details.6 * Licensed under GPLv2, see file LICENSE in this source tree. 7 7 */ 8 9 8 #include "libbb.h" 10 9 11 int mkswap_main(int argc, char **argv); 12 int mkswap_main(int argc, char **argv)10 #if ENABLE_SELINUX 11 static void mkswap_selinux_setcontext(int fd, const char *path) 13 12 { 14 int fd, pagesize; 13 struct stat stbuf; 14 15 if (!is_selinux_enabled()) 16 return; 17 18 xfstat(fd, &stbuf, path); 19 if (S_ISREG(stbuf.st_mode)) { 20 security_context_t newcon; 21 security_context_t oldcon = NULL; 22 context_t context; 23 24 if (fgetfilecon(fd, &oldcon) < 0) { 25 if (errno != ENODATA) 26 goto error; 27 if (matchpathcon(path, stbuf.st_mode, &oldcon) < 0) 28 goto error; 29 } 30 context = context_new(oldcon); 31 if (!context || context_type_set(context, "swapfile_t")) 32 goto error; 33 newcon = context_str(context); 34 if (!newcon) 35 goto error; 36 /* fsetfilecon_raw is hidden */ 37 if (strcmp(oldcon, newcon) != 0 && fsetfilecon(fd, newcon) < 0) 38 goto error; 39 if (ENABLE_FEATURE_CLEAN_UP) { 40 context_free(context); 41 freecon(oldcon); 42 } 43 } 44 return; 45 error: 46 bb_perror_msg_and_die("SELinux relabeling failed"); 47 } 48 #else 49 # define mkswap_selinux_setcontext(fd, path) ((void)0) 50 #endif 51 52 /* from Linux 2.6.23 */ 53 /* 54 * Magic header for a swap area. ... Note that the first 55 * kilobyte is reserved for boot loader or disk label stuff. 56 */ 57 struct swap_header_v1 { 58 /* char bootbits[1024]; Space for disklabel etc. */ 59 uint32_t version; /* second kbyte, word 0 */ 60 uint32_t last_page; /* 1 */ 61 uint32_t nr_badpages; /* 2 */ 62 char sws_uuid[16]; /* 3,4,5,6 */ 63 char sws_volume[16]; /* 7,8,9,10 */ 64 uint32_t padding[117]; /* 11..127 */ 65 uint32_t badpages[1]; /* 128 */ 66 /* total 129 32-bit words in 2nd kilobyte */ 67 } FIX_ALIASING; 68 69 #define NWORDS 129 70 #define hdr ((struct swap_header_v1*)bb_common_bufsiz1) 71 72 struct BUG_sizes { 73 char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1]; 74 char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1]; 75 }; 76 77 /* Stored without terminating NUL */ 78 static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2"; 79 80 int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 81 int mkswap_main(int argc UNUSED_PARAM, char **argv) 82 { 83 int fd; 84 unsigned pagesize; 15 85 off_t len; 16 unsigned int hdr[129];86 const char *label = ""; 17 87 18 // No options supported. 88 opt_complementary = "-1"; /* at least one param */ 89 /* TODO: -p PAGESZ, -U UUID */ 90 getopt32(argv, "L:", &label); 91 argv += optind; 19 92 20 if (argc != 2) bb_show_usage();93 fd = xopen(argv[0], O_WRONLY); 21 94 22 // Figure out how big the device is and announce our intentions. 95 /* Figure out how big the device is */ 96 len = get_volume_size_in_bytes(fd, argv[1], 1024, /*extend:*/ 1); 97 pagesize = getpagesize(); 98 len -= pagesize; 23 99 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); 100 /* Announce our intentions */ 101 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len); 102 mkswap_selinux_setcontext(fd, argv[0]); 29 103 30 // Make a header. 104 /* hdr is zero-filled so far. Clear the first kbyte, or else 105 * mkswap-ing former FAT partition does NOT erase its signature. 106 * 107 * util-linux-ng 2.17.2 claims to erase it only if it does not see 108 * a partition table and is not run on whole disk. -f forces it. 109 */ 110 xwrite(fd, hdr, 1024); 31 111 32 memset(hdr, 0, sizeof(hdr));33 hdr [0]= 1;34 hdr [1] = (len / pagesize) - 1;112 /* Fill the header. */ 113 hdr->version = 1; 114 hdr->last_page = (uoff_t)len / pagesize; 35 115 36 // Write the header. Sync to disk because some kernel versions check 37 // signature on disk (not in cache) during swapon. 116 if (ENABLE_FEATURE_MKSWAP_UUID) { 117 char uuid_string[32]; 118 generate_uuid((void*)hdr->sws_uuid); 119 bin2hex(uuid_string, hdr->sws_uuid, 16); 120 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ 121 printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n", 122 uuid_string, 123 uuid_string+8, 124 uuid_string+8+4, 125 uuid_string+8+4+4, 126 uuid_string+8+4+4+4 127 ); 128 } 129 safe_strncpy(hdr->sws_volume, label, 16); 38 130 39 xlseek(fd, 1024, SEEK_SET); 40 xwrite(fd, hdr, sizeof(hdr)); 41 xlseek(fd, pagesize-10, SEEK_SET); 42 xwrite(fd, "SWAPSPACE2", 10); 131 /* Write the header. Sync to disk because some kernel versions check 132 * signature on disk (not in cache) during swapon. */ 133 xwrite(fd, hdr, NWORDS * 4); 134 xlseek(fd, pagesize - 10, SEEK_SET); 135 xwrite(fd, SWAPSPACE2, 10); 43 136 fsync(fd); 44 137 45 if (ENABLE_FEATURE_CLEAN_UP) close(fd); 138 if (ENABLE_FEATURE_CLEAN_UP) 139 close(fd); 46 140 47 141 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.