mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-09-01 14:31:52 -06:00
qemu-img: Eliminate bdrv_new_open() code duplication
Several commands have code to create a BlockDriverState and open a file. The bdrv_new_open() function can be used to perform these steps. This patch converts the qemu-img commands to actually use bdrv_new_open(). Replaced the bdrv_new_open() 'readonly' argument with bdrv_open()-style flags to support generic flags like BDRV_O_NO_BACKING. Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
8a22f02a88
commit
f163d0736c
1 changed files with 10 additions and 73 deletions
83
qemu-img.c
83
qemu-img.c
|
@ -190,12 +190,11 @@ static int read_password(char *buf, int buf_size)
|
||||||
|
|
||||||
static BlockDriverState *bdrv_new_open(const char *filename,
|
static BlockDriverState *bdrv_new_open(const char *filename,
|
||||||
const char *fmt,
|
const char *fmt,
|
||||||
int readonly)
|
int flags)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs;
|
BlockDriverState *bs;
|
||||||
BlockDriver *drv;
|
BlockDriver *drv;
|
||||||
char password[256];
|
char password[256];
|
||||||
int flags = BRDV_O_FLAGS;
|
|
||||||
|
|
||||||
bs = bdrv_new("");
|
bs = bdrv_new("");
|
||||||
if (!bs)
|
if (!bs)
|
||||||
|
@ -207,9 +206,6 @@ static BlockDriverState *bdrv_new_open(const char *filename,
|
||||||
} else {
|
} else {
|
||||||
drv = NULL;
|
drv = NULL;
|
||||||
}
|
}
|
||||||
if (!readonly) {
|
|
||||||
flags |= BDRV_O_RDWR;
|
|
||||||
}
|
|
||||||
if (bdrv_open(bs, filename, flags, drv) < 0) {
|
if (bdrv_open(bs, filename, flags, drv) < 0) {
|
||||||
error("Could not open '%s'", filename);
|
error("Could not open '%s'", filename);
|
||||||
}
|
}
|
||||||
|
@ -349,7 +345,7 @@ static int img_create(int argc, char **argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bs = bdrv_new_open(backing_file->value.s, fmt, 1);
|
bs = bdrv_new_open(backing_file->value.s, fmt, BRDV_O_FLAGS);
|
||||||
bdrv_get_geometry(bs, &size);
|
bdrv_get_geometry(bs, &size);
|
||||||
size *= 512;
|
size *= 512;
|
||||||
bdrv_delete(bs);
|
bdrv_delete(bs);
|
||||||
|
@ -384,7 +380,6 @@ static int img_check(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int c, ret;
|
int c, ret;
|
||||||
const char *filename, *fmt;
|
const char *filename, *fmt;
|
||||||
BlockDriver *drv;
|
|
||||||
BlockDriverState *bs;
|
BlockDriverState *bs;
|
||||||
|
|
||||||
fmt = NULL;
|
fmt = NULL;
|
||||||
|
@ -405,19 +400,7 @@ static int img_check(int argc, char **argv)
|
||||||
help();
|
help();
|
||||||
filename = argv[optind++];
|
filename = argv[optind++];
|
||||||
|
|
||||||
bs = bdrv_new("");
|
bs = bdrv_new_open(filename, fmt, BRDV_O_FLAGS);
|
||||||
if (!bs)
|
|
||||||
error("Not enough memory");
|
|
||||||
if (fmt) {
|
|
||||||
drv = bdrv_find_format(fmt);
|
|
||||||
if (!drv)
|
|
||||||
error("Unknown file format '%s'", fmt);
|
|
||||||
} else {
|
|
||||||
drv = NULL;
|
|
||||||
}
|
|
||||||
if (bdrv_open(bs, filename, BRDV_O_FLAGS, drv) < 0) {
|
|
||||||
error("Could not open '%s'", filename);
|
|
||||||
}
|
|
||||||
ret = bdrv_check(bs);
|
ret = bdrv_check(bs);
|
||||||
switch(ret) {
|
switch(ret) {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -443,7 +426,6 @@ static int img_commit(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int c, ret;
|
int c, ret;
|
||||||
const char *filename, *fmt;
|
const char *filename, *fmt;
|
||||||
BlockDriver *drv;
|
|
||||||
BlockDriverState *bs;
|
BlockDriverState *bs;
|
||||||
|
|
||||||
fmt = NULL;
|
fmt = NULL;
|
||||||
|
@ -464,19 +446,7 @@ static int img_commit(int argc, char **argv)
|
||||||
help();
|
help();
|
||||||
filename = argv[optind++];
|
filename = argv[optind++];
|
||||||
|
|
||||||
bs = bdrv_new("");
|
bs = bdrv_new_open(filename, fmt, BRDV_O_FLAGS | BDRV_O_RDWR);
|
||||||
if (!bs)
|
|
||||||
error("Not enough memory");
|
|
||||||
if (fmt) {
|
|
||||||
drv = bdrv_find_format(fmt);
|
|
||||||
if (!drv)
|
|
||||||
error("Unknown file format '%s'", fmt);
|
|
||||||
} else {
|
|
||||||
drv = NULL;
|
|
||||||
}
|
|
||||||
if (bdrv_open(bs, filename, BRDV_O_FLAGS | BDRV_O_RDWR, drv) < 0) {
|
|
||||||
error("Could not open '%s'", filename);
|
|
||||||
}
|
|
||||||
ret = bdrv_commit(bs);
|
ret = bdrv_commit(bs);
|
||||||
switch(ret) {
|
switch(ret) {
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -633,7 +603,7 @@ static int img_convert(int argc, char **argv)
|
||||||
|
|
||||||
total_sectors = 0;
|
total_sectors = 0;
|
||||||
for (bs_i = 0; bs_i < bs_n; bs_i++) {
|
for (bs_i = 0; bs_i < bs_n; bs_i++) {
|
||||||
bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, 1);
|
bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BRDV_O_FLAGS);
|
||||||
if (!bs[bs_i])
|
if (!bs[bs_i])
|
||||||
error("Could not open '%s'", argv[optind + bs_i]);
|
error("Could not open '%s'", argv[optind + bs_i]);
|
||||||
bdrv_get_geometry(bs[bs_i], &bs_sectors);
|
bdrv_get_geometry(bs[bs_i], &bs_sectors);
|
||||||
|
@ -691,7 +661,7 @@ static int img_convert(int argc, char **argv)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out_bs = bdrv_new_open(out_filename, out_fmt, 0);
|
out_bs = bdrv_new_open(out_filename, out_fmt, BRDV_O_FLAGS | BDRV_O_RDWR);
|
||||||
|
|
||||||
bs_i = 0;
|
bs_i = 0;
|
||||||
bs_offset = 0;
|
bs_offset = 0;
|
||||||
|
@ -889,7 +859,6 @@ static int img_info(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int c;
|
int c;
|
||||||
const char *filename, *fmt;
|
const char *filename, *fmt;
|
||||||
BlockDriver *drv;
|
|
||||||
BlockDriverState *bs;
|
BlockDriverState *bs;
|
||||||
char fmt_name[128], size_buf[128], dsize_buf[128];
|
char fmt_name[128], size_buf[128], dsize_buf[128];
|
||||||
uint64_t total_sectors;
|
uint64_t total_sectors;
|
||||||
|
@ -916,19 +885,7 @@ static int img_info(int argc, char **argv)
|
||||||
help();
|
help();
|
||||||
filename = argv[optind++];
|
filename = argv[optind++];
|
||||||
|
|
||||||
bs = bdrv_new("");
|
bs = bdrv_new_open(filename, fmt, BRDV_O_FLAGS | BDRV_O_NO_BACKING);
|
||||||
if (!bs)
|
|
||||||
error("Not enough memory");
|
|
||||||
if (fmt) {
|
|
||||||
drv = bdrv_find_format(fmt);
|
|
||||||
if (!drv)
|
|
||||||
error("Unknown file format '%s'", fmt);
|
|
||||||
} else {
|
|
||||||
drv = NULL;
|
|
||||||
}
|
|
||||||
if (bdrv_open(bs, filename, BRDV_O_FLAGS | BDRV_O_NO_BACKING, drv) < 0) {
|
|
||||||
error("Could not open '%s'", filename);
|
|
||||||
}
|
|
||||||
bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
|
bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
|
||||||
bdrv_get_geometry(bs, &total_sectors);
|
bdrv_get_geometry(bs, &total_sectors);
|
||||||
get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
|
get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
|
||||||
|
@ -1028,13 +985,7 @@ static int img_snapshot(int argc, char **argv)
|
||||||
filename = argv[optind++];
|
filename = argv[optind++];
|
||||||
|
|
||||||
/* Open the image */
|
/* Open the image */
|
||||||
bs = bdrv_new("");
|
bs = bdrv_new_open(filename, NULL, bdrv_oflags);
|
||||||
if (!bs)
|
|
||||||
error("Not enough memory");
|
|
||||||
|
|
||||||
if (bdrv_open(bs, filename, bdrv_oflags, NULL) < 0) {
|
|
||||||
error("Could not open '%s'", filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Perform the requested action */
|
/* Perform the requested action */
|
||||||
switch(action) {
|
switch(action) {
|
||||||
|
@ -1080,7 +1031,7 @@ static int img_snapshot(int argc, char **argv)
|
||||||
static int img_rebase(int argc, char **argv)
|
static int img_rebase(int argc, char **argv)
|
||||||
{
|
{
|
||||||
BlockDriverState *bs, *bs_old_backing, *bs_new_backing;
|
BlockDriverState *bs, *bs_old_backing, *bs_new_backing;
|
||||||
BlockDriver *drv, *old_backing_drv, *new_backing_drv;
|
BlockDriver *old_backing_drv, *new_backing_drv;
|
||||||
char *filename;
|
char *filename;
|
||||||
const char *fmt, *out_basefmt, *out_baseimg;
|
const char *fmt, *out_basefmt, *out_baseimg;
|
||||||
int c, flags, ret;
|
int c, flags, ret;
|
||||||
|
@ -1124,22 +1075,8 @@ static int img_rebase(int argc, char **argv)
|
||||||
* Ignore the old backing file for unsafe rebase in case we want to correct
|
* Ignore the old backing file for unsafe rebase in case we want to correct
|
||||||
* the reference to a renamed or moved backing file.
|
* the reference to a renamed or moved backing file.
|
||||||
*/
|
*/
|
||||||
bs = bdrv_new("");
|
|
||||||
if (!bs)
|
|
||||||
error("Not enough memory");
|
|
||||||
|
|
||||||
drv = NULL;
|
|
||||||
if (fmt) {
|
|
||||||
drv = bdrv_find_format(fmt);
|
|
||||||
if (drv == NULL) {
|
|
||||||
error("Invalid format name: '%s'", fmt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
flags = BRDV_O_FLAGS | BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
|
flags = BRDV_O_FLAGS | BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
|
||||||
if (bdrv_open(bs, filename, flags, drv) < 0) {
|
bs = bdrv_new_open(filename, fmt, flags);
|
||||||
error("Could not open '%s'", filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Find the right drivers for the backing files */
|
/* Find the right drivers for the backing files */
|
||||||
old_backing_drv = NULL;
|
old_backing_drv = NULL;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue