Break out FS volume build routines to their own functions.

Improves readability, particularly wrt the pending CreateFromXML changes.
This will also help implementing File->Block volume creation in the future,
since some of this code will need to be moved to a backend agnostic file.
This commit is contained in:
Cole Robinson 2009-05-12 20:27:17 +00:00
parent 6852c88fc7
commit be9e5185b4
2 changed files with 196 additions and 161 deletions

View File

@ -1,3 +1,8 @@
Tue May 12 16:25:59 EDT 2009 Cole Robinson <crobinso@redhat.com>
* src/storage_backend_fs.c: Break out FS volume build routines to
their own functions.
Tue May 12 16:16:09 EDT 2009 Cole Robinson <crobinso@redhat.com>
* src/virsh.c: Virsh commands vol-clone and vol-create-from

View File

@ -62,6 +62,8 @@ static int qcowXGetBackingStore(virConnectPtr, char **,
static int vmdk4GetBackingStore(virConnectPtr, char **,
const unsigned char *, size_t);
typedef int (*createFile)(virConnectPtr conn, virStorageVolDefPtr vol);
static int track_allocation_progress = 0;
/* Either 'magic' or 'extension' *must* be provided */
@ -1011,18 +1013,10 @@ virStorageBackendFileSystemVolCreate(virConnectPtr conn,
return 0;
}
/**
* Allocate a new file as a volume. This is either done directly
* for raw/sparse files, or by calling qemu-img/qcow-create for
* special kinds of files
*/
static int
virStorageBackendFileSystemVolBuild(virConnectPtr conn,
virStorageVolDefPtr vol)
{
static int createRaw(virConnectPtr conn,
virStorageVolDefPtr vol) {
int fd;
if (vol->target.format == VIR_STORAGE_VOL_FILE_RAW) {
if ((fd = open(vol->target.path, O_RDWR | O_CREAT | O_EXCL,
vol->target.perms.mode)) < 0) {
virReportSystemError(conn, errno,
@ -1043,7 +1037,6 @@ virStorageBackendFileSystemVolBuild(virConnectPtr conn,
/* Pre-allocate any data if requested */
/* XXX slooooooooooooooooow on non-extents-based file systems */
/* FIXME: Add in progress bars & bg thread if progress bar requested */
if (vol->allocation) {
if (track_allocation_progress) {
unsigned long long remain = vol->allocation;
@ -1081,7 +1074,18 @@ virStorageBackendFileSystemVolBuild(virConnectPtr conn,
}
}
} else if (vol->target.format == VIR_STORAGE_VOL_FILE_DIR) {
if (close(fd) < 0) {
virReportSystemError(conn, errno,
_("cannot close file '%s'"),
vol->target.path);
return -1;
}
return 0;
}
static int createFileDir(virConnectPtr conn,
virStorageVolDefPtr vol) {
if (mkdir(vol->target.path, vol->target.perms.mode) < 0) {
virReportSystemError(conn, errno,
_("cannot create path '%s'"),
@ -1089,14 +1093,12 @@ virStorageBackendFileSystemVolBuild(virConnectPtr conn,
return -1;
}
if ((fd = open(vol->target.path, O_RDWR)) < 0) {
virReportSystemError(conn, errno,
_("cannot read path '%s'"),
vol->target.path);
return -1;
return 0;
}
} else {
#if HAVE_QEMU_IMG
static int createQemuImg(virConnectPtr conn,
virStorageVolDefPtr vol) {
const char *type = virStorageVolFormatFileSystemTypeToString(vol->target.format);
const char *backingType = vol->backingStore.path ?
virStorageVolFormatFileSystemTypeToString(vol->backingStore.format) : NULL;
@ -1143,17 +1145,16 @@ virStorageBackendFileSystemVolBuild(virConnectPtr conn,
return -1;
}
if ((fd = open(vol->target.path, O_RDONLY)) < 0) {
virReportSystemError(conn, errno,
_("cannot read path '%s'"),
vol->target.path);
return -1;
return 0;
}
#elif HAVE_QCOW_CREATE
/*
* Xen removed the fully-functional qemu-img, and replaced it
* with a partially functional qcow-create. Go figure ??!?
*/
static int createQemuCreate(virConnectPtr conn,
virStorageVolDefPtr vol) {
char size[100];
const char *imgargv[4];
@ -1182,12 +1183,31 @@ virStorageBackendFileSystemVolBuild(virConnectPtr conn,
return -1;
}
if ((fd = open(vol->target.path, O_RDONLY)) < 0) {
virReportSystemError(conn, errno,
_("cannot read path '%s'"),
vol->target.path);
return -1;
return 0;
}
#endif /* HAVE_QEMU_IMG, elif HAVE_QCOW_CREATE */
/**
* Allocate a new file as a volume. This is either done directly
* for raw/sparse files, or by calling qemu-img/qcow-create for
* special kinds of files
*/
static int
virStorageBackendFileSystemVolBuild(virConnectPtr conn,
virStorageVolDefPtr vol)
{
int fd;
createFile create_func;
if (vol->target.format == VIR_STORAGE_VOL_FILE_RAW) {
create_func = createRaw;
} else if (vol->target.format == VIR_STORAGE_VOL_FILE_DIR) {
create_func = createFileDir;
} else {
#if HAVE_QEMU_IMG
create_func = createQemuImg;
#elif HAVE_QCOW_CREATE
create_func = createQemuCreate;
#else
virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR,
"%s", _("creation of non-raw images "
@ -1196,6 +1216,16 @@ virStorageBackendFileSystemVolBuild(virConnectPtr conn,
#endif
}
if (create_func(conn, vol) < 0)
return -1;
if ((fd = open(vol->target.path, O_RDONLY)) < 0) {
virReportSystemError(conn, errno,
_("cannot read path '%s'"),
vol->target.path);
return -1;
}
/* We can only chown/grp if root */
if (getuid() == 0) {
if (fchown(fd, vol->target.perms.uid, vol->target.perms.gid) < 0) {