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>
26 # ifdef HAVE_UUID_UUID_H
27 # include <uuid/uuid.h>
35 #include "swapheader.h"
39 #define SWAP_UUID_OFFSET (offsetof(struct swap_header_v1_2, uuid))
40 #define SWAP_LABEL_OFFSET (offsetof(struct swap_header_v1_2, volume_name))
43 * Returns new libblkid prober. This function call exit() on error.
45 static blkid_probe
get_swap_prober(const char *devname
)
49 const char *version
= NULL
;
50 char *swap_filter
[] = { "swap", NULL
};
52 pr
= blkid_new_probe_from_filename(devname
);
54 warn(_("%s: unable to probe device"), devname
);
58 blkid_probe_enable_superblocks(pr
, TRUE
);
59 blkid_probe_set_superblocks_flags(pr
,
60 BLKID_SUBLKS_LABEL
| BLKID_SUBLKS_UUID
|
61 BLKID_SUBLKS_VERSION
);
63 blkid_probe_filter_superblocks_type(pr
, BLKID_FLTR_ONLYIN
, swap_filter
);
65 rc
= blkid_do_safeprobe(pr
);
67 warn(_("%s: unable to probe device"), devname
);
69 warnx(_("%s: ambivalent probing result, use wipefs(8)"), devname
);
71 warnx(_("%s: not a valid swap partition"), devname
);
74 /* supported is SWAPSPACE2 only */
75 blkid_probe_lookup_value(pr
, "VERSION", &version
, NULL
);
76 if (strcmp(version
, "2"))
77 warnx(_("%s: unsupported swap version '%s'"),
87 /* Print the swap partition information */
88 static int print_info(blkid_probe pr
, const char *devname
)
92 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
93 printf("LABEL: %s\n", data
);
95 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
96 printf("UUID: %s\n", data
);
101 /* Change the swap partition info */
102 static int change_info(const char *devname
, const char *label
, const char *uuid
)
106 fd
= open(devname
, O_RDWR
);
108 warn(_("%s: failed to open"), devname
);
112 /* Write the uuid if it was provided */
116 if (uuid_parse(uuid
, newuuid
) == -1)
117 warnx(_("failed to parse UUID: %s"), uuid
);
119 if (lseek(fd
, SWAP_UUID_OFFSET
, SEEK_SET
) !=
121 warn(_("%s: failed to seek to swap UUID"), devname
);
124 } else if (write_all(fd
, newuuid
, sizeof(newuuid
))) {
125 warn(_("%s: failed to write UUID"), devname
);
131 /* Write the label if it was provided */
133 char newlabel
[SWAP_LABEL_LENGTH
];
135 if (lseek(fd
, SWAP_LABEL_OFFSET
, SEEK_SET
) != SWAP_LABEL_OFFSET
) {
136 warn(_("%s: failed to seek to swap label "), devname
);
139 memset(newlabel
, 0, sizeof(newlabel
));
140 xstrncpy(newlabel
, label
, sizeof(newlabel
));
142 if (strlen(label
) > strlen(newlabel
))
143 warnx(_("label is too long. Truncating it to '%s'"),
145 if (write_all(fd
, newlabel
, sizeof(newlabel
))) {
146 warn(_("%s: failed to write label"), devname
);
159 static void __attribute__((__noreturn__
)) usage(FILE *out
)
161 fprintf(out
, _("Usage: %s [options] <device>\n\nOptions:\n"),
162 program_invocation_short_name
);
165 " -h, --help this help\n"
166 " -L, --label <label> specify a new label\n"
167 " -U, --uuid <uuid> specify a new uuid\n"));
169 fprintf(out
, _("\nFor more information see swaplabel(8).\n"));
171 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
174 int main(int argc
, char *argv
[])
176 blkid_probe pr
= NULL
;
177 char *uuid
= NULL
, *label
= NULL
, *devname
;
180 struct option longopts
[] = {
181 { "help", 0, 0, 'h' },
182 { "label", 1, 0, 'L' },
183 { "uuid", 1, 0, 'U' },
187 setlocale(LC_ALL
, "");
188 bindtextdomain(PACKAGE
, LOCALEDIR
);
191 while ((c
= getopt_long(argc
, argv
, "hL:U:", longopts
, NULL
)) != -1) {
203 warnx(_("ignore -U (UUIDs are unsupported)"));
215 devname
= argv
[optind
];
216 pr
= get_swap_prober(devname
);
219 rc
= change_info(devname
, label
, uuid
);
221 rc
= print_info(pr
, devname
);
222 blkid_free_probe(pr
);
224 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;