]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/swaplabel.c
translation: unify file open error messages
[thirdparty/util-linux.git] / disk-utils / swaplabel.c
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>
21 #include <blkid.h>
22 #include <getopt.h>
23
24 #ifdef HAVE_LIBUUID
25 # include <uuid.h>
26 #endif
27
28 #include "c.h"
29 #include "closestream.h"
30 #include "all-io.h"
31 #include "swapheader.h"
32 #include "strutils.h"
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 */
71 if (blkid_probe_lookup_value(pr, "VERSION", &version, NULL) == 0
72 && version
73 && strcmp(version, "2"))
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 */
85 static int print_info(blkid_probe pr)
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) {
105 warn(_("cannot open %s"), devname);
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
148 close(fd);
149 return 0;
150 err:
151 if (fd >= 0)
152 close(fd);
153 return -1;
154 }
155
156 static void __attribute__((__noreturn__)) usage(FILE *out)
157 {
158 fputs(USAGE_HEADER, out);
159 fprintf(out, _(" %s [options] <device>\n"),
160 program_invocation_short_name);
161 fputs(USAGE_OPTIONS, out);
162 fputs(_(" -L, --label <label> specify a new label\n"
163 " -U, --uuid <uuid> specify a new uuid\n"), out);
164 fputs(USAGE_SEPARATOR, out);
165 fputs(USAGE_HELP, out);
166 fputs(USAGE_VERSION, out);
167 fprintf(out, USAGE_MAN_TAIL("swaplabel(8)"));
168 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
169 }
170
171 int main(int argc, char *argv[])
172 {
173 blkid_probe pr = NULL;
174 char *uuid = NULL, *label = NULL, *devname;
175 int c, rc = -1;
176
177 static const struct option longopts[] = {
178 { "help", 0, 0, 'h' },
179 { "version", 0, 0, 'V' },
180 { "label", 1, 0, 'L' },
181 { "uuid", 1, 0, 'U' },
182 { NULL, 0, 0, 0 }
183 };
184
185 setlocale(LC_ALL, "");
186 bindtextdomain(PACKAGE, LOCALEDIR);
187 textdomain(PACKAGE);
188 atexit(close_stdout);
189
190 while ((c = getopt_long(argc, argv, "hVL:U:", longopts, NULL)) != -1) {
191 switch (c) {
192 case 'h':
193 usage(stdout);
194 break;
195 case 'V':
196 printf(UTIL_LINUX_VERSION);
197 return EXIT_SUCCESS;
198 case 'L':
199 label = optarg;
200 break;
201 case 'U':
202 #ifdef HAVE_LIBUUID
203 uuid = optarg;
204 #else
205 warnx(_("ignore -U (UUIDs are unsupported)"));
206 #endif
207 break;
208 default:
209 usage(stderr);
210 break;
211 }
212 }
213
214 if (optind == argc)
215 usage(stderr);
216
217 devname = argv[optind];
218 pr = get_swap_prober(devname);
219 if (pr) {
220 if (uuid || label)
221 rc = change_info(devname, label, uuid);
222 else
223 rc = print_info(pr);
224 blkid_free_probe(pr);
225 }
226 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
227 }
228