Skip to contentSkip to conversation

qemu-config: Add new -add-fd command line option

PATCH Superseded Corey Bryant 3 participants 9 messages Oct 16, 2012 – Oct 18, 2012

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 4/4 qemu-config: Add new -add-fd command line option

This option can be used for passing file descriptors on the command line. It mirrors the existing add-fd QMP command which allows an fd to be passed to QEMU via SCM_RIGHTS and added to an fd set.

This can be combined with commands such as -drive to link file descriptors in an fd set to a drive:

    qemu-kvm -add-fd fd=3,set=2,opaque="rdwr:/path/to/file"
             -add-fd fd=4,set=2,opaque="rdonly:/path/to/file"
             -drive file=/dev/fdset/2,index=0,media=disk

This example adds dups of fds 4 and 5, and the accompanying opaque strings to the fd set with ID=2. qemu_open() already knows how to handle a filename of this format. qemu_open() searches the corresponding fd set for an fd and when it finds a match, QEMU goes on to use a dup of that fd just like it would have used an fd that it opened itself.

---
v2:
- The -add-fd option is new in v2 ([email protected])

v3:
- Require passed fd to be > stderr ([email protected])
- Changed fds in examples to fd=3 and fd=4
- Add dup of passed fd to fd set and close passed fds after processing all -add-fd commands ([email protected])

 qemu-config.c   | 22 ++++++++++++++++++
 qemu-options.hx | 36 +++++++++++++++++++++++++++++
 vl.c            | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+)
qemu-config.c +22 -0
index cd1ec21..601237d 100644
@@ -653,6 +653,27 @@ QemuOptsList qemu_boot_opts = {
     },
 };
 
+static QemuOptsList qemu_add_fd_opts = {
+    .name = "add-fd",
+    .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
+    .desc = {
+        {
+            .name = "fd",
+            .type = QEMU_OPT_NUMBER,
+            .help = "file descriptor of which a duplicate is added to fd set",
+        },{
+            .name = "set",
+            .type = QEMU_OPT_NUMBER,
+            .help = "ID of the fd set to add fd to",
+        },{
+            .name = "opaque",
+            .type = QEMU_OPT_STRING,
+            .help = "free-form string used to describe fd",
+        },
+        { /* end of list */ }
+    },
+};
+
 static QemuOptsList *vm_config_groups[32] = {
     &qemu_drive_opts,
     &qemu_chardev_opts,
@@ -669,6 +690,7 @@ static QemuOptsList *vm_config_groups[32] = {
     &qemu_boot_opts,
     &qemu_iscsi_opts,
     &qemu_sandbox_opts,
+    &qemu_add_fd_opts,
     NULL,
 };
 
qemu-options.hx +36 -0
index 7d97f96..a70182a 100644
@@ -257,6 +257,14 @@ qemu-system-i386 -drive file=file,index=2,media=disk
 qemu-system-i386 -drive file=file,index=3,media=disk
 @end example
 
+You can open an image using pre-opened file descriptors from an fd set:
+@example
+qemu-system-i386
+-add-fd fd=3,set=2,opaque="rdwr:/path/to/file"
+-add-fd fd=4,set=2,opaque="rdonly:/path/to/file"
+-drive file=/dev/fdset/2,index=0,media=disk
+@end example
+
 You can connect a CDROM to the slave of ide0:
 @example
 qemu-system-i386 -drive file=file,if=ide,index=1,media=cdrom
@@ -289,6 +297,34 @@ qemu-system-i386 -hda a -hdb b
 @end example
 ETEXI
 
+DEF("add-fd", HAS_ARG, QEMU_OPTION_add_fd,
+    "-add-fd fd=fd,set=set[,opaque=opaque]\n"
+    "                Add 'fd' to fd 'set'\n", QEMU_ARCH_ALL)
+STEXI
+@item -add-fd fd=@var{fd},set=@var{set}[,opaque=@var{opaque}]
+@findex -add-fd
+
+Add a file descriptor to an fd set.  Valid options are:
+
+@table @option
+@item fd=@var{fd}
+This option defines the file descriptor of which a duplicate is added to fd set.
+The file descriptor cannot be stdin, stdout, or stderr.
+@item set=@var{set}
+This option defines the ID of the fd set to add the file descriptor to.
+@item opaque=@var{opaque}
+This option defines a free-form string that can be used to describe @var{fd}.
+@end table
+
+You can open an image using pre-opened file descriptors from an fd set:
+@example
+qemu-system-i386
+-add-fd fd=3,set=2,opaque="rdwr:/path/to/file"
+-add-fd fd=4,set=2,opaque="rdonly:/path/to/file"
+-drive file=/dev/fdset/2,index=0,media=disk
+@end example
+ETEXI
+
 DEF("set", HAS_ARG, QEMU_OPTION_set,
     "-set group.id.arg=value\n"
     "                set <arg> parameter for item <id> of type <group>\n"
vl.c +72 -0

90 lines not on this page · raw message

Signature · 1 line
1.7.11.4
#1 lore raw
Quoted 5 lines · Corey Bryant

This option can be used for passing file descriptors on the command line. It mirrors the existing add-fd QMP command which allows an fd to be passed to QEMU via SCM_RIGHTS and added to an fd set.

Quoted 8 lines

This can be combined with commands such as -drive to link file descriptors in an fd set to a drive:

    qemu-kvm -add-fd fd=3,set=2,opaque="rdwr:/path/to/file"
             -add-fd fd=4,set=2,opaque="rdonly:/path/to/file"
             -drive file=/dev/fdset/2,index=0,media=disk

This example adds dups of fds 4 and 5, and the accompanying opaque

s/4 and 5/3 and 4/

Quoted 11 lines

strings to the fd set with ID=2. qemu_open() already knows how to handle a filename of this format. qemu_open() searches the corresponding fd set for an fd and when it finds a match, QEMU goes on to use a dup of that fd just like it would have used an fd that it opened itself.

---
v2:
- The -add-fd option is new in v2 ([email protected])

Quoted 5 lines

v3:
- Require passed fd to be > stderr ([email protected])
- Changed fds in examples to fd=3 and fd=4
- Add dup of passed fd to fd set and close passed fds after processing all -add-fd commands ([email protected])

I'm still seeing the corner case of:

qemu-kvm -add-fd fd=3,set=1 -add-fd fd=4,set=2 4<&-

where the dup(3) will populate fd 4 prior to the point where we get to process the -add-fd fd=4 command to notice that the user started qemu-kvm with fd 4 closed, and thus qemu will silently proceed to use the wrong fd.

On the other hand, I'm not sure if that corner case is worth worrying about, or if we just chalk it up to user stupidity (aka libvirt programmer stupidity) if they did something like that (most likely, because the management app forgot to clear FD_CLOEXEC before exec()ing qemu-kvm).

Hmm, this makes me wonder if I can do something crazy like:

qemu-kvm -add-fd fd=4,set=1 -qmp /dev/fdset/1

to open a monitor on the fd I just passed in? And what if so, what then happens on that monitor if I request that fdset 1 be removed?

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#2 lore raw

Am 17.10.2012 06:16, schrieb Eric Blake:

Quoted 9 lines

I'm still seeing the corner case of:

qemu-kvm -add-fd fd=3,set=1 -add-fd fd=4,set=2 4<&-

where the dup(3) will populate fd 4 prior to the point where we get to process the -add-fd fd=4 command to notice that the user started qemu-kvm with fd 4 closed, and thus qemu will silently proceed to use the wrong fd.

Quoted 5 lines

On the other hand, I'm not sure if that corner case is worth worrying about, or if we just chalk it up to user stupidity (aka libvirt programmer stupidity) if they did something like that (most likely, because the management app forgot to clear FD_CLOEXEC before exec()ing qemu-kvm).

If you specify an FD number that isn't actually open when qemu is stared, you can get any FD that qemu opens internally. I think the correct answer to this problem is "then don't do that".

Quoted 5 lines

Hmm, this makes me wonder if I can do something crazy like:

qemu-kvm -add-fd fd=4,set=1 -qmp /dev/fdset/1

to open a monitor on the fd I just passed in?

I think so. At least on my side it was intended to allow this.

Quoted 2 lines

And what if so, what then
happens on that monitor if I request that fdset 1 be removed?

The same as with block devices: The fd stays open until the monitor connection is closed. A closed monitor also triggers fd garbage collection, so at this point the original fd would be closed (well, assuming that you had only one monitor).

Kevin

#3 lore raw
Quoted 16 lines · Kevin Wolf

Am 17.10.2012 06:16, schrieb Eric Blake:

Quoted 9 lines

I'm still seeing the corner case of:

qemu-kvm -add-fd fd=3,set=1 -add-fd fd=4,set=2 4<&-

where the dup(3) will populate fd 4 prior to the point where we get to process the -add-fd fd=4 command to notice that the user started qemu-kvm with fd 4 closed, and thus qemu will silently proceed to use the wrong fd.

Quoted 5 lines

On the other hand, I'm not sure if that corner case is worth worrying about, or if we just chalk it up to user stupidity (aka libvirt programmer stupidity) if they did something like that (most likely, because the management app forgot to clear FD_CLOEXEC before exec()ing qemu-kvm).

Quoted 3 lines

If you specify an FD number that isn't actually open when qemu is stared, you can get any FD that qemu opens internally. I think the correct answer to this problem is "then don't do that".

Overnight, I realized we do have one potential safety valve: we are guaranteed that any fd inherited by the exec() of qemu-kvm has FD_CLOEXEC clear, and we also strive to have qemu open/dup all of its internal fds with FD_CLOEXEC set. Therefore, it may be worth a sanity check of fcntl(F_GETFD) to see if FD_CLOEXEC is set, and if so, the user must have failed to pass in the fd, and we are now looking at a qemu internal fd, and should therefore report failure. But I'm not sure if it's worth the extra code.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#4 lore raw

Am 17.10.2012 17:01, schrieb Eric Blake:

Quoted 21 lines · Kevin Wolf
Quoted 16 lines

Am 17.10.2012 06:16, schrieb Eric Blake:

Quoted 9 lines

I'm still seeing the corner case of:

qemu-kvm -add-fd fd=3,set=1 -add-fd fd=4,set=2 4<&-

where the dup(3) will populate fd 4 prior to the point where we get to process the -add-fd fd=4 command to notice that the user started qemu-kvm with fd 4 closed, and thus qemu will silently proceed to use the wrong fd.

Quoted 5 lines

On the other hand, I'm not sure if that corner case is worth worrying about, or if we just chalk it up to user stupidity (aka libvirt programmer stupidity) if they did something like that (most likely, because the management app forgot to clear FD_CLOEXEC before exec()ing qemu-kvm).

Quoted 3 lines

If you specify an FD number that isn't actually open when qemu is stared, you can get any FD that qemu opens internally. I think the correct answer to this problem is "then don't do that".

Quoted 8 lines

Overnight, I realized we do have one potential safety valve: we are guaranteed that any fd inherited by the exec() of qemu-kvm has FD_CLOEXEC clear, and we also strive to have qemu open/dup all of its internal fds with FD_CLOEXEC set. Therefore, it may be worth a sanity check of fcntl(F_GETFD) to see if FD_CLOEXEC is set, and if so, the user must have failed to pass in the fd, and we are now looking at a qemu internal fd, and should therefore report failure. But I'm not sure if it's worth the extra code.

Hm, this sounds actually easy enough. I'll leave the decision to Corey, but I like the idea.

Kevin

#8 lore raw
In reply to Eric Blake
Quoted 9 lines · Eric Blake
Quoted 8 lines · Corey Bryant

This option can be used for passing file descriptors on the command line. It mirrors the existing add-fd QMP command which allows an fd to be passed to QEMU via SCM_RIGHTS and added to an fd set.

This can be combined with commands such as -drive to link file descriptors in an fd set to a drive:

Quoted 7 lines
Quoted 5 lines
     qemu-kvm -add-fd fd=3,set=2,opaque="rdwr:/path/to/file"
              -add-fd fd=4,set=2,opaque="rdonly:/path/to/file"
              -drive file=/dev/fdset/2,index=0,media=disk

This example adds dups of fds 4 and 5, and the accompanying opaque

s/4 and 5/3 and 4/

Doh.. thanks.

Signature · 2 lines
Regards,
Corey Bryant
#5 lore raw
In reply to Kevin Wolf
Quoted 31 lines · Kevin Wolf

Am 17.10.2012 17:01, schrieb Eric Blake:

Quoted 21 lines · Kevin Wolf
Quoted 16 lines

Am 17.10.2012 06:16, schrieb Eric Blake:

Quoted 9 lines

I'm still seeing the corner case of:

qemu-kvm -add-fd fd=3,set=1 -add-fd fd=4,set=2 4<&-

where the dup(3) will populate fd 4 prior to the point where we get to process the -add-fd fd=4 command to notice that the user started qemu-kvm with fd 4 closed, and thus qemu will silently proceed to use the wrong fd.

Quoted 5 lines

On the other hand, I'm not sure if that corner case is worth worrying about, or if we just chalk it up to user stupidity (aka libvirt programmer stupidity) if they did something like that (most likely, because the management app forgot to clear FD_CLOEXEC before exec()ing qemu-kvm).

Quoted 3 lines

If you specify an FD number that isn't actually open when qemu is stared, you can get any FD that qemu opens internally. I think the correct answer to this problem is "then don't do that".

Quoted 8 lines

Overnight, I realized we do have one potential safety valve: we are guaranteed that any fd inherited by the exec() of qemu-kvm has FD_CLOEXEC clear, and we also strive to have qemu open/dup all of its internal fds with FD_CLOEXEC set. Therefore, it may be worth a sanity check of fcntl(F_GETFD) to see if FD_CLOEXEC is set, and if so, the user must have failed to pass in the fd, and we are now looking at a qemu internal fd, and should therefore report failure. But I'm not sure if it's worth the extra code.

Quoted 2 lines

Hm, this sounds actually easy enough. I'll leave the decision to Corey, but I like the idea.

Sure I can add this.

Signature · 2 lines
Regards,
Corey Bryant
#6 lore raw
In reply to Kevin Wolf
Quoted 16 lines · Kevin Wolf

Am 17.10.2012 06:16, schrieb Eric Blake:

Quoted 9 lines

I'm still seeing the corner case of:

qemu-kvm -add-fd fd=3,set=1 -add-fd fd=4,set=2 4<&-

where the dup(3) will populate fd 4 prior to the point where we get to process the -add-fd fd=4 command to notice that the user started qemu-kvm with fd 4 closed, and thus qemu will silently proceed to use the wrong fd.

Quoted 5 lines

On the other hand, I'm not sure if that corner case is worth worrying about, or if we just chalk it up to user stupidity (aka libvirt programmer stupidity) if they did something like that (most likely, because the management app forgot to clear FD_CLOEXEC before exec()ing qemu-kvm).

Quoted 4 lines

If you specify an FD number that isn't actually open when qemu is stared, you can get any FD that qemu opens internally. I think the correct answer to this problem is "then don't do that".

I'd also say "then don't do that". Or maybe "why are you doing that?". But I'm not opposed to closing a corner case if it's not cluttering the code base.

Quoted 11 lines
Quoted 5 lines

Hmm, this makes me wonder if I can do something crazy like:

qemu-kvm -add-fd fd=4,set=1 -qmp /dev/fdset/1

to open a monitor on the fd I just passed in?

I think so. At least on my side it was intended to allow this.

Quoted 2 lines

And what if so, what then
happens on that monitor if I request that fdset 1 be removed?

Quoted 7 lines

The same as with block devices: The fd stays open until the monitor connection is closed. A closed monitor also triggers fd garbage collection, so at this point the original fd would be closed (well, assuming that you had only one monitor).

Kevin

True, but I think in this case we care more about the dup'd fd staying open than the fd in the fdset. Remember that qemu_open() dups the fd from the fd set. So assuming the open/close of the QMP fd occurs in qemu_open()/qemu_close(), the QMP fd would be a dup of the fd that was added to the fd set. So if remove-fd removed the fd from the fdset, or it removed the entire fdset, the QMP fd would remain open until qemu_close() was called. I'll try this out today to make sure but I don't think this is an issue.

Signature · 2 lines
Regards,
Corey Bryant
#7 lore raw
Quoted 47 lines · Corey Bryant
Quoted 16 lines · Kevin Wolf

Am 17.10.2012 06:16, schrieb Eric Blake:

Quoted 9 lines

I'm still seeing the corner case of:

qemu-kvm -add-fd fd=3,set=1 -add-fd fd=4,set=2 4<&-

where the dup(3) will populate fd 4 prior to the point where we get to process the -add-fd fd=4 command to notice that the user started qemu-kvm with fd 4 closed, and thus qemu will silently proceed to use the wrong fd.

Quoted 5 lines

On the other hand, I'm not sure if that corner case is worth worrying about, or if we just chalk it up to user stupidity (aka libvirt programmer stupidity) if they did something like that (most likely, because the management app forgot to clear FD_CLOEXEC before exec()ing qemu-kvm).

Quoted 4 lines

If you specify an FD number that isn't actually open when qemu is stared, you can get any FD that qemu opens internally. I think the correct answer to this problem is "then don't do that".

I'd also say "then don't do that". Or maybe "why are you doing that?". But I'm not opposed to closing a corner case if it's not cluttering the code base.

Quoted 11 lines
Quoted 5 lines

Hmm, this makes me wonder if I can do something crazy like:

qemu-kvm -add-fd fd=4,set=1 -qmp /dev/fdset/1

to open a monitor on the fd I just passed in?

I think so. At least on my side it was intended to allow this.

Quoted 2 lines

And what if so, what then
happens on that monitor if I request that fdset 1 be removed?

Quoted 7 lines

The same as with block devices: The fd stays open until the monitor connection is closed. A closed monitor also triggers fd garbage collection, so at this point the original fd would be closed (well, assuming that you had only one monitor).

Kevin

Quoted 9 lines

True, but I think in this case we care more about the dup'd fd staying open than the fd in the fdset. Remember that qemu_open() dups the fd from the fd set. So assuming the open/close of the QMP fd occurs in qemu_open()/qemu_close(), the QMP fd would be a dup of the fd that was added to the fd set. So if remove-fd removed the fd from the fdset, or it removed the entire fdset, the QMP fd would remain open until qemu_close() was called. I'll try this out today to make sure but I don't think this is an issue.

After digging into this some more it appears to be a non issue. Only qemu_open() and qemu_close() deal with fdsets. The QMP fd is created with qemu_socket(), not qemu_open(), so it doesn't deal with fdsets. The ensuing bind() call that specifies the path ends up failing with ENOENT because the actual path "/dev/fdset/1" doesn't exist:

bind(unix:/dev/fdset/1): No such file or directory

Signature · 2 lines
Regards,
Corey Bryant