#include "hammer.h"
void
hammer_cmd_volume_add(char **av, int ac)
{
struct hammer_ioc_volume ioc;
volume_info_t volume;
int fd;
const char *device, *filesystem;
if (ac != 2) {
errx(1, "hammer volume-add <device> <filesystem>");
}
device = av[0];
filesystem = av[1];
fd = open(filesystem, O_RDONLY);
if (fd < 0) {
err(1, "hammer volume-add: unable to access %s", filesystem);
}
volume = init_volume(device, O_RDONLY, -1);
assert(volume->vol_no == -1);
if (is_regfile(volume)) {
errx(1, "Not a block device: %s", device);
}
close(volume->fd);
bzero(&ioc, sizeof(ioc));
strncpy(ioc.device_name, device, sizeof(ioc.device_name) - 1);
ioc.vol_size = volume->size;
ioc.boot_area_size = init_boot_area_size(0, ioc.vol_size);
ioc.memory_log_size = init_memory_log_size(0, ioc.vol_size);
if (ioctl(fd, HAMMERIOC_ADD_VOLUME, &ioc) < 0) {
err(1, "hammer volume-add ioctl");
}
close(fd);
hammer_cmd_volume_list(av + 1, ac - 1);
}
void
hammer_cmd_volume_del(char **av, int ac)
{
struct hammer_ioc_volume ioc;
int fd, retried = 0;
const char *device, *filesystem;
if (ac != 2) {
errx(1, "hammer volume-del <device> <filesystem>");
}
device = av[0];
filesystem = av[1];
fd = open(filesystem, O_RDONLY);
if (fd < 0) {
err(1, "hammer volume-del: unable to access %s", filesystem);
}
bzero(&ioc, sizeof(ioc));
strncpy(ioc.device_name, device, sizeof(ioc.device_name) - 1);
if (ForceOpt)
ioc.flag |= HAMMER_IOC_VOLUME_REBLOCK;
retry:
if (ioctl(fd, HAMMERIOC_DEL_VOLUME, &ioc) < 0) {
if ((errno == ENOTEMPTY) && (retried++ == 0)) {
printf("%s is not empty, ", device);
printf("do you want to reblock %s? [y/n] ", device);
fflush(stdout);
if (getyn() == 1) {
ioc.flag |= HAMMER_IOC_VOLUME_REBLOCK;
goto retry;
}
}
err(1, "hammer volume-del ioctl");
}
close(fd);
hammer_cmd_volume_list(av + 1, ac - 1);
}
void
hammer_cmd_volume_list(char **av, int ac)
{
struct hammer_ioc_volume_list ioc;
char *device_name;
int vol_no, i;
if (ac < 1) {
errx(1, "hammer volume-list <filesystem>");
}
if (hammer_fs_to_vol(av[0], &ioc) == -1) {
errx(1, "hammer volume-list: failed");
}
for (i = 0; i < ioc.nvols; i++) {
device_name = ioc.vols[i].device_name;
vol_no = ioc.vols[i].vol_no;
if (VerboseOpt) {
printf("%d\t%s%s\n", vol_no, device_name,
(vol_no == HAMMER_ROOT_VOLNO ?
" (Root Volume)" : ""));
} else {
printf("%s\n", device_name);
}
}
free(ioc.vols);
}
void
hammer_cmd_volume_blkdevs(char **av, int ac)
{
struct hammer_ioc_volume_list ioc;
int i;
if (ac < 1) {
errx(1, "hammer volume-blkdevs <filesystem>");
}
if (hammer_fs_to_vol(av[0], &ioc) == -1) {
errx(1, "hammer volume-list: failed");
}
for (i = 0; i < ioc.nvols; i++) {
printf("%s", ioc.vols[i].device_name);
if (i != ioc.nvols - 1)
printf(":");
}
printf("\n");
free(ioc.vols);
}