Richard W.M. Jones
2012-Apr-02 09:15 UTC
[Libguestfs] [PATCH 0/2] Fix btrfs blocksize and bind mkfs.btrfs (RHBZ#807905).
https://bugzilla.redhat.com/show_bug.cgi?id=807905 Currently if you specify the blocksize parameter to mkfs-opts with a btrfs filesystem, then it fails, because mkfs.btrfs interprets the -b option as meaning filesystem size. The first patch fixes this by disallowing blocksize (it cannot be mapped meaningfully into btrfs parameters). The second patch adds the full /sbin/mkfs.btrfs utility to the API. We should eventually do the same for mke2fs. Rich.
Richard W.M. Jones
2012-Apr-02 09:15 UTC
[Libguestfs] [PATCH 1/2] mkfs: Don't allow blocksize to be set on btrfs (RHBZ#807905).
From: "Richard W.M. Jones" <rjones at redhat.com> --- daemon/mkfs.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/daemon/mkfs.c b/daemon/mkfs.c index 5475582..492b690 100644 --- a/daemon/mkfs.c +++ b/daemon/mkfs.c @@ -133,6 +133,11 @@ do_mkfs_opts (const char *fstype, const char *device, int blocksize, ADD_ARG (argv, i, "-c"); ADD_ARG (argv, i, blocksize_str); } + else if (STREQ (fstype, "btrfs")) { + /* For btrfs, blocksize cannot be specified (RHBZ#807905). */ + reply_with_error ("blocksize cannot be set on btrfs filesystems"); + return -1; + } else { /* For all other filesystem types, try the -b option. */ snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize); -- 1.7.9.3
Richard W.M. Jones
2012-Apr-02 09:15 UTC
[Libguestfs] [PATCH 2/2] New API: mkfs-btrfs for creating btrfs filesystems (RHBZ#807905).
From: "Richard W.M. Jones" <rjones at redhat.com> This allows all parameters from btrfs to be accessed. --- daemon/btrfs.c | 109 ++++++++++++++++++++++++++++++++++++++++ daemon/mkfs.c | 2 +- generator/generator_actions.ml | 11 ++++ gobject/Makefile.inc | 6 ++- po/POTFILES.in | 1 + src/MAX_PROC_NR | 2 +- 6 files changed, 127 insertions(+), 4 deletions(-) diff --git a/daemon/btrfs.c b/daemon/btrfs.c index a20ee08..64d84ed 100644 --- a/daemon/btrfs.c +++ b/daemon/btrfs.c @@ -84,3 +84,112 @@ do_btrfs_filesystem_resize (const char *filesystem, int64_t size) free (err); return 0; } + +/* Takes optional arguments, consult optargs_bitmask. */ +int +do_mkfs_btrfs (const char *device, + int64_t allocstart, int64_t bytecount, const char *datatype, + int leafsize, const char *label, const char *metadata, + int nodesize, int sectorsize) +{ + const char *argv[MAX_ARGS]; + size_t i = 0; + int r; + char *err; + char allocstart_s[64]; + char bytecount_s[64]; + char leafsize_s[64]; + char nodesize_s[64]; + char sectorsize_s[64]; + + ADD_ARG (argv, i, "mkfs.btrfs"); + + /* Optional arguments. */ + if (optargs_bitmask & GUESTFS_MKFS_BTRFS_ALLOCSTART_BITMASK) { + if (allocstart < 0) { + reply_with_error ("allocstart must be >= 0"); + return -1; + } + snprintf (allocstart_s, sizeof allocstart_s, "%" PRIi64, allocstart); + ADD_ARG (argv, i, "--alloc-start"); + ADD_ARG (argv, i, allocstart_s); + } + + if (optargs_bitmask & GUESTFS_MKFS_BTRFS_BYTECOUNT_BITMASK) { + if (bytecount <= 0) { /* actually the minimum is 256MB */ + reply_with_error ("bytecount must be > 0"); + return -1; + } + snprintf (bytecount_s, sizeof bytecount_s, "%" PRIi64, bytecount); + ADD_ARG (argv, i, "--byte-count"); + ADD_ARG (argv, i, bytecount_s); + } + + if (optargs_bitmask & GUESTFS_MKFS_BTRFS_DATATYPE_BITMASK) { + if (STRNEQ (datatype, "raid0") && STRNEQ (datatype, "raid1") && + STRNEQ (datatype, "raid10") && STRNEQ (datatype, "single")) { + reply_with_error ("datatype not one of the allowed values"); + return -1; + } + ADD_ARG (argv, i, "--data"); + ADD_ARG (argv, i, datatype); + } + + if (optargs_bitmask & GUESTFS_MKFS_BTRFS_LEAFSIZE_BITMASK) { + if (!is_power_of_2 (leafsize) || leafsize <= 0) { + reply_with_error ("leafsize must be > 0 and a power of two"); + return -1; + } + snprintf (leafsize_s, sizeof leafsize_s, "%d", leafsize); + ADD_ARG (argv, i, "--leafsize"); + ADD_ARG (argv, i, leafsize_s); + } + + if (optargs_bitmask & GUESTFS_MKFS_BTRFS_LABEL_BITMASK) { + ADD_ARG (argv, i, "--label"); + ADD_ARG (argv, i, label); + } + + if (optargs_bitmask & GUESTFS_MKFS_BTRFS_METADATA_BITMASK) { + if (STRNEQ (metadata, "raid0") && STRNEQ (metadata, "raid1") && + STRNEQ (metadata, "raid10") && STRNEQ (metadata, "single")) { + reply_with_error ("metadata not one of the allowed values"); + return -1; + } + ADD_ARG (argv, i, "--metadata"); + ADD_ARG (argv, i, metadata); + } + + if (optargs_bitmask & GUESTFS_MKFS_BTRFS_NODESIZE_BITMASK) { + if (!is_power_of_2 (nodesize) || nodesize <= 0) { + reply_with_error ("nodesize must be > 0 and a power of two"); + return -1; + } + snprintf (nodesize_s, sizeof nodesize_s, "%d", nodesize); + ADD_ARG (argv, i, "--nodesize"); + ADD_ARG (argv, i, nodesize_s); + } + + if (optargs_bitmask & GUESTFS_MKFS_BTRFS_SECTORSIZE_BITMASK) { + if (!is_power_of_2 (sectorsize) || sectorsize <= 0) { + reply_with_error ("sectorsize must be > 0 and a power of two"); + return -1; + } + snprintf (sectorsize_s, sizeof sectorsize_s, "%d", sectorsize); + ADD_ARG (argv, i, "--sectorsize"); + ADD_ARG (argv, i, sectorsize_s); + } + + ADD_ARG (argv, i, device); + ADD_ARG (argv, i, NULL); + + r = commandv (NULL, &err, argv); + if (r == -1) { + reply_with_error ("%s: %s", device, err); + free (err); + return -1; + } + + free (err); + return 0; +} diff --git a/daemon/mkfs.c b/daemon/mkfs.c index 492b690..85442e0 100644 --- a/daemon/mkfs.c +++ b/daemon/mkfs.c @@ -135,7 +135,7 @@ do_mkfs_opts (const char *fstype, const char *device, int blocksize, } else if (STREQ (fstype, "btrfs")) { /* For btrfs, blocksize cannot be specified (RHBZ#807905). */ - reply_with_error ("blocksize cannot be set on btrfs filesystems"); + reply_with_error ("blocksize cannot be set on btrfs filesystems, use 'mkfs-btrfs'"); return -1; } else { diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index bbf3c6f..4c331af 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -6929,6 +6929,17 @@ replacement =back"); + ("mkfs_btrfs", (RErr, [Device "device"], [OInt64 "allocstart"; OInt64 "bytecount"; OString "datatype"; OInt "leafsize"; OString "label"; OString "metadata"; OInt "nodesize"; OInt "sectorsize"]), 317, [Optional "btrfs"], + [InitEmpty, Always, TestRun ( + [["part_disk"; "/dev/sda"; "mbr"]; + ["mkfs_btrfs"; "/dev/sda1"; "0"; "268435456"; "single"; "4096"; "test"; "single"; "4096"; "512"]])], + "create a btrfs filesystem", + "\ +Create a btrfs filesystem, allowing all configurables to be set. +For more information on the optional arguments, see L<mkfs.btrfs(8)>. + +To create general filesystems, use C<guestfs_mkfs_opts>."); + ] let all_functions = non_daemon_functions @ daemon_functions diff --git a/gobject/Makefile.inc b/gobject/Makefile.inc index ec6577d..6bbe688 100644 --- a/gobject/Makefile.inc +++ b/gobject/Makefile.inc @@ -57,7 +57,8 @@ guestfs_gobject_headers=\ guestfs-gobject-optargs-md_create.h \ guestfs-gobject-optargs-e2fsck.h \ guestfs-gobject-optargs-ntfsfix.h \ - guestfs-gobject-optargs-ntfsclone_out.h + guestfs-gobject-optargs-ntfsclone_out.h \ + guestfs-gobject-optargs-mkfs_btrfs.h guestfs_gobject_sources=\ guestfs-gobject-session.c \ @@ -96,4 +97,5 @@ guestfs_gobject_sources=\ guestfs-gobject-optargs-md_create.c \ guestfs-gobject-optargs-e2fsck.c \ guestfs-gobject-optargs-ntfsfix.c \ - guestfs-gobject-optargs-ntfsclone_out.c + guestfs-gobject-optargs-ntfsclone_out.c \ + guestfs-gobject-optargs-mkfs_btrfs.c diff --git a/po/POTFILES.in b/po/POTFILES.in index 40842ea..8a52e08 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -142,6 +142,7 @@ gobject/guestfs-gobject-optargs-copy_file_to_file.c gobject/guestfs-gobject-optargs-e2fsck.c gobject/guestfs-gobject-optargs-inspect_get_icon.c gobject/guestfs-gobject-optargs-md_create.c +gobject/guestfs-gobject-optargs-mkfs_btrfs.c gobject/guestfs-gobject-optargs-mkfs_opts.c gobject/guestfs-gobject-optargs-mount_9p.c gobject/guestfs-gobject-optargs-mount_local.c diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 47eb669..4dab36b 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -316 +317 -- 1.7.9.3