From: Richard W.M. Jones Date: Wed, 9 Nov 2011 17:54:38 +0000 (+0000) Subject: New API: Bind the tune2fs command. X-Git-Tag: 1.15.4~12 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=c81a16a4e2e703d0aaa595151602bcf03430f6ab;p=libguestfs.git New API: Bind the tune2fs command. Previously we bound the 'tune2fs -l' command so that we could list out the tunables of an ext2/3/4 filesystem. Also commands like set_e2label and set_e2uuid used tune2fs. This commit binds many of the tunables that can be set using tune2fs. The coverage is not complete, but we can add more later because this uses optional parameters so the call is extensible without breaking ABI. The current change gives us enough for using libguestfs within OpenStack. --- diff --git a/daemon/ext2.c b/daemon/ext2.c index f7889da..79fd354 100644 --- a/daemon/ext2.c +++ b/daemon/ext2.c @@ -32,6 +32,8 @@ /* Confirmed this is true up to ext4 from the Linux sources. */ #define EXT2_LABEL_MAX 16 +#define MAX_ARGS 64 + /* Choose which tools like mke2fs to use. For RHEL 5 (only) there * is a special set of tools which support ext2/3/4. eg. On RHEL 5, * mke2fs only supports ext2/3, but mke4fs supports ext2/3/4. @@ -498,3 +500,150 @@ do_mke2fs_JU (const char *fstype, int blocksize, const char *device, free (err); return 0; } + +/* Takes optional arguments, consult optargs_bitmask. */ +int +do_tune2fs (const char *device, /* only required parameter */ + int force, + int maxmountcount, + int mountcount, + const char *errorbehavior, + int64_t group, + int intervalbetweenchecks, + int reservedblockspercentage, + const char *lastmounteddirectory, + int64_t reservedblockscount, + int64_t user) +{ + const char *argv[MAX_ARGS]; + size_t i = 0; + int r; + char *err; + char prog[] = "tune2fs"; + char maxmountcount_s[64]; + char mountcount_s[64]; + char group_s[64]; + char intervalbetweenchecks_s[64]; + char reservedblockspercentage_s[64]; + char reservedblockscount_s[64]; + char user_s[64]; + + if (e2prog (prog) == -1) + return -1; + + ADD_ARG (argv, i, prog); + + if (optargs_bitmask & GUESTFS_TUNE2FS_FORCE_BITMASK) { + if (force) + ADD_ARG (argv, i, "-f"); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_MAXMOUNTCOUNT_BITMASK) { + if (maxmountcount < 0) { + reply_with_error ("maxmountcount cannot be negative"); + return -1; + } + ADD_ARG (argv, i, "-c"); + snprintf (maxmountcount_s, sizeof maxmountcount_s, "%d", maxmountcount); + ADD_ARG (argv, i, maxmountcount_s); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_MOUNTCOUNT_BITMASK) { + if (mountcount < 0) { + reply_with_error ("mountcount cannot be negative"); + return -1; + } + ADD_ARG (argv, i, "-C"); + snprintf (mountcount_s, sizeof mountcount_s, "%d", mountcount); + ADD_ARG (argv, i, mountcount_s); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_ERRORBEHAVIOR_BITMASK) { + if (STRNEQ (errorbehavior, "continue") && + STRNEQ (errorbehavior, "remount-ro") && + STRNEQ (errorbehavior, "panic")) { + reply_with_error ("invalid errorbehavior parameter: %s", errorbehavior); + return -1; + } + ADD_ARG (argv, i, "-e"); + ADD_ARG (argv, i, errorbehavior); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_GROUP_BITMASK) { + if (group < 0) { + reply_with_error ("group cannot be negative"); + return -1; + } + ADD_ARG (argv, i, "-g"); + snprintf (group_s, sizeof group_s, "%" PRIi64, group); + ADD_ARG (argv, i, group_s); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_INTERVALBETWEENCHECKS_BITMASK) { + if (intervalbetweenchecks < 0) { + reply_with_error ("intervalbetweenchecks cannot be negative"); + return -1; + } + ADD_ARG (argv, i, "-i"); + if (intervalbetweenchecks > 0) { + /* -i s is not documented in the man page, but has been + * supported in tune2fs for several years. + */ + snprintf (intervalbetweenchecks_s, sizeof intervalbetweenchecks_s, + "%ds", intervalbetweenchecks); + ADD_ARG (argv, i, intervalbetweenchecks_s); + } + else + ADD_ARG (argv, i, "0"); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_RESERVEDBLOCKSPERCENTAGE_BITMASK) { + if (reservedblockspercentage < 0) { + reply_with_error ("reservedblockspercentage cannot be negative"); + return -1; + } + ADD_ARG (argv, i, "-m"); + snprintf (reservedblockspercentage_s, sizeof reservedblockspercentage_s, + "%d", reservedblockspercentage); + ADD_ARG (argv, i, reservedblockspercentage_s); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_LASTMOUNTEDDIRECTORY_BITMASK) { + ADD_ARG (argv, i, "-M"); + ADD_ARG (argv, i, lastmounteddirectory); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_RESERVEDBLOCKSCOUNT_BITMASK) { + if (reservedblockscount < 0) { + reply_with_error ("reservedblockscount cannot be negative"); + return -1; + } + ADD_ARG (argv, i, "-r"); + snprintf (reservedblockscount_s, sizeof reservedblockscount_s, + "%" PRIi64, reservedblockscount); + ADD_ARG (argv, i, reservedblockscount_s); + } + + if (optargs_bitmask & GUESTFS_TUNE2FS_USER_BITMASK) { + if (user < 0) { + reply_with_error ("user cannot be negative"); + return -1; + } + ADD_ARG (argv, i, "-u"); + snprintf (user_s, sizeof user_s, "%" PRIi64, user); + ADD_ARG (argv, i, user_s); + } + + ADD_ARG (argv, i, device); + ADD_ARG (argv, i, NULL); + + r = commandv (NULL, &err, argv); + if (r == -1) { + reply_with_error ("%s: %s: %s", prog, device, err); + free (err); + return -1; + } + + free (err); + return 0; +} diff --git a/generator/generator_actions.ml b/generator/generator_actions.ml index 243ea9b..fe40fbf 100644 --- a/generator/generator_actions.ml +++ b/generator/generator_actions.ml @@ -6335,6 +6335,103 @@ is for copying blocks within existing files. See C, C and C for general file copying and moving functions."); + ("tune2fs", (RErr, [Device "device"], [Bool "force"; Int "maxmountcount"; Int "mountcount"; String "errorbehavior"; Int64 "group"; Int "intervalbetweenchecks"; Int "reservedblockspercentage"; String "lastmounteddirectory"; Int64 "reservedblockscount"; Int64 "user"]), 298, [], + [InitScratchFS, Always, TestOutputHashtable ( + [["tune2fs"; "/dev/sdb1"; "false"; "0"; ""; "NOARG"; ""; "0"; ""; "NOARG"; ""; ""]; + ["tune2fs_l"; "/dev/sdb1"]], + ["Check interval", "0 ()"; + "Maximum mount count", "-1"]); + InitScratchFS, Always, TestOutputHashtable ( + [["tune2fs"; "/dev/sdb1"; "false"; "0"; ""; "NOARG"; ""; "86400"; ""; "NOARG"; ""; ""]; + ["tune2fs_l"; "/dev/sdb1"]], + ["Check interval", "86400 (1 day)"; + "Maximum mount count", "-1"]); + InitScratchFS, Always, TestOutputHashtable ( + [["tune2fs"; "/dev/sdb1"; "false"; ""; ""; "NOARG"; "1"; ""; ""; "NOARG"; ""; "1"]; + ["tune2fs_l"; "/dev/sdb1"]], + ["Reserved blocks uid", "1 (user bin)"; + "Reserved blocks gid", "1 (group bin)"]); + InitScratchFS, Always, TestOutputHashtable ( + [["tune2fs"; "/dev/sdb1"; "false"; ""; ""; "NOARG"; "0"; ""; ""; "NOARG"; ""; "0"]; + ["tune2fs_l"; "/dev/sdb1"]], + ["Reserved blocks uid", "0 (user root)"; + "Reserved blocks gid", "0 (group root)"]) + ], + "adjust ext2/ext3/ext4 filesystem parameters", + "\ +This call allows you to adjust various filesystem parameters of +an ext2/ext3/ext4 filesystem called C. + +The optional parameters are: + +=over 4 + +=item C + +Force tune2fs to complete the operation even in the face of errors. +This is the same as the tune2fs C<-f> option. + +=item C + +Set the number of mounts after which the filesystem is checked +by L. If this is C<0> then the number of mounts is +disregarded. This is the same as the tune2fs C<-c> option. + +=item C + +Set the number of times the filesystem has been mounted. +This is the same as the tune2fs C<-C> option. + +=item C + +Change the behavior of the kernel code when errors are detected. +Possible values currently are: C, C, C. +In practice these options don't really make any difference, +particularly for write errors. + +This is the same as the tune2fs C<-e> option. + +=item C + +Set the group which can use reserved filesystem blocks. +This is the same as the tune2fs C<-g> option except that it +can only be specified as a number. + +=item C + +Adjust the maximal time between two filesystem checks +(in seconds). If the option is passed as C<0> then +time-dependent checking is disabled. + +This is the same as the tune2fs C<-i> option. + +=item C + +Set the percentage of the filesystem which may only be allocated +by privileged processes. +This is the same as the tune2fs C<-m> option. + +=item C + +Set the last mounted directory. +This is the same as the tune2fs C<-M> option. + +=item C +Set the number of reserved filesystem blocks. +This is the same as the tune2fs C<-r> option. + +=item C + +Set the user who can use the reserved filesystem blocks. +This is the same as the tune2fs C<-u> option except that it +can only be specified as a number. + +=back + +To get the current values of filesystem parameters, see +C. For precise details of how tune2fs +works, see the L man page."); + ] let all_functions = non_daemon_functions @ daemon_functions diff --git a/src/MAX_PROC_NR b/src/MAX_PROC_NR index 95de1ee..a1f7f63 100644 --- a/src/MAX_PROC_NR +++ b/src/MAX_PROC_NR @@ -1 +1 @@ -297 +298