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>
25 # ifdef HAVE_UUID_UUID_H
26 # include <uuid/uuid.h>
34 #include "swapheader.h"
38 #define SWAP_UUID_OFFSET (offsetof(struct swap_header_v1_2, uuid))
39 #define SWAP_LABEL_OFFSET (offsetof(struct swap_header_v1_2, volume_name))
42 * Returns new libblkid prober. This function call exit() on error.
44 static blkid_probe
get_swap_prober(const char *devname
)
48 const char *version
= NULL
;
49 char *swap_filter
[] = { "swap", NULL
};
51 pr
= blkid_new_probe_from_filename(devname
);
53 warn(_("%s: unable to probe device"), devname
);
57 blkid_probe_enable_superblocks(pr
, TRUE
);
58 blkid_probe_set_superblocks_flags(pr
,
59 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
60 BLKID_SUBLKS_VERSION
);
62 blkid_probe_filter_superblocks_type(pr
, BLKID_FLTR_ONLYIN
, swap_filter
);
64 rc
= blkid_do_safeprobe(pr
);
66 warn(_("%s: unable to probe device"), devname
);
68 warnx(_("%s: ambivalent probing result, use wipefs(8)"), devname
);
70 warnx(_("%s: not a valid swap partition"), devname
);
73 /* supported is SWAPSPACE2 only */
74 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
75 if (strcmp(version
, "2"))
76 warnx(_("%s: unsupported swap version '%s'"),
86 /* Print the swap partition information */
87 static int print_info(blkid_probe pr
, const char *devname
)
91 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
92 printf("LABEL: %s\n", data
);
94 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
95 printf("UUID: %s\n", data
);
100 /* Change the swap partition info */
101 static int change_info(const char *devname
, const char *label
, const char *uuid
)
105 fd
= open(devname
, O_RDWR
);
107 warn(_("%s: failed to open"), devname
);
111 /* Write the uuid if it was provided */
115 if (uuid_parse(uuid
, newuuid
) == -1)
116 warnx(_("failed to parse UUID: %s"), uuid
);
118 if (lseek(fd
, SWAP_UUID_OFFSET
, SEEK_SET
) !=
120 warn(_("%s: failed to seek to swap UUID"), devname
);
123 } else if (write_all(fd
, newuuid
, sizeof(newuuid
))) {
124 warn(_("%s: failed to write UUID"), devname
);
130 /* Write the label if it was provided */
132 char newlabel
[SWAP_LABEL_LENGTH
];
134 if (lseek(fd
, SWAP_LABEL_OFFSET
, SEEK_SET
) != SWAP_LABEL_OFFSET
) {
135 warn(_("%s: failed to seek to swap label "), devname
);
138 memset(newlabel
, 0, sizeof(newlabel
));
139 xstrncpy(newlabel
, label
, sizeof(newlabel
));
141 if (strlen(label
) > strlen(newlabel
))
142 warnx(_("label is too long. Truncating it to '%s'"),
144 if (write_all(fd
, newlabel
, sizeof(newlabel
))) {
145 warn(_("%s: failed to write label"), devname
);
158 static void __attribute__((__noreturn__
)) usage(FILE *out
)
160 fprintf(out
, _("Usage: %s [options] <device>\n\nOptions:\n"),
161 program_invocation_short_name
);
164 " -h, --help this help\n"
165 " -L, --label <label> specify a new label\n"
166 " -U, --uuid <uuid> specify a new uuid\n"));
168 fprintf(out
, _("\nFor more information see swaplabel(8).\n"));
170 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
173 int main(int argc
, char *argv
[])
175 blkid_probe pr
= NULL
;
176 char *uuid
= NULL
, *label
= NULL
, *devname
;
179 static const struct option longopts
[] = {
180 { "help", 0, 0, 'h' },
181 { "label", 1, 0, 'L' },
182 { "uuid", 1, 0, 'U' },
186 setlocale(LC_ALL
, "");
187 bindtextdomain(PACKAGE
, LOCALEDIR
);
190 while ((c
= getopt_long(argc
, argv
, "hL:U:", longopts
, NULL
)) != -1) {
202 warnx(_("ignore -U (UUIDs are unsupported)"));
214 devname
= argv
[optind
];
215 pr
= get_swap_prober(devname
);
218 rc
= change_info(devname
, label
, uuid
);
220 rc
= print_info(pr
, devname
);
221 blkid_free_probe(pr
);
223 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;