aboutsummaryrefslogtreecommitdiff
path: root/util-linux
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-11-03 05:51:20 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2009-11-03 05:51:20 +0100
commit68c67469aabe112cc0ef90bd87700370d96a4085 (patch)
treec5cd96f07291db18c736da2cd2ae5698948b3061 /util-linux
parentc6fb2a6b5fcece0ee4a7caf42cc344b5e0177822 (diff)
downloadbusybox-w32-68c67469aabe112cc0ef90bd87700370d96a4085.tar.gz
busybox-w32-68c67469aabe112cc0ef90bd87700370d96a4085.tar.bz2
busybox-w32-68c67469aabe112cc0ef90bd87700370d96a4085.zip
mkswap: add -L LABEL option. closes bug 689.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'util-linux')
-rw-r--r--util-linux/mkswap.c92
1 files changed, 46 insertions, 46 deletions
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c
index 2f7827d6f..289692da3 100644
--- a/util-linux/mkswap.c
+++ b/util-linux/mkswap.c
@@ -50,72 +50,70 @@ static void mkswap_selinux_setcontext(int fd, const char *path)
50# define mkswap_selinux_setcontext(fd, path) ((void)0) 50# define mkswap_selinux_setcontext(fd, path) ((void)0)
51#endif 51#endif
52 52
53#if 0 /* from Linux 2.6.23 */ 53/* from Linux 2.6.23 */
54/* 54/*
55 * Magic header for a swap area. The first part of the union is 55 * Magic header for a swap area. ... Note that the first
56 * what the swap magic looks like for the old (limited to 128MB) 56 * kilobyte is reserved for boot loader or disk label stuff.
57 * swap area format, the second part of the union adds - in the
58 * old reserved area - some extra information. Note that the first
59 * kilobyte is reserved for boot loader or disk label stuff...
60 */ 57 */
61union swap_header { 58struct swap_header_v1 {
62 struct { 59/* char bootbits[1024]; Space for disklabel etc. */
63 char reserved[PAGE_SIZE - 10]; 60 uint32_t version; /* second kbyte, word 0 */
64 char magic[10]; /* SWAP-SPACE or SWAPSPACE2 */ 61 uint32_t last_page; /* 1 */
65 } magic; 62 uint32_t nr_badpages; /* 2 */
66 struct { 63 char sws_uuid[16]; /* 3,4,5,6 */
67 char bootbits[1024]; /* Space for disklabel etc. */ 64 char sws_volume[16]; /* 7,8,9,10 */
68 __u32 version; /* second kbyte, word 0 */ 65 uint32_t padding[117]; /* 11..127 */
69 __u32 last_page; /* 1 */ 66 uint32_t badpages[1]; /* 128 */
70 __u32 nr_badpages; /* 2 */ 67 /* total 129 32-bit words in 2nd kilobyte */
71 unsigned char sws_uuid[16]; /* 3,4,5,6 */
72 unsigned char sws_volume[16]; /* 7,8,9,10 */
73 __u32 padding[117]; /* 11..127 */
74 __u32 badpages[1]; /* 128, total 129 32-bit words */
75 } info;
76}; 68};
77#endif
78 69
79#define NWORDS 129 70#define NWORDS 129
80#define hdr ((uint32_t*)(&bb_common_bufsiz1)) 71#define hdr ((struct swap_header_v1*)bb_common_bufsiz1)
81 72
82struct BUG_bufsiz1_is_too_small { 73struct BUG_sizes {
83 char BUG_bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1]; 74 char swap_header_v1_wrong[sizeof(*hdr) != (NWORDS * 4) ? -1 : 1];
75 char bufsiz1_is_too_small[COMMON_BUFSIZE < (NWORDS * 4) ? -1 : 1];
84}; 76};
85 77
86/* Stored without terminating NUL */ 78/* Stored without terminating NUL */
87static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2"; 79static const char SWAPSPACE2[sizeof("SWAPSPACE2")-1] ALIGN1 = "SWAPSPACE2";
88 80
89int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 81int mkswap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
90int mkswap_main(int argc, char **argv) 82int mkswap_main(int argc UNUSED_PARAM, char **argv)
91{ 83{
92 int fd, pagesize; 84 int fd;
85 unsigned pagesize;
93 off_t len; 86 off_t len;
87 const char *label = "";
94 88
95 // No options supported. 89 opt_complementary = "=1";
96 90 /* TODO: -p PAGESZ, -U UUID,
97 if (argc != 2) bb_show_usage(); 91 * optional SIZE_IN_KB 2nd param
92 */
93 getopt32(argv, "L:", &label);
94 argv += optind;
98 95
99 // Figure out how big the device is and announce our intentions. 96 fd = xopen(argv[0], O_WRONLY);
100 97
101 fd = xopen(argv[1], O_RDWR); 98 /* Figure out how big the device is and announce our intentions */
102 /* fdlength was reported to be unreliable - use seek */ 99 /* fdlength was reported to be unreliable - use seek */
103 len = xlseek(fd, 0, SEEK_END); 100 len = xlseek(fd, 0, SEEK_END);
104#if ENABLE_SELINUX 101 if (ENABLE_SELINUX)
105 xlseek(fd, 0, SEEK_SET); 102 xlseek(fd, 0, SEEK_SET);
106#endif 103
107 pagesize = getpagesize(); 104 pagesize = getpagesize();
108 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", 105 len -= pagesize;
109 len - pagesize); 106 printf("Setting up swapspace version 1, size = %"OFF_FMT"u bytes\n", len);
110 mkswap_selinux_setcontext(fd, argv[1]); 107 mkswap_selinux_setcontext(fd, argv[0]);
108
109 /* Make a header. hdr is zero-filled so far... */
110 hdr->version = 1;
111 hdr->last_page = (uoff_t)len / pagesize;
111 112
112 // Make a header. hdr is zero-filled so far...
113 hdr[0] = 1;
114 hdr[1] = (len / pagesize) - 1;
115 if (ENABLE_FEATURE_MKSWAP_UUID) { 113 if (ENABLE_FEATURE_MKSWAP_UUID) {
116 char uuid_string[32]; 114 char uuid_string[32];
117 generate_uuid((void*) &hdr[3]); 115 generate_uuid((void*)hdr->sws_uuid);
118 bin2hex(uuid_string, (void*) &hdr[3], 16); 116 bin2hex(uuid_string, hdr->sws_uuid, 16);
119 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */ 117 /* f.e. UUID=dfd9c173-be52-4d27-99a5-c34c6c2ff55f */
120 printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n", 118 printf("UUID=%.8s" "-%.4s-%.4s-%.4s-%.12s\n",
121 uuid_string, 119 uuid_string,
@@ -125,16 +123,18 @@ int mkswap_main(int argc, char **argv)
125 uuid_string+8+4+4+4 123 uuid_string+8+4+4+4
126 ); 124 );
127 } 125 }
126 safe_strncpy(hdr->sws_volume, label, 16);
128 127
129 // Write the header. Sync to disk because some kernel versions check 128 /* Write the header. Sync to disk because some kernel versions check
130 // signature on disk (not in cache) during swapon. 129 * signature on disk (not in cache) during swapon. */
131 xlseek(fd, 1024, SEEK_SET); 130 xlseek(fd, 1024, SEEK_SET);
132 xwrite(fd, hdr, NWORDS * 4); 131 xwrite(fd, hdr, NWORDS * 4);
133 xlseek(fd, pagesize - 10, SEEK_SET); 132 xlseek(fd, pagesize - 10, SEEK_SET);
134 xwrite(fd, SWAPSPACE2, 10); 133 xwrite(fd, SWAPSPACE2, 10);
135 fsync(fd); 134 fsync(fd);
136 135
137 if (ENABLE_FEATURE_CLEAN_UP) close(fd); 136 if (ENABLE_FEATURE_CLEAN_UP)
137 close(fd);
138 138
139 return 0; 139 return 0;
140} 140}