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