Skip to content

Commit 26e7309

Browse files
author
Andrea Righi
committed
virtme-ng: allow to run long commands in graphic mode
Use the same syntax to run commands also in graphic mode, for example: $ vng -g -- glxgears This will start glxgears in a graphic session, using the same syntax to run regular (non-graphic) commands. Signed-off-by: Andrea Righi <andrea.righi@canonical.com>
1 parent dc14a3a commit 26e7309

File tree

5 files changed

+38
-36
lines changed

5 files changed

+38
-36
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,23 +219,23 @@ Examples
219219

220220
- Run `glxgears` inside a kernel recompiled in the current directory:
221221
```
222-
$ vng -g glxgears
222+
$ vng -g -- glxgears
223223
224224
(virtme-ng is started in graphical mode)
225225
```
226226

227227
- Execute an `awesome` window manager session with kernel
228228
6.2.0-1003-lowlatency (installed in the system):
229229
```
230-
$ vng -r 6.2.0-1003-lowlatency -g awesome
230+
$ vng -r 6.2.0-1003-lowlatency -g -- awesome
231231
232232
(virtme-ng is started in graphical mode)
233233
```
234234

235235
- Run the `steam` snap (tested in Ubuntu) inside a virtme-ng instance using
236236
the 6.2.0-1003-lowlatency kernel:
237237
```
238-
$ vng -r 6.2.0-1003-lowlatency --snaps --net user -g /snap/bin/steam
238+
$ vng -r 6.2.0-1003-lowlatency --snaps --net user -g -- /snap/bin/steam
239239
240240
(virtme-ng is started in graphical mode)
241241
```

virtme/commands/run.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,11 +1007,9 @@ def do_it() -> int:
10071007
)
10081008

10091009
def do_script(shellcmd: str, show_boot_console=False) -> None:
1010-
if args.graphics is not None:
1011-
arg_fail("scripts and --graphics are mutually exclusive")
1012-
1013-
# Turn off default I/O
1014-
qemuargs.extend(arch.qemu_nodisplay_args())
1010+
if args.graphics is None:
1011+
# Turn off default I/O
1012+
qemuargs.extend(arch.qemu_nodisplay_args())
10151013

10161014
# Configure kernel console output
10171015
if show_boot_console:
@@ -1063,13 +1061,6 @@ def do_script(shellcmd: str, show_boot_console=False) -> None:
10631061
# Scripts shouldn't reboot
10641062
qemuargs.extend(["-no-reboot"])
10651063

1066-
# Encode the shell command to base64 to handle special characters (such
1067-
# as quotes, double quotes, etc.).
1068-
shellcmd = b64encode(shellcmd.encode('utf-8')).decode('utf-8')
1069-
1070-
# Ask virtme-init to run the script
1071-
kernelargs.append(f"virtme.exec=`{shellcmd}`")
1072-
10731064
# Nasty issue: QEMU will set O_NONBLOCK on fds 0, 1, and 2.
10741065
# This isn't inherently bad, but it can cause a problem if
10751066
# another process is reading from 1 or writing to 0, which is
@@ -1087,6 +1078,20 @@ def do_script(shellcmd: str, show_boot_console=False) -> None:
10871078
os.dup2(newfd, oldfd)
10881079
os.close(newfd)
10891080

1081+
# Encode the shell command to base64 to handle special characters (such
1082+
# as quotes, double quotes, etc.).
1083+
shellcmd = b64encode(shellcmd.encode('utf-8')).decode('utf-8')
1084+
1085+
if args.graphics is not None:
1086+
kernelargs.append(f"virtme_graphics=1")
1087+
1088+
# Ask virtme-init to run the script
1089+
kernelargs.append(f"virtme.exec=`{shellcmd}`")
1090+
1091+
# Do not break old syntax "-g command"
1092+
if args.graphics is not None and args.script_sh is None:
1093+
args.script_sh = args.graphics
1094+
10901095
if args.script_sh is not None:
10911096
do_script(args.script_sh, show_boot_console=args.show_boot_console)
10921097

@@ -1100,8 +1105,6 @@ def do_script(shellcmd: str, show_boot_console=False) -> None:
11001105
video_args = arch.qemu_display_args()
11011106
if video_args:
11021107
qemuargs.extend(video_args)
1103-
if args.graphics != "":
1104-
kernelargs.append("virtme_graphics=%s" % args.graphics)
11051108

11061109
if args.net:
11071110
qemuargs.extend(["-device", "%s,netdev=n0" % arch.virtio_dev_type("net")])

virtme/guest/virtme-init

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,17 @@ if [[ -n "${user_cmd}" ]]; then
253253
# Decode shell command (base64) and dump it to a script
254254
echo $user_cmd | base64 -d > /tmp/.virtme-script
255255

256-
# Start the script
257-
log 'starting script'
258-
setsid bash /tmp/.virtme-script </dev/virtio-ports/virtme.stdin >/dev/virtio-ports/virtme.stdout 2>/dev/virtio-ports/virtme.stderr
259-
log "script returned $?"
260-
261-
# Hmm. We should expose the return value somehow.
262-
sync
263-
poweroff -f
264-
exit 0
256+
if [[ ! -n "${virtme_graphics}" ]]; then
257+
# Start the script
258+
log 'starting script'
259+
setsid bash /tmp/.virtme-script </dev/virtio-ports/virtme.stdin >/dev/virtio-ports/virtme.stdout 2>/dev/virtio-ports/virtme.stderr
260+
log "script returned $?"
261+
262+
# Hmm. We should expose the return value somehow.
263+
sync
264+
poweroff -f
265+
exit 0
266+
fi
265267
fi
266268

267269
# Figure out what the main console is
@@ -330,7 +332,8 @@ if [[ -n "${virtme_graphics}" ]]; then
330332

331333
# Create a .xinitrc to start the requested graphical application.
332334
xinit_rc=/tmp/.xinitrc
333-
echo -e "${pre_exec_cmd}\nexec ${virtme_graphics}" > ${xinit_rc}
335+
echo -e "${pre_exec_cmd}\nexec /tmp/.virtme-script" > ${xinit_rc}
336+
chmod +x /tmp/.virtme-script
334337
if [[ -n "${virtme_user}" ]]; then
335338
chown ${virtme_user}:${virtme_user} $XDG_RUNTIME_DIR
336339
chown ${virtme_user}:${virtme_user} ${xinit_rc}

virtme_ng/run.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,8 @@ def make_parser():
329329
parser.add_argument(
330330
"--graphics",
331331
"-g",
332-
action="store",
333-
nargs="?",
334-
const="",
335-
metavar="BINARY",
336-
help="Show graphical output instead of using a console. "
337-
+ "An argument can be optionally specified to start a graphical application.",
332+
action="store_true",
333+
help="Show graphical output instead of using a console.",
338334
)
339335

340336
parser.add_argument(
@@ -829,8 +825,8 @@ def _get_virtme_initramfs(self, args):
829825
self.virtme_param["force_initramfs"] = ""
830826

831827
def _get_virtme_graphics(self, args):
832-
if args.graphics is not None:
833-
self.virtme_param["graphics"] = f'--graphics "{args.graphics}"'
828+
if args.graphics:
829+
self.virtme_param["graphics"] = f'--graphics'
834830
else:
835831
self.virtme_param["graphics"] = ""
836832

virtme_ng_init

0 commit comments

Comments
 (0)