Blob Blame History Raw
From 340ee2f41ee30f8699e676171c37feeb33ca1435 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Thu, 29 Aug 2013 11:32:05 +0100
Subject: [PATCH] format: Add --label option for setting filesystem label.

$ ./run ./format/virt-format -a /tmp/test.img --filesystem=ext2 --label=BOOT
$ ./run ./cat/virt-filesystems -a /tmp/test.img --all --long -h
Name       Type        VFS   Label  MBR  Size  Parent
/dev/sda1  filesystem  ext2  BOOT   -    1.0G  -
/dev/sda1  partition   -     -      83   1.0G  /dev/sda
/dev/sda   device      -     -      -    1.0G  -

Thanks: Gerd Hoffmann (kraxel)
(cherry picked from commit c46e41cb3c41f6283e0350cac6ef0b9178ed3941)
---
 format/format.c        | 10 ++++++++++
 format/virt-format.pod |  4 ++++
 2 files changed, 14 insertions(+)

diff --git a/format/format.c b/format/format.c
index 718fb2b..cff0d82 100644
--- a/format/format.c
+++ b/format/format.c
@@ -47,6 +47,7 @@ const char *libvirt_uri = NULL;
 static const char *filesystem = NULL;
 static const char *vg = NULL, *lv = NULL;
 static const char *partition = "DEFAULT";
+static const char *label = NULL;
 static int wipe = 0;
 static int have_wipefs;
 
@@ -77,6 +78,7 @@ usage (int status)
              "  --filesystem=..      Create empty filesystem\n"
              "  --format[=raw|..]    Force disk format for -a option\n"
              "  --help               Display brief help\n"
+             "  --label=..           Set filesystem label\n"
              "  --lvm=..             Create Linux LVM2 logical volume\n"
              "  --partition=..       Create / set partition type\n"
              "  -v|--verbose         Verbose messages\n"
@@ -107,6 +109,7 @@ main (int argc, char *argv[])
     { "filesystem", 1, 0, 0 },
     { "format", 2, 0, 0 },
     { "help", 0, 0, HELP_OPTION },
+    { "label", 1, 0, 0 },
     { "long-options", 0, 0, 0 },
     { "lvm", 2, 0, 0 },
     { "partition", 2, 0, 0 },
@@ -174,6 +177,8 @@ main (int argc, char *argv[])
           partition = optarg;
       } else if (STREQ (long_options[option_index].name, "wipe")) {
         wipe = 1;
+      } else if (STREQ (long_options[option_index].name, "label")) {
+        label = optarg;
       } else {
         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
                  program_name, long_options[option_index].name, option_index);
@@ -415,6 +420,11 @@ do_format (void)
     if (filesystem) {
       if (guestfs_mkfs_opts (g, filesystem, dev, -1) == -1)
         exit (EXIT_FAILURE);
+
+      if (label) {
+        if (guestfs_set_label (g, dev, label) == -1)
+          exit (EXIT_FAILURE);
+      }
     }
 
     if (free_dev)
diff --git a/format/virt-format.pod b/format/virt-format.pod
index bcab888..ac2b8e6 100755
--- a/format/virt-format.pod
+++ b/format/virt-format.pod
@@ -114,6 +114,10 @@ If you have untrusted raw-format guest disk images, you should use
 this option to specify the disk format.  This avoids a possible
 security problem with malicious guests (CVE-2010-3851).
 
+=item B<--label=LABEL>
+
+Set the filesystem label.
+
 =item B<--lvm=/dev/I<VG>/I<LV>>
 
 Create a Linux LVM2 logical volume called C</dev/I<VG>/I<LV>>.  You
-- 
1.8.3.1