]>
Commit | Line | Data |
---|---|---|
4dddc2d4 JB |
1 | /* |
2 | * swaplabel.c - Print or change the label / UUID of a swap partition | |
3 | * | |
4 | * Copyright (C) 2010 Jason Borden <jborden@bluehost.com> | |
5 | * Copyright (C) 2010 Karel Zak <kzak@redhat.com> | |
6 | * | |
7 | * Usage: swaplabel [-L label] [-U UUID] device | |
8 | * | |
9 | * This file may be redistributed under the terms of the GNU Public License | |
10 | * version 2 or later. | |
11 | * | |
12 | */ | |
13 | #include <stdio.h> | |
14 | #include <stddef.h> | |
15 | #include <string.h> | |
16 | #include <fcntl.h> | |
17 | #include <sys/types.h> | |
18 | #include <sys/stat.h> | |
19 | #include <unistd.h> | |
20 | #include <stdlib.h> | |
4dddc2d4 JB |
21 | #include <blkid.h> |
22 | #include <getopt.h> | |
23 | ||
24 | #ifdef HAVE_LIBUUID | |
7ee96990 | 25 | # include <uuid.h> |
4dddc2d4 JB |
26 | #endif |
27 | ||
28 | #include "c.h" | |
45ca68ec | 29 | #include "closestream.h" |
e12c9866 | 30 | #include "all-io.h" |
4dddc2d4 | 31 | #include "swapheader.h" |
8abcf290 | 32 | #include "strutils.h" |
4dddc2d4 JB |
33 | #include "nls.h" |
34 | ||
35 | #define SWAP_UUID_OFFSET (offsetof(struct swap_header_v1_2, uuid)) | |
36 | #define SWAP_LABEL_OFFSET (offsetof(struct swap_header_v1_2, volume_name)) | |
37 | ||
38 | /* | |
39 | * Returns new libblkid prober. This function call exit() on error. | |
40 | */ | |
41 | static blkid_probe get_swap_prober(const char *devname) | |
42 | { | |
43 | blkid_probe pr; | |
44 | int rc; | |
45 | const char *version = NULL; | |
46 | char *swap_filter[] = { "swap", NULL }; | |
47 | ||
48 | pr = blkid_new_probe_from_filename(devname); | |
49 | if (!pr) { | |
50 | warn(_("%s: unable to probe device"), devname); | |
51 | return NULL; | |
52 | } | |
53 | ||
54 | blkid_probe_enable_superblocks(pr, TRUE); | |
55 | blkid_probe_set_superblocks_flags(pr, | |
56 | BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID | | |
57 | BLKID_SUBLKS_VERSION); | |
58 | ||
59 | blkid_probe_filter_superblocks_type(pr, BLKID_FLTR_ONLYIN, swap_filter); | |
60 | ||
61 | rc = blkid_do_safeprobe(pr); | |
62 | if (rc == -1) | |
63 | warn(_("%s: unable to probe device"), devname); | |
64 | else if (rc == -2) | |
65 | warnx(_("%s: ambivalent probing result, use wipefs(8)"), devname); | |
66 | else if (rc == 1) | |
67 | warnx(_("%s: not a valid swap partition"), devname); | |
68 | ||
69 | if (rc == 0) { | |
70 | /* supported is SWAPSPACE2 only */ | |
6b21a6b9 KZ |
71 | if (blkid_probe_lookup_value(pr, "VERSION", &version, NULL) == 0 |
72 | && version | |
fce72f96 | 73 | && strcmp(version, "1")) |
4dddc2d4 JB |
74 | warnx(_("%s: unsupported swap version '%s'"), |
75 | devname, version); | |
76 | else | |
77 | return pr; | |
78 | } | |
79 | ||
80 | blkid_free_probe(pr); | |
81 | return NULL; | |
82 | } | |
83 | ||
84 | /* Print the swap partition information */ | |
ae7ec06b | 85 | static int print_info(blkid_probe pr) |
4dddc2d4 JB |
86 | { |
87 | const char *data; | |
88 | ||
89 | if (!blkid_probe_lookup_value(pr, "LABEL", &data, NULL)) | |
90 | printf("LABEL: %s\n", data); | |
91 | ||
92 | if (!blkid_probe_lookup_value(pr, "UUID", &data, NULL)) | |
93 | printf("UUID: %s\n", data); | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
98 | /* Change the swap partition info */ | |
99 | static int change_info(const char *devname, const char *label, const char *uuid) | |
100 | { | |
101 | int fd; | |
102 | ||
103 | fd = open(devname, O_RDWR); | |
104 | if (fd < 0) { | |
289dcc90 | 105 | warn(_("cannot open %s"), devname); |
4dddc2d4 JB |
106 | goto err; |
107 | } | |
108 | #ifdef HAVE_LIBUUID | |
109 | /* Write the uuid if it was provided */ | |
110 | if (uuid) { | |
111 | uuid_t newuuid; | |
112 | ||
113 | if (uuid_parse(uuid, newuuid) == -1) | |
114 | warnx(_("failed to parse UUID: %s"), uuid); | |
115 | else { | |
116 | if (lseek(fd, SWAP_UUID_OFFSET, SEEK_SET) != | |
117 | SWAP_UUID_OFFSET) { | |
118 | warn(_("%s: failed to seek to swap UUID"), devname); | |
119 | goto err; | |
120 | ||
121 | } else if (write_all(fd, newuuid, sizeof(newuuid))) { | |
122 | warn(_("%s: failed to write UUID"), devname); | |
123 | goto err; | |
124 | } | |
125 | } | |
126 | } | |
127 | #endif | |
128 | /* Write the label if it was provided */ | |
129 | if (label) { | |
130 | char newlabel[SWAP_LABEL_LENGTH]; | |
131 | ||
132 | if (lseek(fd, SWAP_LABEL_OFFSET, SEEK_SET) != SWAP_LABEL_OFFSET) { | |
133 | warn(_("%s: failed to seek to swap label "), devname); | |
134 | goto err; | |
135 | } | |
136 | memset(newlabel, 0, sizeof(newlabel)); | |
137 | xstrncpy(newlabel, label, sizeof(newlabel)); | |
138 | ||
139 | if (strlen(label) > strlen(newlabel)) | |
140 | warnx(_("label is too long. Truncating it to '%s'"), | |
141 | newlabel); | |
142 | if (write_all(fd, newlabel, sizeof(newlabel))) { | |
143 | warn(_("%s: failed to write label"), devname); | |
144 | goto err; | |
145 | } | |
146 | } | |
147 | ||
de2ca559 SK |
148 | if (close_fd(fd) != 0) { |
149 | warn(_("write failed: %s"), devname); | |
150 | return -1; | |
151 | } | |
4dddc2d4 JB |
152 | return 0; |
153 | err: | |
154 | if (fd >= 0) | |
155 | close(fd); | |
156 | return -1; | |
157 | } | |
158 | ||
159 | static void __attribute__((__noreturn__)) usage(FILE *out) | |
160 | { | |
1275d8a1 SK |
161 | fputs(USAGE_HEADER, out); |
162 | fprintf(out, _(" %s [options] <device>\n"), | |
163 | program_invocation_short_name); | |
164 | fputs(USAGE_OPTIONS, out); | |
165 | fputs(_(" -L, --label <label> specify a new label\n" | |
166 | " -U, --uuid <uuid> specify a new uuid\n"), out); | |
167 | fputs(USAGE_SEPARATOR, out); | |
168 | fputs(USAGE_HELP, out); | |
169 | fputs(USAGE_VERSION, out); | |
170 | fprintf(out, USAGE_MAN_TAIL("swaplabel(8)")); | |
4dddc2d4 JB |
171 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); |
172 | } | |
173 | ||
174 | int main(int argc, char *argv[]) | |
175 | { | |
176 | blkid_probe pr = NULL; | |
177 | char *uuid = NULL, *label = NULL, *devname; | |
178 | int c, rc = -1; | |
179 | ||
6c7d5ae9 | 180 | static const struct option longopts[] = { |
4dddc2d4 | 181 | { "help", 0, 0, 'h' }, |
1275d8a1 | 182 | { "version", 0, 0, 'V' }, |
4dddc2d4 JB |
183 | { "label", 1, 0, 'L' }, |
184 | { "uuid", 1, 0, 'U' }, | |
185 | { NULL, 0, 0, 0 } | |
186 | }; | |
187 | ||
188 | setlocale(LC_ALL, ""); | |
189 | bindtextdomain(PACKAGE, LOCALEDIR); | |
190 | textdomain(PACKAGE); | |
45ca68ec | 191 | atexit(close_stdout); |
4dddc2d4 | 192 | |
1275d8a1 | 193 | while ((c = getopt_long(argc, argv, "hVL:U:", longopts, NULL)) != -1) { |
4dddc2d4 JB |
194 | switch (c) { |
195 | case 'h': | |
196 | usage(stdout); | |
197 | break; | |
1275d8a1 SK |
198 | case 'V': |
199 | printf(UTIL_LINUX_VERSION); | |
200 | return EXIT_SUCCESS; | |
4dddc2d4 JB |
201 | case 'L': |
202 | label = optarg; | |
203 | break; | |
204 | case 'U': | |
205 | #ifdef HAVE_LIBUUID | |
206 | uuid = optarg; | |
207 | #else | |
208 | warnx(_("ignore -U (UUIDs are unsupported)")); | |
209 | #endif | |
210 | break; | |
211 | default: | |
212 | usage(stderr); | |
213 | break; | |
214 | } | |
215 | } | |
216 | ||
217 | if (optind == argc) | |
218 | usage(stderr); | |
219 | ||
220 | devname = argv[optind]; | |
221 | pr = get_swap_prober(devname); | |
222 | if (pr) { | |
223 | if (uuid || label) | |
224 | rc = change_info(devname, label, uuid); | |
225 | else | |
ae7ec06b | 226 | rc = print_info(pr); |
4dddc2d4 JB |
227 | blkid_free_probe(pr); |
228 | } | |
229 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; | |
230 | } | |
231 |