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

[Libguestfs] [PATCH 2/2] New API: mkfs_opts, mkfs with optional arguments.



-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
>From 0710326ac5f1a06cf1dc3500617d04cf4cba5631 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones redhat com>
Date: Thu, 2 Dec 2010 13:35:14 +0000
Subject: [PATCH 2/2] New API: mkfs_opts, mkfs with optional arguments.

This is an extensible version of 'mkfs' which supports optional
arguments.  There is now no need for 'mkfs_b' since you should
use 'mkfs_opts' with the optional 'blocksize' argument instead.
---
 contrib/visualize-alignment/README  |    2 +-
 daemon/mkfs.c                       |   98 +++++++++++++++++-----------------
 generator/generator_actions.ml      |   29 ++++++++++-
 images/guest-aux/make-debian-img.sh |   10 ++--
 images/guest-aux/make-fedora-img.sh |   10 ++--
 images/guest-aux/make-ubuntu-img.sh |    4 +-
 src/MAX_PROC_NR                     |    2 +-
 7 files changed, 91 insertions(+), 64 deletions(-)

diff --git a/contrib/visualize-alignment/README b/contrib/visualize-alignment/README
index 6c30f94..e68a902 100644
--- a/contrib/visualize-alignment/README
+++ b/contrib/visualize-alignment/README
@@ -54,7 +54,7 @@ qemu patch in the current directory.
       pvcreate /dev/vda : \
       vgcreate VG /dev/vda : \
       lvcreate LV VG 32 : \
-      mkfs-b ext4 4096 /dev/VG/LV
+      mkfs-opts ext4 /dev/VG/LV blocksize:4096
 
   Some points to note:
     * an ext4 filesystem, so it has a journal and extents
diff --git a/daemon/mkfs.c b/daemon/mkfs.c
index ad3014c..911fad3 100644
--- a/daemon/mkfs.c
+++ b/daemon/mkfs.c
@@ -31,12 +31,13 @@
 
 #define MAX_ARGS 16
 
-static int
-mkfs (const char *fstype, const char *device,
-      const char **extra, size_t nr_extra)
+/* Takes optional arguments, consult optargs_bitmask. */
+int
+do_mkfs_opts (const char *fstype, const char *device, int blocksize)
 {
   const char *argv[MAX_ARGS];
   size_t i = 0, j;
+  char blocksize_str[32];
   int r;
   char *err;
 
@@ -72,8 +73,47 @@ mkfs (const char *fstype, const char *device,
     argv[i++] = "-O";
   }
 
-  for (j = 0; j < nr_extra; ++j)
-    argv[i++] = extra[j];
+  /* Process blocksize parameter if set. */
+  if (optargs_bitmask & GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK) {
+    if (blocksize <= 0 || !is_power_of_2 (blocksize)) {
+      reply_with_error ("block size must be > 0 and a power of 2");
+      return -1;
+    }
+
+    if (STREQ (fstype, "vfat") ||
+        STREQ (fstype, "msdos")) {
+      /* For VFAT map the blocksize into a cluster size.  However we
+       * have to determine the block device sector size in order to do
+       * this.
+       */
+      int sectorsize = do_blockdev_getss (device);
+      if (sectorsize == -1)
+        return -1;
+
+      int sectors_per_cluster = blocksize / sectorsize;
+      if (sectors_per_cluster < 1 || sectors_per_cluster > 128) {
+        reply_with_error ("unsupported cluster size for %s filesystem (requested cluster size = %d, sector size = %d, trying sectors per cluster = %d)",
+                          fstype, blocksize, sectorsize, sectors_per_cluster);
+        return -1;
+      }
+
+      snprintf (blocksize_str, sizeof blocksize_str, "%d", sectors_per_cluster);
+      argv[i++] = "-s";
+      argv[i++] = blocksize_str;
+    }
+    else if (STREQ (fstype, "ntfs")) {
+      /* For NTFS map the blocksize into a cluster size. */
+      snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
+      argv[i++] = "-c";
+      argv[i++] = blocksize_str;
+    }
+    else {
+      /* For all other filesystem types, try the -b option. */
+      snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
+      argv[i++] = "-b";
+      argv[i++] = blocksize_str;
+    }
+  }
 
   argv[i++] = device;
   argv[i++] = NULL;
@@ -95,53 +135,13 @@ mkfs (const char *fstype, const char *device,
 int
 do_mkfs (const char *fstype, const char *device)
 {
-  return mkfs (fstype, device, NULL, 0);
+  optargs_bitmask = 0;
+  return do_mkfs_opts (fstype, device, 0);
 }
 
 int
 do_mkfs_b (const char *fstype, int blocksize, const char *device)
 {
-  const char *extra[2];
-  char n[32];
-
-  if (blocksize <= 0 || !is_power_of_2 (blocksize)) {
-    reply_with_error ("block size must be > 0 and a power of 2");
-    return -1;
-  }
-
-  if (STREQ (fstype, "vfat") ||
-      STREQ (fstype, "msdos")) {
-    /* For VFAT map the blocksize into a cluster size.  However we
-     * have to determine the block device sector size in order to do
-     * this.
-     */
-    int sectorsize = do_blockdev_getss (device);
-    if (sectorsize == -1)
-      return -1;
-
-    int sectors_per_cluster = blocksize / sectorsize;
-    if (sectors_per_cluster < 1 || sectors_per_cluster > 128) {
-      reply_with_error ("unsupported cluster size for %s filesystem (requested cluster size = %d, sector size = %d, trying sectors per cluster = %d)",
-                        fstype, blocksize, sectorsize, sectors_per_cluster);
-      return -1;
-    }
-
-    snprintf (n, sizeof n, "%d", sectors_per_cluster);
-    extra[0] = "-s";
-    extra[1] = n;
-  }
-  else if (STREQ (fstype, "ntfs")) {
-    /* For NTFS map the blocksize into a cluster size. */
-    snprintf (n, sizeof n, "%d", blocksize);
-    extra[0] = "-c";
-    extra[1] = n;
-  }
-  else {
-    /* For all other filesystem types, try the -b option. */
-    snprintf (n, sizeof n, "%d", blocksize);
-    extra[0] = "-b";
-    extra[1] = n;
-  }
-
-  return mkfs (fstype, device, extra, 2);
+  optargs_bitmask = GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK;
+  return do_mkfs_opts (fstype, device, blocksize);
 }
diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml
index a405fd4..5624dec 100644
--- a/generator/generator_actions.ml
+++ b/generator/generator_actions.ml
@@ -4186,7 +4186,7 @@ This gets the SELinux security context of the daemon.
 See the documentation about SELINUX in L<guestfs(3)>,
 and C<guestfs_setcon>");
 
-  ("mkfs_b", (RErr, [String "fstype"; Int "blocksize"; Device "device"], []), 187, [],
+  ("mkfs_b", (RErr, [String "fstype"; Int "blocksize"; Device "device"], []), 187, [DeprecatedBy "mkfs_opts"],
    [InitEmpty, Always, TestOutput (
       [["part_disk"; "/dev/sda"; "mbr"];
        ["mkfs_b"; "ext2"; "4096"; "/dev/sda1"];
@@ -5606,6 +5606,33 @@ not refer to a logical volume.
 
 See also C<guestfs_is_lv>.");
 
+  ("mkfs_opts", (RErr, [String "fstype"; Device "device"], [Int "blocksize"]), 278, [],
+   [InitEmpty, Always, TestOutput (
+      [["part_disk"; "/dev/sda"; "mbr"];
+       ["mkfs_opts"; "ext2"; "/dev/sda1"; "4096"];
+       ["mount_options"; ""; "/dev/sda1"; "/"];
+       ["write"; "/new"; "new file contents"];
+       ["cat"; "/new"]], "new file contents")],
+   "make a filesystem",
+   "\
+This function creates a filesystem on C<device>.  The filesystem
+type is C<fstype>, for example C<ext3>.
+
+The optional arguments are:
+
+=over 4
+
+=item C<blocksize>
+
+The filesystem block size.  Supported block sizes depend on the
+filesystem type, but typically they are C<1024>, C<2048> or C<4096>
+for Linux ext2/3 filesystems.
+
+For VFAT and NTFS the C<blocksize> parameter is treated as
+the requested cluster size.
+
+=back");
+
 ]
 
 let all_functions = non_daemon_functions @ daemon_functions
diff --git a/images/guest-aux/make-debian-img.sh b/images/guest-aux/make-debian-img.sh
index bb34672..2170cec 100755
--- a/images/guest-aux/make-debian-img.sh
+++ b/images/guest-aux/make-debian-img.sh
@@ -48,18 +48,18 @@ lvcreate var debian 32
 lvcreate home debian 32
 
 # Phony /boot filesystem.
-mkfs-b ext2 4096 /dev/sda1
+mkfs-opts ext2 /dev/sda1 blocksize:4096
 set-e2label /dev/sda1 BOOT
 set-e2uuid /dev/sda1 01234567-0123-0123-0123-012345678901
 
 # Phony root and other filesystems.
-mkfs-b ext2 4096 /dev/debian/root
+mkfs-opts ext2 /dev/debian/root blocksize:4096
 set-e2uuid /dev/debian/root 01234567-0123-0123-0123-012345678902
-mkfs-b ext2 4096 /dev/debian/usr
+mkfs-opts ext2 /dev/debian/usr blocksize:4096
 set-e2uuid /dev/debian/usr 01234567-0123-0123-0123-012345678903
-mkfs-b ext2 4096 /dev/debian/var
+mkfs-opts ext2 /dev/debian/var blocksize:4096
 set-e2uuid /dev/debian/var 01234567-0123-0123-0123-012345678904
-mkfs-b ext2 4096 /dev/debian/home
+mkfs-opts ext2 /dev/debian/home blocksize:4096
 set-e2uuid /dev/debian/home 01234567-0123-0123-0123-012345678905
 
 # Enough to fool inspection API.
diff --git a/images/guest-aux/make-fedora-img.sh b/images/guest-aux/make-fedora-img.sh
index 3198930..8c38e51 100755
--- a/images/guest-aux/make-fedora-img.sh
+++ b/images/guest-aux/make-fedora-img.sh
@@ -48,12 +48,12 @@ lvcreate LV2 VG 32
 lvcreate LV3 VG 64
 
 # Phony /boot filesystem.
-mkfs-b ext2 4096 /dev/sda1
+mkfs-opts ext2 /dev/sda1 blocksize:4096
 set-e2label /dev/sda1 BOOT
 set-e2uuid /dev/sda1 01234567-0123-0123-0123-012345678901
 
 # Phony root filesystem.
-mkfs-b ext2 4096 /dev/VG/Root
+mkfs-opts ext2 /dev/VG/Root blocksize:4096
 set-e2label /dev/VG/Root ROOT
 set-e2uuid /dev/VG/Root 01234567-0123-0123-0123-012345678902
 
@@ -92,9 +92,9 @@ mknod 0777 10 10 /bin/test7
 
 # Other filesystems.
 # Note that these should be empty, for testing virt-df.
-mkfs-b ext2 4096 /dev/VG/LV1
-mkfs-b ext2 1024 /dev/VG/LV2
-mkfs-b ext2 2048 /dev/VG/LV3
+mkfs-opts ext2 /dev/VG/LV1 blocksize:4096
+mkfs-opts ext2 /dev/VG/LV2 blocksize:1024
+mkfs-opts ext2 /dev/VG/LV3 blocksize:2048
 EOF
 
 rm fstab.tmp
diff --git a/images/guest-aux/make-ubuntu-img.sh b/images/guest-aux/make-ubuntu-img.sh
index 8d62e5d..f008c76 100755
--- a/images/guest-aux/make-ubuntu-img.sh
+++ b/images/guest-aux/make-ubuntu-img.sh
@@ -46,12 +46,12 @@ part-add /dev/sda p 64     524287
 part-add /dev/sda p 524288    -64
 
 # Phony /boot filesystem.
-mkfs-b ext2 4096 /dev/sda1
+mkfs-opts ext2 /dev/sda1 blocksize:4096
 set-e2label /dev/sda1 BOOT
 set-e2uuid /dev/sda1 01234567-0123-0123-0123-012345678901
 
 # Phony root filesystem (Ubuntu doesn't use LVM by default).
-mkfs-b ext2 4096 /dev/sda2
+mkfs-opts ext2 /dev/sda2 blocksize:4096
 set-e2uuid /dev/sda2 01234567-0123-0123-0123-012345678902
 
 # Enough to fool inspection API.
diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR
index 2681747..3d242f5 100644
--- a/src/MAX_PROC_NR
+++ b/src/MAX_PROC_NR
@@ -1 +1 @@
-277
+278
-- 
1.7.3.2


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