#include <unistd.h>
#include <getopt.h>
#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
#include <errno.h>
#include "kerncompat.h"
#include "commands.h"
#include "utils.h"
+#include "help.h"
static const char * const balance_cmd_group_usage[] = {
"btrfs balance <command> [options] <path>",
printf("WARNING:\n\n");
printf("\tFull balance without filters requested. This operation is very\n");
printf("\tintense and takes potentially very long. It is recommended to\n");
- printf("\tuse the balance filters to narrow down the balanced data.\n");
+ printf("\tuse the balance filters to narrow down the scope of balance.\n");
printf("\tUse 'btrfs balance start --full-balance' option to skip this\n");
printf("\twarning. The operation will start in %d seconds.\n", delay);
printf("\tUse Ctrl-C to stop it.\n");
"-v be verbose",
"-f force reducing of metadata integrity",
"--full-balance do not print warning and do not delay start",
+ "--background|--bg",
+ " run the balance as a background process",
NULL
};
&args.meta, NULL };
int force = 0;
int verbose = 0;
+ int background = 0;
unsigned start_flags = 0;
int i;
memset(&args, 0, sizeof(args));
while (1) {
- enum { GETOPT_VAL_FULL_BALANCE = 256 };
+ enum { GETOPT_VAL_FULL_BALANCE = 256,
+ GETOPT_VAL_BACKGROUND = 257 };
static const struct option longopts[] = {
{ "data", optional_argument, NULL, 'd'},
{ "metadata", optional_argument, NULL, 'm' },
{ "verbose", no_argument, NULL, 'v' },
{ "full-balance", no_argument, NULL,
GETOPT_VAL_FULL_BALANCE },
+ { "background", no_argument, NULL,
+ GETOPT_VAL_BACKGROUND },
+ { "bg", no_argument, NULL, GETOPT_VAL_BACKGROUND },
{ NULL, 0, NULL, 0 }
};
case GETOPT_VAL_FULL_BALANCE:
start_flags |= BALANCE_START_NOWARN;
break;
+ case GETOPT_VAL_BACKGROUND:
+ background = 1;
+ break;
default:
usage(cmd_balance_start_usage);
}
args.flags |= BTRFS_BALANCE_FORCE;
if (verbose)
dump_ioctl_balance_args(&args);
+ if (background) {
+ switch (fork()) {
+ case (-1):
+ error("unable to fork to run balance in background");
+ return 1;
+ case (0):
+ setsid();
+ switch(fork()) {
+ case (-1):
+ error(
+ "unable to fork to run balance in background");
+ exit(1);
+ case (0):
+ /*
+ * Read the return value to silence compiler
+ * warning. Change to / should succeed and
+ * we're not in a security-sensitive context.
+ */
+ i = chdir("/");
+ close(0);
+ close(1);
+ close(2);
+ open("/dev/null", O_RDONLY);
+ open("/dev/null", O_WRONLY);
+ open("/dev/null", O_WRONLY);
+ break;
+ default:
+ exit(0);
+ }
+ break;
+ default:
+ exit(0);
+ }
+ }
return do_balance(argv[optind], &args, start_flags);
}