390 lines
9.8 KiB
C
390 lines
9.8 KiB
C
|
/*
|
||
|
*/
|
||
|
|
||
|
#include <libcbd.h>
|
||
|
|
||
|
#include <cbdutil.h>
|
||
|
|
||
|
#include <getopt.h>
|
||
|
|
||
|
/*
|
||
|
* Parse argument into an unsigned 64-bit value.
|
||
|
*
|
||
|
* The following suffixes are recognized (case insensitive and
|
||
|
* always base two, as in LVM):
|
||
|
* b bytes
|
||
|
* s sectors
|
||
|
* k kilo (1024)
|
||
|
* m mega (1024^2)
|
||
|
* g giga (1024^3)
|
||
|
* t tera (1024^4)
|
||
|
* p peta (1024^5)
|
||
|
*
|
||
|
* If no suffix is given, bytes are assumed.
|
||
|
*/
|
||
|
static bool
|
||
|
parse_arg(const char* arg, uint64_t* val)
|
||
|
{
|
||
|
unsigned long ulval;
|
||
|
const char* end;
|
||
|
|
||
|
ulval = strtoul(arg, (char**)&end, 0);
|
||
|
if (ulval == ULONG_MAX || end == arg) {
|
||
|
return false;
|
||
|
}
|
||
|
*val = ulval;
|
||
|
switch (*end) {
|
||
|
case '\0':
|
||
|
case 'B':
|
||
|
case 'b':
|
||
|
/* No conversion */
|
||
|
break;
|
||
|
case 'S':
|
||
|
case 's':
|
||
|
*val <<= 9;
|
||
|
break;
|
||
|
case 'K':
|
||
|
case 'k':
|
||
|
*val <<= 10;
|
||
|
break;
|
||
|
case 'M':
|
||
|
case 'm':
|
||
|
*val <<= 20;
|
||
|
break;
|
||
|
case 'G':
|
||
|
case 'g':
|
||
|
*val <<= 30;
|
||
|
break;
|
||
|
case 'T':
|
||
|
case 't':
|
||
|
*val <<= 40;
|
||
|
break;
|
||
|
case 'P':
|
||
|
case 'p':
|
||
|
*val <<= 50;
|
||
|
break;
|
||
|
default:
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
static const char* progname;
|
||
|
|
||
|
static void __attribute__((noreturn))
|
||
|
usage(void)
|
||
|
{
|
||
|
fprintf(stderr, "Usage: %s [-h] [-v] <command> [options] <device>\n"
|
||
|
"\n", progname);
|
||
|
fprintf(stderr, "Global arguments:\n"
|
||
|
" -h --help Show this help message and exit\n"
|
||
|
" -v --verbose Increase verbose level\n"
|
||
|
"\n");
|
||
|
fprintf(stderr, "Commands:\n"
|
||
|
" format [opts] <device> Create (format) a compressed device\n"
|
||
|
" -S --pysical-size Physical size [device size]\n"
|
||
|
" -O --offset Offset in device [0]\n"
|
||
|
" -L --logical-blksize Logical block size\n"
|
||
|
" -c --compress-factor Compression factor [2.0]\n"
|
||
|
" -l --logical-shift Logical block shift [4]\n"
|
||
|
" -s --size Logical size\n"
|
||
|
" Note:\n"
|
||
|
" -c and -s are different ways of specifying the compressed device size.\n"
|
||
|
" Only one may be used, not both.\n"
|
||
|
" -l and -L are different ways of specifying the logical block size.\n"
|
||
|
" Only one may be used, not both.\n"
|
||
|
"\n"
|
||
|
" open [opts] <device> <name> Open an existing compressed device\n"
|
||
|
" create [opts] <device> <name> Alias for open\n"
|
||
|
" close [opts] <name> Close an opened compressed device\n"
|
||
|
" check [opts] <device> Check and repair a compressed device\n"
|
||
|
" -n --assume-no Assume \"no\" no all questions\n"
|
||
|
" -y --assume-yes Assume \"yes\" to all questions\n"
|
||
|
" resize [opts] <device> Resize an existing compressed device\n"
|
||
|
" -S --physical-size New (physical) size [use all]\n"
|
||
|
" stats [opts] <name> Show device statistics\n"
|
||
|
"\n");
|
||
|
exit(1);
|
||
|
}
|
||
|
|
||
|
static int
|
||
|
do_format(int argc, char** argv)
|
||
|
{
|
||
|
static const char short_opts[] = "S:O:L:c:l:s:";
|
||
|
static const struct option long_opts[] = {
|
||
|
{ "physical-size", 1, NULL, 'S' },
|
||
|
{ "offset", 1, NULL, 'O' },
|
||
|
{ "logical-blksize", 1, NULL, 'L' },
|
||
|
{ "compress-factor", 1, NULL, 'c' },
|
||
|
{ "logical-shift", 1, NULL, 'l' },
|
||
|
{ "size", 1, NULL, 's' },
|
||
|
{ NULL, 0, NULL, 0 }
|
||
|
};
|
||
|
char opt;
|
||
|
uint64_t optval;
|
||
|
uint64_t poff = 0;
|
||
|
uint64_t psize = 0;
|
||
|
uint64_t lsize = 0;
|
||
|
uint16_t lshift = 0;
|
||
|
|
||
|
const char* dev;
|
||
|
|
||
|
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
||
|
switch (opt) {
|
||
|
case 'S':
|
||
|
if (!parse_arg(optarg, &optval)) {
|
||
|
error("Failed to parse \"%s\"\n", optarg);
|
||
|
}
|
||
|
psize = optval;
|
||
|
if (psize % PBLK_SIZE) {
|
||
|
error("Size \"%s\" is not a multiple of %u bytes\n", optarg, (uint)PBLK_SIZE);
|
||
|
}
|
||
|
break;
|
||
|
case 'O':
|
||
|
if (!parse_arg(optarg, &optval)) {
|
||
|
error("Failed to parse \"%s\"\n", optarg);
|
||
|
}
|
||
|
poff = optval;
|
||
|
break;
|
||
|
case 'L':
|
||
|
if (!parse_arg(optarg, &optval)) {
|
||
|
error("Failed to parse \"%s\"\n", optarg);
|
||
|
}
|
||
|
if ((optval & (optval-1)) ||
|
||
|
optval < (1 << (PBLK_SHIFT + LBLK_SHIFT_MIN)) ||
|
||
|
optval > (1 << (PBLK_SHIFT + LBLK_SHIFT_MAX))) {
|
||
|
error("Size \"%s\" is not a valid logical block size\n", optarg);
|
||
|
}
|
||
|
lshift = (optval >> PBLK_SHIFT);
|
||
|
break;
|
||
|
case 'c':
|
||
|
error("Implement me!\n");
|
||
|
break;
|
||
|
case 'l':
|
||
|
if (!parse_arg(optarg, &optval)) {
|
||
|
error("Failed to parse \"%s\"\n", optarg);
|
||
|
}
|
||
|
if (optval < LBLK_SHIFT_MIN || optval > LBLK_SHIFT_MAX) {
|
||
|
error("Size \"%s\" is not a valid logical block shift\n", optarg);
|
||
|
}
|
||
|
lshift = optval;
|
||
|
break;
|
||
|
case 's':
|
||
|
if (!parse_arg(optarg, &optval)) {
|
||
|
error("Failed to parse \"%s\"\n", optarg);
|
||
|
}
|
||
|
lsize = optval;
|
||
|
if (lsize % PBLK_SIZE) {
|
||
|
error("Size \"%s\" is not a multiple of %u bytes\n", optarg, (uint)PBLK_SIZE);
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
usage();
|
||
|
}
|
||
|
}
|
||
|
if (argc - optind != 1) {
|
||
|
usage();
|
||
|
}
|
||
|
dev = argv[optind++];
|
||
|
|
||
|
cbd_format(dev, poff, psize, lsize, lshift);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int
|
||
|
do_open(int argc, char** argv)
|
||
|
{
|
||
|
char dev[PATH_MAX];
|
||
|
char name[PATH_MAX];
|
||
|
|
||
|
if (argc != 3) {
|
||
|
usage();
|
||
|
}
|
||
|
strcpy(dev, argv[1]);
|
||
|
strcpy(name, argv[2]);
|
||
|
|
||
|
if (dev[0] != '/') {
|
||
|
sprintf(dev, "/dev/mapper/%s", argv[1]);
|
||
|
}
|
||
|
|
||
|
cbd_open(dev, 0, 0, name);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int
|
||
|
do_close(int argc, char** argv)
|
||
|
{
|
||
|
const char* dev;
|
||
|
|
||
|
if (argc != 2) {
|
||
|
usage();
|
||
|
}
|
||
|
dev = argv[1];
|
||
|
|
||
|
cbd_close(dev);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int
|
||
|
do_check(int argc, char** argv)
|
||
|
{
|
||
|
static const char short_opts[] = "ny";
|
||
|
static const struct option long_opts[] = {
|
||
|
{ "assume-no", 0, NULL, 'n' },
|
||
|
{ "assume-yes", 0, NULL, 'y' },
|
||
|
{ NULL, 0, NULL, 0 }
|
||
|
};
|
||
|
char opt;
|
||
|
tristate_t auto_response = t_none;
|
||
|
|
||
|
const char* dev;
|
||
|
|
||
|
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
||
|
switch (opt) {
|
||
|
case 'y':
|
||
|
if (auto_response == t_false) {
|
||
|
fprintf(stderr, "Cannot specify both -y and -n\n");
|
||
|
usage();
|
||
|
}
|
||
|
auto_response = t_true;
|
||
|
break;
|
||
|
case 'n':
|
||
|
if (auto_response == t_true) {
|
||
|
fprintf(stderr, "Cannot specify both -y and -n\n");
|
||
|
usage();
|
||
|
}
|
||
|
auto_response = t_false;
|
||
|
break;
|
||
|
default:
|
||
|
usage();
|
||
|
}
|
||
|
}
|
||
|
if (argc - optind != 1) {
|
||
|
usage();
|
||
|
}
|
||
|
dev = argv[optind++];
|
||
|
|
||
|
cbd_check(dev, auto_response);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int
|
||
|
do_resize(int argc, char** argv)
|
||
|
{
|
||
|
static const char short_opts[] = "S:";
|
||
|
static const struct option long_opts[] = {
|
||
|
{ "physical-size", 1, NULL, 'S' },
|
||
|
{ NULL, 0, NULL, 0 }
|
||
|
};
|
||
|
char opt;
|
||
|
uint64_t poff = 0;
|
||
|
uint64_t psize = 0;
|
||
|
|
||
|
const char* dev;
|
||
|
|
||
|
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
||
|
switch (opt) {
|
||
|
case 'S':
|
||
|
if (!parse_arg(optarg, &psize)) {
|
||
|
fprintf(stderr, "Failed to parse \"%s\"\n", optarg);
|
||
|
usage();
|
||
|
}
|
||
|
if (psize % PBLK_SIZE) {
|
||
|
fprintf(stderr, "Size \"%s\" is not a multiple of %u bytes\n", optarg, (uint)PBLK_SIZE);
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
usage();
|
||
|
}
|
||
|
}
|
||
|
if (argc - optind != 1) {
|
||
|
usage();
|
||
|
}
|
||
|
dev = argv[optind++];
|
||
|
|
||
|
cbd_resize(dev, poff, psize / PBLK_SHIFT);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int
|
||
|
do_stats(int argc, char** argv)
|
||
|
{
|
||
|
const char* dev;
|
||
|
|
||
|
if (argc != 1) {
|
||
|
usage();
|
||
|
}
|
||
|
dev = argv[0];
|
||
|
|
||
|
cbd_stats(dev);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
struct cmd_dispatch
|
||
|
{
|
||
|
const char* cmd;
|
||
|
int (*func)(int, char**);
|
||
|
};
|
||
|
|
||
|
static struct cmd_dispatch dispatch[] =
|
||
|
{
|
||
|
{ "format", do_format },
|
||
|
{ "open", do_open },
|
||
|
{ "create", do_open },
|
||
|
{ "close", do_close },
|
||
|
{ "check", do_check },
|
||
|
{ "resize", do_resize },
|
||
|
{ "stats", do_stats },
|
||
|
};
|
||
|
|
||
|
int
|
||
|
main(int argc, char** argv)
|
||
|
{
|
||
|
static const char short_opts[] = "v";
|
||
|
static const struct option long_opts[] = {
|
||
|
{ "verbose", 0, NULL, 'v' },
|
||
|
{ NULL, 0, NULL, 0 }
|
||
|
};
|
||
|
char opt;
|
||
|
int verbose_level = 0; /* XXX: make global or ...? */
|
||
|
|
||
|
const char* cmd;
|
||
|
uint n;
|
||
|
|
||
|
progname = argv[0];
|
||
|
|
||
|
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
||
|
switch (opt) {
|
||
|
case 'v':
|
||
|
++verbose_level;
|
||
|
break;
|
||
|
default:
|
||
|
usage();
|
||
|
}
|
||
|
}
|
||
|
if (argc - optind < 1) {
|
||
|
usage();
|
||
|
}
|
||
|
cmd = argv[optind];
|
||
|
|
||
|
argc -= optind;
|
||
|
argv += optind;
|
||
|
optind = 0;
|
||
|
for (n = 0; n < ARRAY_SIZE(dispatch); ++n) {
|
||
|
if (!strcmp(cmd, dispatch[n].cmd)) {
|
||
|
return dispatch[n].func(argc, argv);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fprintf(stderr, "Unrecognized command \"%s\"\n", cmd);
|
||
|
usage();
|
||
|
/* NOTREACHED */
|
||
|
}
|