[PATCH] swaplabel: Program to print or change the label on a swap partition

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This patch adds a new program to the project which is analogous to e2label
but for swap partitions. The reason behind having this program is to allow
the labeling of swap partitions without having to turn them off (swapoff)
and then recreating them with mkswap. For many people this may not seem
very important, but for those of us where just the act of disabling a swap
partition is a big deal this utility can be very helpful especially if
needed to do this on hundreds of servers.

Signed-off-by: Jason Borden <jborden@xxxxxxxxxxxx>
---
 disk-utils/Makefile.am |    6 +-
 disk-utils/swaplabel.8 |   51 +++++++++++++++++++++++
 disk-utils/swaplabel.c |  104
++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 158 insertions(+), 3 deletions(-)
 create mode 100644 disk-utils/swaplabel.8
 create mode 100644 disk-utils/swaplabel.c

diff --git a/disk-utils/Makefile.am b/disk-utils/Makefile.am
index ed0a6e4..c66eebf 100644
--- a/disk-utils/Makefile.am
+++ b/disk-utils/Makefile.am
@@ -6,13 +6,13 @@ utils_common += ../lib/linux_version.c
 endif
 
 dist_man_MANS = isosize.8 mkfs.8 mkswap.8 \
-           fsck.minix.8 mkfs.minix.8 mkfs.bfs.8
+           fsck.minix.8 mkfs.minix.8 mkfs.bfs.8 swaplabel.8
 
-sbin_PROGRAMS = mkfs mkswap fsck.minix mkfs.minix mkfs.bfs
+sbin_PROGRAMS = mkfs mkswap fsck.minix mkfs.minix mkfs.bfs swaplabel
 fsck_minix_SOURCES = fsck.minix.c minix.h
 mkfs_minix_SOURCES = mkfs.minix.c minix.h $(utils_common)
 mkfs_bfs_SOURCES = mkfs.bfs.c $(utils_common)
-
+swaplabel_SOURCES = swaplabel.c
 mkswap_SOURCES = mkswap.c $(utils_common) ../lib/wholedisk.c
 mkswap_LDADD =
 mkswap_CFLAGS = $(AM_CFLAGS)
diff --git a/disk-utils/swaplabel.8 b/disk-utils/swaplabel.8
new file mode 100644
index 0000000..988d393
--- /dev/null
+++ b/disk-utils/swaplabel.8
@@ -0,0 +1,51 @@
+.\" Copyright 2010 Jason Borden <jborden@xxxxxxxxxxxx>
+.\"
+.\" This file may be copied under the terms of the GNU Public License.
+.\"
+.TH SWAPLABEL 8 "4 March 2010" "Linux" "Linux Programmer's Manual"
+.SH NAME
+swaplabel \- Print or change the label on a swap partition
+.SH SYNOPSIS
+.B swaplabel
+.I device
+[
+.I new-label
+]
+.SH DESCRIPTION
+.B swaplabel
+will display or change the label on a swap partition located on
+.I device.
+.PP
+If the optional argument
+.I new-label
+is not present,
+.B swaplabel
+will simply display the current swap partition label.
+.PP
+If the optional argument
+.I new-label
+is present, then
+.B swaplabel
+will set the swap partition label to be
+.IR new-label .
+Swap partition labels can be at most 16 characters long; if
+.I new-label
+is longer than 16 characters,
+.B swaplabel
+will truncate it and print a warning message.
+.PP
+It is also possible to set the swap label when making the swap partition
using
 the
+.B \-L
+option of
+.BR mkswap (8).
+.PP
+.SH AUTHOR
+.B swaplabel
+was written by Jason Borden <jborden@xxxxxxxxxxxx>.
+.SH AVAILABILITY
+.B swaplabel
+is part of the util-linux-ng package and is available from
ftp://ftp.kernel.or
g/pub/linux/utils/util-linux-ng/.
+.SH SEE ALSO
+.BR mkswap (8),
+.BR swapon (8)
+
diff --git a/disk-utils/swaplabel.c b/disk-utils/swaplabel.c
new file mode 100644
index 0000000..bb116cb
--- /dev/null
+++ b/disk-utils/swaplabel.c
@@ -0,0 +1,104 @@
+/*
+ * swaplabel.c - Print or change the volume label on a swap partition
+ *
+ * Copyright 2010 Jason Borden <jborden@xxxxxxxxxxxx>
+ *
+ * Usage: swaplabel device [new-label]
+ *
+ * This file may be redistributed under the terms of the GNU Public
License.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <errno.h>
+#include "nls.h"
+
+#define SWAP_MAGIC_OFFSET 0xFF6
+#define SWAP_MAGIC_VALUE "SWAPSPACE2"
+#define SWAP_MAGIC_LENGTH 10
+
+#define SWAP_LABEL_OFFSET 0x41C
+#define SWAP_LABEL_LENGTH 16
+
+/* A function to print errors and exit */
+static void perror_exit(const char *perrorstr, int exitcode, const char
*stder
rstr, ...)
+{
+    va_list ap;
+
+    if (perrorstr)
+        perror(perrorstr);
+    va_start(ap, stderrstr);
+    vfprintf(stderr, stderrstr, ap);
+    va_end(ap);
+    exit(exitcode);
+}
+
+/* Open the swap partition, verify the magic, and seek to the label */
+static int open_swap(const char *program_name, const char *dev, int mode)
+{
+    int fd;
+    char magic_buffer[SWAP_MAGIC_LENGTH];
+
+    fd = open(dev, mode);
+    if (fd < 0)
+        perror_exit(dev, 1, _("%s: cannot open %s\n"), program_name,
dev);
+    if (lseek(fd, SWAP_MAGIC_OFFSET, SEEK_SET) != SWAP_MAGIC_OFFSET)
+        perror_exit(dev, 1, _("%s: cannot seek to swap magic\n"),
program_name
);
+    if (read(fd, magic_buffer, sizeof(magic_buffer)) !=
sizeof(magic_buffer))
+        perror_exit(dev, 1, _("%s: error reading swap magic\n"),
program_name)
;
+    if (memcmp(magic_buffer, SWAP_MAGIC_VALUE, SWAP_MAGIC_LENGTH) != 0)
+        perror_exit(NULL, 1, _("%s: %s is not a valid swap partition\n"),
+            program_name, dev);
+    if (lseek(fd, SWAP_LABEL_OFFSET, SEEK_SET) != SWAP_LABEL_OFFSET)
+        perror_exit(dev, 1, _("%s: cannot seek to swap label\n"),
program_name
);
+
+    return fd;
+}
+
+static void print_label(const char *program_name, const char *dev)
+{
+    int fd;
+    char swaplabel[SWAP_LABEL_LENGTH + 1]; /* +1 for null terminator */
+
+    fd = open_swap(program_name, dev, O_RDONLY);
+    if (read(fd, swaplabel, SWAP_LABEL_LENGTH) != SWAP_LABEL_LENGTH)
+        perror_exit(dev, 1, _("%s: cannot read swap label\n"),
program_name);
+    swaplabel[SWAP_LABEL_LENGTH] = 0;
+    printf("%s\n", swaplabel);
+    close(fd);
+}
+
+static void change_label(const char *program_name, const char *dev, const
char
 *newlabel)
+{
+    int fd;
+    char swaplabel[SWAP_LABEL_LENGTH + 1]; /* +1 for null terminator */
+
+    fd = open_swap(program_name, dev, O_RDWR);
+    memset(swaplabel, 0, sizeof(swaplabel));
+    strncpy(swaplabel, newlabel, SWAP_LABEL_LENGTH);
+    if (strlen(newlabel) > SWAP_LABEL_LENGTH)
+        fprintf(stderr, _("Warning: label is too long. Truncating it to
%s\n")
,
+            swaplabel);
+    if (write(fd, swaplabel, SWAP_LABEL_LENGTH) != SWAP_LABEL_LENGTH)
+        perror_exit(dev, 1, _("%s: error writing label\n"),
program_name);
+    close(fd);
+}
+
+int main (int argc, char *argv[])
+{
+    if (argc == 2)
+        print_label(argv[0], argv[1]);
+    else if (argc == 3)
+        change_label(argv[0], argv[1], argv[2]);
+    else
+        perror_exit(NULL, 1, _("Usage: %s device [new-label]\n"),
argv[0]);
+
+    return 0;
+}
-- 
1.7.0

--
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux