コード例 #1
0
ファイル: ext2.c プロジェクト: will-Do/libguestfs
int
do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
              const char *uuid)
{
    CLEANUP_FREE char *err = NULL;
    int r;

    if (!fstype_is_extfs (fstype)) {
        reply_with_error ("%s: not a valid extended filesystem type", fstype);
        return -1;
    }

    char blocksize_s[32];
    snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);

    size_t len = strlen (uuid);
    char jdev[len+32];
    snprintf (jdev, len+32, "device=UUID=%s", uuid);

    wipe_device_before_mkfs (device);

    r = command (NULL, &err,
                 str_mke2fs, "-F", "-t", fstype, "-J", jdev, "-b", blocksize_s,
                 device, NULL);
    if (r == -1) {
        reply_with_error ("%s", err);
        return -1;
    }

    return 0;
}
コード例 #2
0
ファイル: ext2.c プロジェクト: will-Do/libguestfs
int
do_mke2journal_L (int blocksize, const char *label, const char *device)
{
    CLEANUP_FREE char *err = NULL;
    int r;

    if (strlen (label) > EXT2_LABEL_MAX) {
        reply_with_error ("%s: ext2/3/4 labels are limited to %d bytes",
                          label, EXT2_LABEL_MAX);
        return -1;
    }

    char blocksize_s[32];
    snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);

    wipe_device_before_mkfs (device);

    r = command (NULL, &err,
                 str_mke2fs, "-F", "-O", "journal_dev", "-b", blocksize_s,
                 "-L", label,
                 device, NULL);
    if (r == -1) {
        reply_with_error ("%s", err);
        return -1;
    }

    return 0;
}
コード例 #3
0
ファイル: swap.c プロジェクト: libguestfs/libguestfs
/* Takes optional arguments, consult optargs_bitmask. */
int
do_mkswap (const char *device, const char *label, const char *uuid)
{
  const size_t MAX_ARGS = 64;
  const char *argv[MAX_ARGS];
  size_t i = 0;
  int r;
  CLEANUP_FREE char *err = NULL;

  ADD_ARG (argv, i, "mkswap");
  ADD_ARG (argv, i, "-f");

  if (optargs_bitmask & GUESTFS_MKSWAP_LABEL_BITMASK) {
    assert (label != NULL); /* suppress a warning with -O3 */
    if (strlen (label) > SWAP_LABEL_MAX) {
      reply_with_error ("%s: Linux swap labels are limited to %d bytes",
                        label, SWAP_LABEL_MAX);
      return -1;
    }

    ADD_ARG (argv, i, "-L");
    ADD_ARG (argv, i, label);
  }

  if (optargs_bitmask & GUESTFS_MKSWAP_UUID_BITMASK) {
    ADD_ARG (argv, i, "-U");
    ADD_ARG (argv, i, uuid);
  }

  ADD_ARG (argv, i, device);
  ADD_ARG (argv, i, NULL);

  wipe_device_before_mkfs (device);

  r = commandv (NULL, &err, argv);
  if (r == -1) {
    reply_with_error ("%s: %s", device, err);
    return -1;
  }

  udev_settle ();

  return 0;
}
コード例 #4
0
ファイル: ext2.c プロジェクト: will-Do/libguestfs
int
do_mke2journal (int blocksize, const char *device)
{
    CLEANUP_FREE char *err = NULL;
    int r;

    char blocksize_s[32];
    snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);

    wipe_device_before_mkfs (device);

    r = command (NULL, &err,
                 str_mke2fs, "-F", "-O", "journal_dev", "-b", blocksize_s,
                 device, NULL);
    if (r == -1) {
        reply_with_error ("%s", err);
        return -1;
    }

    return 0;
}
コード例 #5
0
ファイル: ext2.c プロジェクト: will-Do/libguestfs
int
do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
              const char *label)
{
    CLEANUP_FREE char *err = NULL;
    int r;

    if (!fstype_is_extfs (fstype)) {
        reply_with_error ("%s: not a valid extended filesystem type", fstype);
        return -1;
    }

    if (strlen (label) > EXT2_LABEL_MAX) {
        reply_with_error ("%s: ext2/3/4 labels are limited to %d bytes",
                          label, EXT2_LABEL_MAX);
        return -1;
    }

    char blocksize_s[32];
    snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);

    size_t len = strlen (label);
    char jdev[len+32];
    snprintf (jdev, len+32, "device=LABEL=%s", label);

    wipe_device_before_mkfs (device);

    r = command (NULL, &err,
                 str_mke2fs, "-F", "-t", fstype, "-J", jdev, "-b", blocksize_s,
                 device, NULL);
    if (r == -1) {
        reply_with_error ("%s", err);
        return -1;
    }

    return 0;
}
コード例 #6
0
ファイル: ext2.c プロジェクト: will-Do/libguestfs
int
do_mke2fs (const char *device,               /* 0 */
           int64_t blockscount,
           int64_t blocksize,
           int64_t fragsize,
           int64_t blockspergroup,
           int64_t numberofgroups,           /* 5 */
           int64_t bytesperinode,
           int64_t inodesize,
           int64_t journalsize,
           int64_t numberofinodes,
           int64_t stridesize,               /* 10 */
           int64_t stripewidth,
           int64_t maxonlineresize,
           int reservedblockspercentage,
           int mmpupdateinterval,
           const char *journaldevice,        /* 15 */
           const char *label,
           const char *lastmounteddir,
           const char *creatoros,
           const char *fstype,
           const char *usagetype,            /* 20 */
           const char *uuid,
           int forcecreate,
           int writesbandgrouponly,
           int lazyitableinit,
           int lazyjournalinit,              /* 25 */
           int testfs,
           int discard,
           int quotatype,
           int extent,
           int filetype,                     /* 30 */
           int flexbg,
           int hasjournal,
           int journaldev,
           int largefile,
           int quota,                        /* 35 */
           int resizeinode,
           int sparsesuper,
           int uninitbg)
{
    int r;
    CLEANUP_FREE char *err = NULL;
    const char *argv[MAX_ARGS];
    char blockscount_s[64];
    char blocksize_s[64];
    char fragsize_s[64];
    char blockspergroup_s[64];
    char numberofgroups_s[64];
    char bytesperinode_s[64];
    char inodesize_s[64];
    char journalsize_s[64];
    CLEANUP_FREE char *journaldevice_translated = NULL;
    CLEANUP_FREE char *journaldevice_s = NULL;
    char reservedblockspercentage_s[64];
    char numberofinodes_s[64];
    char mmpupdateinterval_s[84];
    char stridesize_s[74];
    char stripewidth_s[84];
    char maxonlineresize_s[74];
    size_t i = 0;

    ADD_ARG (argv, i, str_mke2fs);

    if (optargs_bitmask & GUESTFS_MKE2FS_BLOCKSIZE_BITMASK) {
        if (blocksize < 0) {
            reply_with_error ("blocksize must be >= 0");
            return -1;
        }
        snprintf (blocksize_s, sizeof blocksize_s, "%" PRIi64, blocksize);
        ADD_ARG (argv, i, "-b");
        ADD_ARG (argv, i, blocksize_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_FRAGSIZE_BITMASK) {
        if (fragsize < 0) {
            reply_with_error ("fragsize must be >= 0");
            return -1;
        }
        snprintf (fragsize_s, sizeof fragsize_s, "%" PRIi64, fragsize);
        ADD_ARG (argv, i, "-f");
        ADD_ARG (argv, i, fragsize_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_FORCECREATE_BITMASK) {
        if (forcecreate)
            ADD_ARG (argv, i, "-F");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_BLOCKSPERGROUP_BITMASK) {
        if (blockspergroup < 0) {
            reply_with_error ("blockspergroup must be >= 0");
            return -1;
        }
        snprintf (blockspergroup_s, sizeof blockspergroup_s,
                  "%" PRIi64, blockspergroup);
        ADD_ARG (argv, i, "-g");
        ADD_ARG (argv, i, blockspergroup_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_NUMBEROFGROUPS_BITMASK) {
        if (numberofgroups < 0) {
            reply_with_error ("numberofgroups must be >= 0");
            return -1;
        }
        snprintf (numberofgroups_s, sizeof numberofgroups_s,
                  "%" PRIi64, numberofgroups);
        ADD_ARG (argv, i, "-G");
        ADD_ARG (argv, i, numberofgroups_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_BYTESPERINODE_BITMASK) {
        if (bytesperinode < 0) {
            reply_with_error ("bytesperinode must be >= 0");
            return -1;
        }
        snprintf (bytesperinode_s, sizeof bytesperinode_s, "%" PRIi64, bytesperinode);
        ADD_ARG (argv, i, "-i");
        ADD_ARG (argv, i, bytesperinode_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_INODESIZE_BITMASK) {
        if (inodesize < 0) {
            reply_with_error ("inodesize must be >= 0");
            return -1;
        }
        snprintf (inodesize_s, sizeof inodesize_s, "%" PRIi64, inodesize);
        ADD_ARG (argv, i, "-I");
        ADD_ARG (argv, i, inodesize_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_JOURNALSIZE_BITMASK) {
        if (journalsize < 0) {
            reply_with_error ("journalsize must be >= 0");
            return -1;
        }
        snprintf (journalsize_s, sizeof journalsize_s,
                  "size=" "%" PRIi64, journalsize);
        ADD_ARG (argv, i, "-J");
        ADD_ARG (argv, i, journalsize_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_JOURNALDEVICE_BITMASK) {
        if (journaldevice) {
            /* OString doesn't do device name translation (RHBZ#876579).  We
             * have to do it manually here, but note that LABEL=.. and
             * UUID=.. are valid strings which do not require translation.
             */
            if (STRPREFIX (journaldevice, "/dev/")) {
                if (is_root_device (journaldevice)) {
                    reply_with_error ("%s: device not found", journaldevice);
                    return -1;
                }
                journaldevice_translated = device_name_translation (journaldevice);
                if (journaldevice_translated == NULL) {
                    reply_with_perror ("%s", journaldevice);
                    return -1;
                }

                journaldevice_s = malloc (strlen (journaldevice_translated) + 8);
                if (!journaldevice_s) {
                    reply_with_perror ("malloc");
                    return -1;
                }

                sprintf (journaldevice_s, "device=%s", journaldevice_translated);
            }
            else {
                journaldevice_s = malloc (strlen (journaldevice) + 8);
                if (!journaldevice_s) {
                    reply_with_perror ("malloc");
                    return -1;
                }

                sprintf (journaldevice_s, "device=%s", journaldevice);
            }

            ADD_ARG (argv, i, "-J");
            ADD_ARG (argv, i, journaldevice_s);
        }
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_LABEL_BITMASK) {
        if (label) {
            ADD_ARG (argv, i, "-L");
            ADD_ARG (argv, i, label);
        }
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_RESERVEDBLOCKSPERCENTAGE_BITMASK) {
        if (reservedblockspercentage < 0) {
            reply_with_error ("reservedblockspercentage must be >= 0");
            return -1;
        }
        snprintf (reservedblockspercentage_s, sizeof reservedblockspercentage_s,
                  "%" PRIi32, reservedblockspercentage);
        ADD_ARG (argv, i, "-m");
        ADD_ARG (argv, i, reservedblockspercentage_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_LASTMOUNTEDDIR_BITMASK) {
        if (lastmounteddir) {
            ADD_ARG (argv, i, "-M");
            ADD_ARG (argv, i, lastmounteddir);
        }
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_NUMBEROFINODES_BITMASK) {
        if (numberofinodes < 0) {
            reply_with_error ("numberofinodes must be >= 0");
            return -1;
        }
        snprintf (numberofinodes_s, sizeof numberofinodes_s,
                  "%" PRIi64, numberofinodes);
        ADD_ARG (argv, i, "-N");
        ADD_ARG (argv, i, numberofinodes_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_CREATOROS_BITMASK) {
        if (creatoros) {
            ADD_ARG (argv, i, "-o");
            ADD_ARG (argv, i, creatoros);
        }
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_WRITESBANDGROUPONLY_BITMASK) {
        if (writesbandgrouponly)
            ADD_ARG (argv, i, "-S");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_FSTYPE_BITMASK) {
        if (fstype) {
            if (!fstype_is_extfs (fstype)) {
                reply_with_error ("%s: not a valid extended filesystem type", fstype);
                return -1;
            }

            ADD_ARG (argv, i, "-t");
            ADD_ARG (argv, i, fstype);
        }
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_USAGETYPE_BITMASK) {
        if (usagetype) {
            ADD_ARG (argv, i, "-T");
            ADD_ARG (argv, i, usagetype);
        }
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_UUID_BITMASK) {
        if (uuid) {
            ADD_ARG (argv, i, "-U");
            ADD_ARG (argv, i, uuid);
        }
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_MMPUPDATEINTERVAL_BITMASK) {
        if (mmpupdateinterval < 0) {
            reply_with_error ("mmpupdateinterval must be >= 0");
            return -1;
        }
        snprintf (mmpupdateinterval_s, sizeof mmpupdateinterval_s,
                  "mmp_update_interval=" "%" PRIi32, mmpupdateinterval);
        ADD_ARG (argv, i, "-E");
        ADD_ARG (argv, i, mmpupdateinterval_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_STRIDESIZE_BITMASK) {
        if (stridesize < 0) {
            reply_with_error ("stridesize must be >= 0");
            return -1;
        }
        snprintf (stridesize_s, sizeof stridesize_s,
                  "stride=" "%" PRIi64, stridesize);
        ADD_ARG (argv, i, "-E");
        ADD_ARG (argv, i, stridesize_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_STRIPEWIDTH_BITMASK) {
        if (stripewidth< 0) {
            reply_with_error ("stripewidth must be >= 0");
            return -1;
        }
        snprintf (stripewidth_s, sizeof stripewidth_s,
                  "stripe_width=" "%" PRIi64, stripewidth);
        ADD_ARG (argv, i, "-E");
        ADD_ARG (argv, i, stripewidth_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_MAXONLINERESIZE_BITMASK) {
        if (maxonlineresize < 0) {
            reply_with_error ("maxonlineresize must be >= 0");
            return -1;
        }
        snprintf (maxonlineresize_s, sizeof maxonlineresize_s,
                  "resize=" "%" PRIi64, maxonlineresize);
        ADD_ARG (argv, i, "-E");
        ADD_ARG (argv, i, maxonlineresize_s);
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_LAZYITABLEINIT_BITMASK) {
        ADD_ARG (argv, i, "-E");
        if (lazyitableinit)
            ADD_ARG (argv, i, "lazy_itable_init=1");
        else
            ADD_ARG (argv, i, "lazy_itable_init=0");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_LAZYJOURNALINIT_BITMASK) {
        ADD_ARG (argv, i, "-E");
        if (lazyjournalinit)
            ADD_ARG (argv, i, "lazy_journal_init=1");
        else
            ADD_ARG (argv, i, "lazy_journal_init=0");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_TESTFS_BITMASK) {
        if (testfs) {
            ADD_ARG (argv, i, "-E");
            ADD_ARG (argv, i, "test_fs");
        }
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_DISCARD_BITMASK) {
        ADD_ARG (argv, i, "-E");
        if (discard)
            ADD_ARG (argv, i, "discard");
        else
            ADD_ARG (argv, i, "nodiscard");
    }

    if (optargs_bitmask & GUESTFS_MKE2FS_EXTENT_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (extent)
            ADD_ARG (argv, i, "extent");
        else
            ADD_ARG (argv, i, "^extent");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_FILETYPE_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (filetype)
            ADD_ARG (argv, i, "filetype");
        else
            ADD_ARG (argv, i, "^filetype");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_FLEXBG_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (flexbg)
            ADD_ARG (argv, i, "flexbg");
        else
            ADD_ARG (argv, i, "^flexbg");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_HASJOURNAL_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (hasjournal)
            ADD_ARG (argv, i, "has_journal");
        else
            ADD_ARG (argv, i, "^has_journal");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_JOURNALDEV_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (journaldev)
            ADD_ARG (argv, i, "journal_dev");
        else
            ADD_ARG (argv, i, "^journal_dev");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_LARGEFILE_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (largefile)
            ADD_ARG (argv, i, "large_file");
        else
            ADD_ARG (argv, i, "^large_file");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_QUOTA_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (quota)
            ADD_ARG (argv, i, "quota");
        else
            ADD_ARG (argv, i, "^quota");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_RESIZEINODE_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (resizeinode)
            ADD_ARG (argv, i, "resize_inode");
        else
            ADD_ARG (argv, i, "^resize_inode");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_SPARSESUPER_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (sparsesuper)
            ADD_ARG (argv, i, "sparse_super");
        else
            ADD_ARG (argv, i, "^sparse_super");
    }
    if (optargs_bitmask & GUESTFS_MKE2FS_UNINITBG_BITMASK) {
        ADD_ARG (argv, i, "-O");
        if (uninitbg)
            ADD_ARG (argv, i, "uninit_bg");
        else
            ADD_ARG (argv, i, "^uninit_bg");
    }

    ADD_ARG (argv, i, device);

    if (optargs_bitmask & GUESTFS_MKE2FS_BLOCKSCOUNT_BITMASK) {
        if (blockscount < 0) {
            reply_with_error ("blockscount must be >= 0");
            return -1;
        }
        snprintf (blockscount_s, sizeof blockscount_s, "%" PRIi64, blockscount);
        ADD_ARG (argv, i, blockscount_s);
    }

    ADD_ARG (argv, i, NULL);

    wipe_device_before_mkfs (device);

    r = commandv (NULL, &err, argv);
    if (r == -1) {
        reply_with_error ("%s: %s", device, err);
        return -1;
    }

    return 0;
}
コード例 #7
0
ファイル: mkfs.c プロジェクト: AlphaStaxLLC/libguestfs
/* Takes optional arguments, consult optargs_bitmask. */
int
do_mkfs (const char *fstype, const char *device, int blocksize,
         const char *features, int inode, int sectorsize, const char *label)
{
  const char *argv[MAX_ARGS];
  size_t i = 0;
  char blocksize_str[32];
  char inode_str[32];
  char sectorsize_str[32];
  int r;
  CLEANUP_FREE char *err = NULL;
  int extfs = 0;

  if (fstype_is_extfs (fstype))
    extfs = 1;

  /* For ext2/3/4 run the mke2fs program directly.  This is because
   * the mkfs program "eats" some options, in particular the -F
   * option.
   */
  if (extfs)
    ADD_ARG (argv, i, str_mke2fs);
  else
    ADD_ARG (argv, i, str_mkfs);

  ADD_ARG (argv, i, "-t");
  ADD_ARG (argv, i, fstype);

  /* Force mke2fs to create a filesystem, even if it thinks it
   * shouldn't (RHBZ#690819).
   */
  if (extfs)
    ADD_ARG (argv, i, "-F");

  /* mkfs.ntfs requires the -Q argument otherwise it writes zeroes to
   * every block and does bad block detection, neither of which are
   * useful behaviour for virtual devices.  Also recent versions need
   * to be forced to create filesystems on non-partitions.
   */
  if (STREQ (fstype, "ntfs")) {
    ADD_ARG (argv, i, "-Q");
    ADD_ARG (argv, i, "-F");
  }

  /* mkfs.reiserfs produces annoying interactive prompts unless you
   * tell it to be quiet.
   * mkfs.jfs is the same
   * mkfs.xfs must force to make xfs filesystem when the device already
   * has a filesystem on it
   */
  if (STREQ (fstype, "reiserfs") || STREQ (fstype, "jfs") ||
      STREQ (fstype, "xfs"))
    ADD_ARG (argv, i, "-f");

  /* For GFS, GFS2, assume a single node. */
  if (STREQ (fstype, "gfs") || STREQ (fstype, "gfs2")) {
    ADD_ARG (argv, i, "-p");
    ADD_ARG (argv, i, "lock_nolock");
    /* The man page says this is default, but it doesn't seem to be: */
    ADD_ARG (argv, i, "-j");
    ADD_ARG (argv, i, "1");
    /* Don't ask questions: */
    ADD_ARG (argv, i, "-O");
  }

  /* Force mkfs.fat to create a whole disk filesystem (RHBZ#1039995). */
  if (STREQ (fstype, "fat") || STREQ (fstype, "vfat") ||
      STREQ (fstype, "msdos"))
    ADD_ARG (argv, i, "-I");

  /* Process blocksize parameter if set. */
  if (optargs_bitmask & GUESTFS_MKFS_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 ss = do_blockdev_getss (device);
      if (ss == -1)
        return -1;

      int sectors_per_cluster = blocksize / ss;
      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, ss, sectors_per_cluster);
        return -1;
      }

      snprintf (blocksize_str, sizeof blocksize_str, "%d", sectors_per_cluster);
      ADD_ARG (argv, i, "-s");
      ADD_ARG (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);
      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, use 'mkfs-btrfs'");
      return -1;
    }
    else if (STREQ (fstype, "xfs")) {
      /* mkfs -t xfs -b size=<size> (RHBZ#981715). */
      snprintf (blocksize_str, sizeof blocksize_str, "size=%d", blocksize);
      ADD_ARG (argv, i, "-b");
      ADD_ARG (argv, i, blocksize_str);
    }
    else {
      /* For all other filesystem types, try the -b option. */
      snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
      ADD_ARG (argv, i, "-b");
      ADD_ARG (argv, i, blocksize_str);
    }
  }

  if (optargs_bitmask & GUESTFS_MKFS_FEATURES_BITMASK) {
    ADD_ARG (argv, i, "-O");
    ADD_ARG (argv, i, features);
  }

  if (optargs_bitmask & GUESTFS_MKFS_INODE_BITMASK) {
    if (!extfs) {
      reply_with_error ("inode size (-I) can only be set on ext2/3/4 filesystems");
      return -1;
    }

    if (inode <= 0) {
      reply_with_error ("inode size must be larger than zero");
      return -1;
    }

    snprintf (inode_str, sizeof inode_str, "%d", inode);
    ADD_ARG (argv, i, "-I");
    ADD_ARG (argv, i, inode_str);
  }

  if (optargs_bitmask & GUESTFS_MKFS_SECTORSIZE_BITMASK) {
    if (!STREQ (fstype, "ufs")) {
      reply_with_error ("sector size (-S) can only be set on ufs filesystems");
      return -1;
    }

    if (sectorsize <= 0) {
      reply_with_error ("sector size must be larger than zero");
      return -1;
    }

    snprintf (sectorsize_str, sizeof sectorsize_str, "%d", sectorsize);
    ADD_ARG (argv, i, "-S");
    ADD_ARG (argv, i, sectorsize_str);
  }

  if (optargs_bitmask & GUESTFS_MKFS_LABEL_BITMASK) {
    if (extfs) {
      if (strlen (label) > EXT2_LABEL_MAX) {
        reply_with_error ("%s: ext2/3/4 labels are limited to %d bytes",
                          label, EXT2_LABEL_MAX);
        return -1;
      }

      ADD_ARG (argv, i, "-L");
      ADD_ARG (argv, i, label);
    }
    else if (STREQ (fstype, "fat") || STREQ (fstype, "vfat") ||
             STREQ (fstype, "msdos")) {
      ADD_ARG (argv, i, "-n");
      ADD_ARG (argv, i, label);
    }
    else if (STREQ (fstype, "ntfs")) {
      ADD_ARG (argv, i, "-L");
      ADD_ARG (argv, i, label);
    }
    else if (STREQ (fstype, "xfs")) {
      if (strlen (label) > XFS_LABEL_MAX) {
        reply_with_error ("%s: xfs labels are limited to %d bytes",
                          label, XFS_LABEL_MAX);
        return -1;
      }

      ADD_ARG (argv, i, "-L");
      ADD_ARG (argv, i, label);
    }
    else if (STREQ (fstype, "btrfs")) {
      ADD_ARG (argv, i, "-L");
      ADD_ARG (argv, i, label);
    }
    else {
      reply_with_error ("don't know how to set the label for '%s' filesystems",
                        fstype);
      return -1;
    }
  }

  ADD_ARG (argv, i, device);
  ADD_ARG (argv, i, NULL);

  wipe_device_before_mkfs (device);

  r = commandv (NULL, &err, argv);
  if (r == -1) {
    reply_with_error ("%s: %s: %s", fstype, device, err);
    return -1;
  }

  return 0;
}