#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;
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;
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);
}
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"];
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
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.
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
# 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