virsh: cmdAttachDisk: Don't assume type='block' if file can't be examined

'virsh attach-disk' uses stat() to determine if the 'source' is a
regular file. If stat fails though it assumes that the file is block.

Since it's way more common to have regular files and the detection does
not work at all when accessing a remote host, modify the default to
assume type='file' by default.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Peter Krempa 2020-11-19 11:01:22 +01:00
parent 426369ebfa
commit 5c9fc43e69
2 changed files with 19 additions and 15 deletions

View File

@ -10,8 +10,8 @@ error: command 'attach-disk' requires <target> option
error: command 'attach-disk' requires <target> option error: command 'attach-disk' requires <target> option
+ attach_disk --target hda + attach_disk --target hda
+ ./virsh attach-disk --print-xml --domain testdom --target hda --source /nonexistent/file + ./virsh attach-disk --print-xml --domain testdom --target hda --source /nonexistent/file
<disk type='block'> <disk type='file'>
<source dev='/nonexistent/file'/> <source file='/nonexistent/file'/>
<target dev='hda'/> <target dev='hda'/>
</disk> </disk>

View File

@ -577,7 +577,7 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
const char *targetbus = NULL; const char *targetbus = NULL;
const char *alias = NULL; const char *alias = NULL;
struct DiskAddress diskAddr; struct DiskAddress diskAddr;
bool isFile = false; bool isBlock = false;
int ret; int ret;
unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT; unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
const char *stype = NULL; const char *stype = NULL;
@ -617,15 +617,15 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
return false; return false;
if (!stype) { if (!stype) {
if (driver && (STREQ(driver, "file") || STREQ(driver, "tap"))) { if (STRNEQ_NULLABLE(driver, "file") &&
isFile = true; STRNEQ_NULLABLE(driver, "tap") &&
} else { source &&
if (source && !stat(source, &st)) stat(source, &st) == 0 &&
isFile = S_ISREG(st.st_mode) ? true : false; S_ISBLK(st.st_mode))
} isBlock = true;
} else if (STREQ(stype, "file")) { } else if (STREQ(stype, "block")) {
isFile = true; isBlock = true;
} else if (STRNEQ(stype, "block")) { } else if (STRNEQ(stype, "file")) {
vshError(ctl, _("Unknown source type: '%s'"), stype); vshError(ctl, _("Unknown source type: '%s'"), stype);
return false; return false;
} }
@ -642,8 +642,12 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
return false; return false;
/* Make XML of disk */ /* Make XML of disk */
virBufferAsprintf(&buf, "<disk type='%s'", virBufferAddLit(&buf, "<disk");
isFile ? "file" : "block"); if (isBlock)
virBufferAddLit(&buf, " type='block'");
else
virBufferAddLit(&buf, " type='file'");
if (type) if (type)
virBufferAsprintf(&buf, " device='%s'", type); virBufferAsprintf(&buf, " device='%s'", type);
if (vshCommandOptBool(cmd, "rawio")) if (vshCommandOptBool(cmd, "rawio"))
@ -670,7 +674,7 @@ cmdAttachDisk(vshControl *ctl, const vshCmd *cmd)
if (source) if (source)
virBufferAsprintf(&buf, "<source %s='%s'/>\n", virBufferAsprintf(&buf, "<source %s='%s'/>\n",
isFile ? "file" : "dev", source); !isBlock ? "file" : "dev", source);
virBufferAsprintf(&buf, "<target dev='%s'", target); virBufferAsprintf(&buf, "<target dev='%s'", target);
if (targetbus) if (targetbus)
virBufferAsprintf(&buf, " bus='%s'", targetbus); virBufferAsprintf(&buf, " bus='%s'", targetbus);