Blob Blame History Raw
From 48035cf253bdf27a1a7d460b86e19e53cfc82e3b Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Thu, 29 Aug 2013 11:41:46 +0100
Subject: [PATCH] daemon: Allow labels to be set on DOS filesystems using
 'dosfslabel'.

You can now use virt-format or virt-make-fs --label option to set a
label for a DOS filesystem:

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

This also contains a small code refactoring.

Thanks: Gerd Hoffmann (kraxel)
(cherry picked from commit a75ca610b80614253f8a9d9828a97dc61d919b8a)
---
 daemon/labels.c | 61 ++++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 41 insertions(+), 20 deletions(-)

diff --git a/daemon/labels.c b/daemon/labels.c
index 10e40dc..f417c57 100644
--- a/daemon/labels.c
+++ b/daemon/labels.c
@@ -28,11 +28,43 @@
 #include "optgroups.h"
 
 GUESTFSD_EXT_CMD(str_btrfs, btrfs);
+GUESTFSD_EXT_CMD(str_dosfslabel, dosfslabel);
 GUESTFSD_EXT_CMD(str_e2label, e2label);
 GUESTFSD_EXT_CMD(str_ntfslabel, ntfslabel);
 GUESTFSD_EXT_CMD(str_xfs_admin, xfs_admin);
 
 static int
+btrfslabel (const char *device, const char *label)
+{
+  int r;
+  CLEANUP_FREE char *err = NULL;
+
+  r = command (NULL, &err, str_btrfs, "filesystem", "label",
+               device, label, NULL);
+  if (r == -1) {
+    reply_with_error ("%s", err);
+    return -1;
+  }
+
+  return 0;
+}
+
+static int
+dosfslabel (const char *device, const char *label)
+{
+  int r;
+  CLEANUP_FREE char *err = NULL;
+
+  r = command (NULL, &err, str_dosfslabel, device, label, NULL);
+  if (r == -1) {
+    reply_with_error ("%s", err);
+    return -1;
+  }
+
+  return 0;
+}
+
+static int
 e2label (const char *device, const char *label)
 {
   int r;
@@ -95,22 +127,6 @@ xfslabel (const char *device, const char *label)
   return 0;
 }
 
-static int
-btrfslabel (const char *device, const char *label)
-{
-  int r;
-  CLEANUP_FREE char *err = NULL;
-
-  r = command (NULL, &err, str_btrfs, "filesystem", "label",
-               device, label, NULL);
-  if (r == -1) {
-    reply_with_error ("%s", err);
-    return -1;
-  }
-
-  return 0;
-}
-
 int
 do_set_label (const mountable_t *mountable, const char *label)
 {
@@ -121,7 +137,15 @@ do_set_label (const mountable_t *mountable, const char *label)
   if (vfs_type == NULL)
     return -1;
 
-  if (fstype_is_extfs (vfs_type))
+  if (STREQ (vfs_type, "btrfs"))
+    r = btrfslabel (mountable->device, label);
+
+  else if (STREQ (vfs_type, "msdos") ||
+           STREQ (vfs_type, "fat") ||
+           STREQ (vfs_type, "vfat"))
+    r = dosfslabel (mountable->device, label);
+
+  else if (fstype_is_extfs (vfs_type))
     r = e2label (mountable->device, label);
 
   else if (STREQ (vfs_type, "ntfs"))
@@ -130,9 +154,6 @@ do_set_label (const mountable_t *mountable, const char *label)
   else if (STREQ (vfs_type, "xfs"))
     r = xfslabel (mountable->device, label);
 
-  else if (STREQ (vfs_type, "btrfs"))
-    r = btrfslabel (mountable->device, label);
-
   else {
     reply_with_error ("don't know how to set the label for '%s' filesystems",
                       vfs_type);
-- 
1.8.3.1