1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (gnu build marionette)
20 #:use-module (srfi srfi-9)
21 #:use-module (srfi srfi-26)
22 #:use-module (rnrs io ports)
23 #:use-module (ice-9 match)
24 #:use-module (ice-9 popen)
30 marionette-screen-text
37 ;;; Instrumentation tools for QEMU virtual machines (VMs). A "marionette" is
38 ;;; essentially a VM (a QEMU instance) with its monitor connected to a
39 ;;; Unix-domain socket, and with a REPL inside the guest listening on a
40 ;;; virtual console, which is itself connected to the host via a Unix-domain
41 ;;; socket--these are the marionette's strings, connecting it to the almighty
46 (define-record-type <marionette>
47 (marionette command pid monitor repl)
49 (command marionette-command) ;list of strings
50 (pid marionette-pid) ;integer
51 (monitor marionette-monitor) ;port
52 (repl %marionette-repl)) ;promise of a port
54 (define-syntax-rule (marionette-repl marionette)
55 (force (%marionette-repl marionette)))
57 (define* (wait-for-monitor-prompt port #:key (quiet? #t))
58 "Read from PORT until we have seen all of QEMU's monitor prompt. When
59 QUIET? is false, the monitor's output is written to the current output port."
61 (string->list "(qemu) "))
63 (let loop ((prompt full-prompt)
68 ;; It's useful to set QUIET? so we don't display the echo of our own
71 (for-each (lambda (line)
72 (format #t "qemu monitor: ~a~%" line))
73 (string-tokenize (list->string (reverse prefix))
74 (char-set-complement (char-set #\newline))))))
76 (let ((read (read-char port)))
77 (cond ((eqv? read chr)
78 (loop rest (cons read matches) prefix))
80 (error "EOF while waiting for QEMU monitor prompt"
81 (list->string (reverse prefix))))
85 (cons read (append matches prefix))))))))))
87 (define* (make-marionette command
88 #:key (socket-directory "/tmp") (timeout 20))
89 "Return a QEMU marionette--i.e., a virtual machine with open connections to the
90 QEMU monitor and to the guest's backdoor REPL."
91 (define (file->sockaddr file)
92 (make-socket-address AF_UNIX
93 (string-append socket-directory "/" file)))
97 "-monitor" (string-append "unix:" socket-directory "/monitor")
98 "-chardev" (string-append "socket,id=repl,path=" socket-directory
102 ;; <http://www.linux-kvm.org/page/VMchannel_Requirements#Invocation>.
103 "-device" "virtio-serial"
104 "-device" "virtserialport,chardev=repl,name=org.gnu.guix.port.0"))
106 (define (accept* port)
107 (match (select (list port) '() (list port) timeout)
111 (error "timeout in 'accept'" port))))
113 (let ((monitor (socket AF_UNIX SOCK_STREAM 0))
114 (repl (socket AF_UNIX SOCK_STREAM 0)))
115 (bind monitor (file->sockaddr "monitor"))
117 (bind repl (file->sockaddr "repl"))
120 (match (primitive-fork)
128 (apply execl program program
129 (append args extra-options)))))
131 (print-exception (current-error-port)
132 (stack-ref (make-stack #t) 1)
134 (primitive-exit 1))))
136 (format #t "QEMU runs as PID ~a~%" pid)
138 (match (accept* monitor)
140 (display "connected to QEMU's monitor\n")
142 (wait-for-monitor-prompt monitor-conn)
143 (display "read QEMU monitor prompt\n")
145 (marionette (append command extra-options) pid
148 ;; The following 'accept' call connects immediately, but
149 ;; we don't know whether the guest has connected until
150 ;; we actually receive the 'ready' message.
151 (match (accept* repl)
153 (display "connected to guest REPL\n")
155 ;; Delay reception of the 'ready' message so that the
156 ;; caller can already send monitor commands.
158 (match (read repl-conn)
160 (display "marionette is ready\n")
161 repl-conn))))))))))))
163 (define (marionette-eval exp marionette)
164 "Evaluate EXP in MARIONETTE's backdoor REPL. Return the result."
166 (($ <marionette> command pid monitor (= force repl))
171 (define* (wait-for-file file marionette
172 #:key (timeout 10) (read 'read))
173 "Wait until FILE exists in MARIONETTE; READ its content and return it. If
174 FILE has not shown up after TIMEOUT seconds, raise an error."
175 (match (marionette-eval
176 `(let loop ((i ,timeout))
177 (cond ((file-exists? ,file)
178 (cons 'success (call-with-input-file ,file ,read)))
188 (error "file didn't show up" file))))
190 (define (marionette-control command marionette)
191 "Run COMMAND in the QEMU monitor of MARIONETTE. COMMAND is a string such as
192 \"sendkey ctrl-alt-f1\" or \"screendump foo.ppm\" (info \"(qemu-doc)
195 (($ <marionette> _ _ monitor)
196 (display command monitor)
198 (wait-for-monitor-prompt monitor))))
200 (define* (marionette-screen-text marionette
203 "Take a screenshot of MARIONETTE, perform optical character
204 recognition (OCR), and return the text read from the screen as a string. Do
205 this by invoking OCRAD (file name for GNU Ocrad's command)"
206 (define (random-file-name)
207 (string-append "/tmp/marionette-screenshot-"
208 (number->string (random (expt 2 32)) 16)
211 (let ((image (random-file-name)))
215 (marionette-control (string-append "screendump " image)
218 ;; Tell Ocrad to invert the image colors (make it black on white) and
219 ;; to scale the image up, which significantly improves the quality of
220 ;; the result. In spite of this, be aware that OCR confuses "y" and
221 ;; "V" and sometimes erroneously introduces white space.
222 (let* ((pipe (open-pipe* OPEN_READ ocrad
223 "-i" "-s" "10" image))
224 (text (get-string-all pipe)))
225 (unless (zero? (close-pipe pipe))
226 (error "'ocrad' failed" ocrad))
229 (false-if-exception (delete-file image))))))
231 (define* (wait-for-screen-text marionette predicate
232 #:key (timeout 30) (ocrad "ocrad"))
233 "Wait for TIMEOUT seconds or until the screen text on MARIONETTE matches
234 PREDICATE, whichever comes first. Raise an error when TIMEOUT is exceeded."
236 (car (gettimeofday)))
242 (if (> (car (gettimeofday)) end)
243 (error "'wait-for-screen-text' timeout" predicate)
244 (or (predicate (marionette-screen-text marionette #:ocrad ocrad))
249 (define %qwerty-us-keystrokes
250 ;; Maps "special" characters to their keystrokes.
251 '((#\newline . "ret")
254 (#\+ . "shift-equal")
257 (#\? . "shift-slash")
258 (#\[ . "bracket_left")
259 (#\] . "bracket_right")
269 (#\" . "shift-apostrophe")
270 (#\` . "grave_accent")
274 (define (character->keystroke chr keystrokes)
275 "Return the keystroke for CHR according to the keyboard layout defined by
277 (if (char-set-contains? char-set:upper-case chr)
278 (string-append "shift-" (string (char-downcase chr)))
279 (or (assoc-ref keystrokes chr)
282 (define* (string->keystroke-commands str
285 %qwerty-us-keystrokes))
286 "Return a list of QEMU monitor commands to send the keystrokes corresponding
287 to STR. KEYSTROKES is an alist specifying a mapping from characters to
289 (string-fold-right (lambda (chr result)
292 (character->keystroke chr keystrokes))
297 (define* (marionette-type str marionette
298 #:key (keystrokes %qwerty-us-keystrokes))
299 "Type STR on MARIONETTE's keyboard, using the KEYSTROKES alist to map characters
300 to actual keystrokes."
301 (for-each (cut marionette-control <> marionette)
302 (string->keystroke-commands str keystrokes)))
304 ;;; marionette.scm ends here