New API: mkfs-btrfs for creating btrfs filesystems (RHBZ#807905).

This allows all parameters from btrfs to be accessed.
This commit is contained in:
Richard W.M. Jones
2012-04-02 10:12:38 +01:00
parent 77ac6b73cd
commit 50009403b3
6 changed files with 127 additions and 4 deletions

View File

@@ -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;
}

View File

@@ -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 {

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -1 +1 @@
316
317