2 * swaplabel.c - Print or change the label / UUID of a swap partition
4 * Copyright (C) 2010 Jason Borden <jborden@bluehost.com>
5 * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
7 * Usage: swaplabel [-L label] [-U UUID] device
9 * This file may be redistributed under the terms of the GNU Public License
17 #include <sys/types.h>
29 #include "closestream.h"
31 #include "swapheader.h"
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))
39 * Returns new libblkid prober. This function call exit() on error.
41 static blkid_probe
get_swap_prober(const char *devname
)
45 const char *version
= NULL
;
46 char *swap_filter
[] = { "swap", NULL
};
48 pr
= blkid_new_probe_from_filename(devname
);
50 warn(_("%s: unable to probe device"), devname
);
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
);
59 blkid_probe_filter_superblocks_type(pr
, BLKID_FLTR_ONLYIN
, swap_filter
);
61 rc
= blkid_do_safeprobe(pr
);
63 warn(_("%s: unable to probe device"), devname
);
65 warnx(_("%s: ambivalent probing result, use wipefs(8)"), devname
);
67 warnx(_("%s: not a valid swap partition"), devname
);
70 /* supported is SWAPSPACE2 only */
71 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
72 if (strcmp(version
, "2"))
73 warnx(_("%s: unsupported swap version '%s'"),
83 /* Print the swap partition information */
84 static int print_info(blkid_probe pr
)
88 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
89 printf("LABEL: %s\n", data
);
91 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
92 printf("UUID: %s\n", data
);
97 /* Change the swap partition info */
98 static int change_info(const char *devname
, const char *label
, const char *uuid
)
102 fd
= open(devname
, O_RDWR
);
104 warn(_("%s: failed to open"), devname
);
108 /* Write the uuid if it was provided */
112 if (uuid_parse(uuid
, newuuid
) == -1)
113 warnx(_("failed to parse UUID: %s"), uuid
);
115 if (lseek(fd
, SWAP_UUID_OFFSET
, SEEK_SET
) !=
117 warn(_("%s: failed to seek to swap UUID"), devname
);
120 } else if (write_all(fd
, newuuid
, sizeof(newuuid
))) {
121 warn(_("%s: failed to write UUID"), devname
);
127 /* Write the label if it was provided */
129 char newlabel
[SWAP_LABEL_LENGTH
];
131 if (lseek(fd
, SWAP_LABEL_OFFSET
, SEEK_SET
) != SWAP_LABEL_OFFSET
) {
132 warn(_("%s: failed to seek to swap label "), devname
);
135 memset(newlabel
, 0, sizeof(newlabel
));
136 xstrncpy(newlabel
, label
, sizeof(newlabel
));
138 if (strlen(label
) > strlen(newlabel
))
139 warnx(_("label is too long. Truncating it to '%s'"),
141 if (write_all(fd
, newlabel
, sizeof(newlabel
))) {
142 warn(_("%s: failed to write label"), devname
);
155 static void __attribute__((__noreturn__
)) usage(FILE *out
)
157 fputs(USAGE_HEADER
, out
);
158 fprintf(out
, _(" %s [options] <device>\n"),
159 program_invocation_short_name
);
160 fputs(USAGE_OPTIONS
, out
);
161 fputs(_(" -L, --label <label> specify a new label\n"
162 " -U, --uuid <uuid> specify a new uuid\n"), out
);
163 fputs(USAGE_SEPARATOR
, out
);
164 fputs(USAGE_HELP
, out
);
165 fputs(USAGE_VERSION
, out
);
166 fprintf(out
, USAGE_MAN_TAIL("swaplabel(8)"));
167 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
170 int main(int argc
, char *argv
[])
172 blkid_probe pr
= NULL
;
173 char *uuid
= NULL
, *label
= NULL
, *devname
;
176 static const struct option longopts
[] = {
177 { "help", 0, 0, 'h' },
178 { "version", 0, 0, 'V' },
179 { "label", 1, 0, 'L' },
180 { "uuid", 1, 0, 'U' },
184 setlocale(LC_ALL
, "");
185 bindtextdomain(PACKAGE
, LOCALEDIR
);
187 atexit(close_stdout
);
189 while ((c
= getopt_long(argc
, argv
, "hVL:U:", longopts
, NULL
)) != -1) {
195 printf(UTIL_LINUX_VERSION
);
204 warnx(_("ignore -U (UUIDs are unsupported)"));
216 devname
= argv
[optind
];
217 pr
= get_swap_prober(devname
);
220 rc
= change_info(devname
, label
, uuid
);
223 blkid_free_probe(pr
);
225 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;