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>
31 #include "closestream.h"
33 #include "swapheader.h"
34 #include "swapprober.h"
36 #define SWAP_UUID_OFFSET (offsetof(struct swap_header_v1_2, uuid))
37 #define SWAP_LABEL_OFFSET (offsetof(struct swap_header_v1_2, volume_name))
39 /* Print the swap partition information */
40 static int print_info(blkid_probe pr
)
44 if (!blkid_probe_lookup_value(pr
, "LABEL", &data
, NULL
))
45 printf("LABEL: %s\n", data
);
47 if (!blkid_probe_lookup_value(pr
, "UUID", &data
, NULL
))
48 printf("UUID: %s\n", data
);
53 /* Change the swap partition info */
54 static int change_info(const char *devname
, const char *label
, const char *uuid
)
58 fd
= open(devname
, O_RDWR
);
60 warn(_("cannot open %s"), devname
);
64 /* Write the uuid if it was provided */
68 if (uuid_parse(uuid
, newuuid
) == -1)
69 warnx(_("failed to parse UUID: %s"), uuid
);
71 if (lseek(fd
, SWAP_UUID_OFFSET
, SEEK_SET
) !=
73 warn(_("%s: failed to seek to swap UUID"), devname
);
76 } else if (write_all(fd
, newuuid
, sizeof(newuuid
))) {
77 warn(_("%s: failed to write UUID"), devname
);
83 /* Write the label if it was provided */
85 char newlabel
[SWAP_LABEL_LENGTH
];
87 if (lseek(fd
, SWAP_LABEL_OFFSET
, SEEK_SET
) != SWAP_LABEL_OFFSET
) {
88 warn(_("%s: failed to seek to swap label "), devname
);
91 memset(newlabel
, 0, sizeof(newlabel
));
92 xstrncpy(newlabel
, label
, sizeof(newlabel
));
94 if (strlen(label
) > strlen(newlabel
))
95 warnx(_("label is too long. Truncating it to '%s'"),
97 if (write_all(fd
, newlabel
, sizeof(newlabel
))) {
98 warn(_("%s: failed to write label"), devname
);
103 if (close_fd(fd
) != 0) {
104 warn(_("write failed: %s"), devname
);
114 static void __attribute__((__noreturn__
)) usage(void)
117 fputs(USAGE_HEADER
, out
);
118 fprintf(out
, _(" %s [options] <device>\n"),
119 program_invocation_short_name
);
121 fputs(USAGE_SEPARATOR
, out
);
122 fputs(_("Display or change the label or UUID of a swap area.\n"), out
);
124 fputs(USAGE_OPTIONS
, out
);
125 fputs(_(" -L, --label <label> specify a new label\n"
126 " -U, --uuid <uuid> specify a new uuid\n"), out
);
127 fputs(USAGE_SEPARATOR
, out
);
128 printf(USAGE_HELP_OPTIONS(21));
129 printf(USAGE_MAN_TAIL("swaplabel(8)"));
133 int main(int argc
, char *argv
[])
135 blkid_probe pr
= NULL
;
136 char *uuid
= NULL
, *label
= NULL
, *devname
;
139 static const struct option longopts
[] = {
140 { "help", no_argument
, NULL
, 'h' },
141 { "version", no_argument
, NULL
, 'V' },
142 { "label", required_argument
, NULL
, 'L' },
143 { "uuid", required_argument
, NULL
, 'U' },
147 setlocale(LC_ALL
, "");
148 bindtextdomain(PACKAGE
, LOCALEDIR
);
150 atexit(close_stdout
);
152 while ((c
= getopt_long(argc
, argv
, "hVL:U:", longopts
, NULL
)) != -1) {
158 printf(UTIL_LINUX_VERSION
);
167 warnx(_("ignore -U (UUIDs are unsupported)"));
171 errtryhelp(EXIT_FAILURE
);
175 if (optind
== argc
) {
176 warnx(_("no device specified"));
177 errtryhelp(EXIT_FAILURE
);
179 devname
= argv
[optind
];
180 pr
= get_swap_prober(devname
);
183 rc
= change_info(devname
, label
, uuid
);
186 blkid_free_probe(pr
);
188 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;