contrib: Update README files.
[libguestfs.git] / contrib / guestfsd-in-wine.sh
1 #!/bin/bash -
2 # Copyright (C) 2009 Red Hat Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 # INSTRUCTIONS
19 #----------------------------------------------------------------------
20 #
21 # This is a QEMU wrapper script that allows you to run a
22 # Windows-compiled guestfsd.exe (daemon) under Wine from a Linux main
23 # program.  You need to read and understand all the instructions below
24 # before use.
25 #
26 # To understand how to compile the daemon for Windows, please read:
27 # http://www.redhat.com/archives/libguestfs/2009-November/msg00255.html
28 #
29 # Adjust the Wine configuration so it can find the libraries, as
30 # described here:
31 # http://fedoraproject.org/wiki/MinGW/Configure_wine
32 #
33 # On Fedora 13 there is a serious bug in Wine.  See:
34 # https://bugzilla.redhat.com/show_bug.cgi?id=533806#c11
35 #
36 # If necessary, adjust the line 'guestfsd=...' below so it points to
37 # the correct location of the guestfsd.exe program.  You can use an
38 # absolute path here if you want.
39 guestfsd=daemon/guestfsd.exe
40 #
41 # This script is a QEMU wrapper.  It pretends to be qemu as far as
42 # libguestfs programs are concerned.  Read this to understand the
43 # purpose of QEMU wrappers:
44 # http://libguestfs.org/guestfs.3.html#qemu_wrappers
45 #
46 # With this script, the qemu program is not actually run.  Instead we
47 # pretend to be qemu, parse out the necessary parts of the long
48 # command line that libguestfs passes to qemu, and run the Windows
49 # daemon, under Wine, with the right command line.  The Windows daemon
50 # then hopefully connects back to the libguestfs socket, and as far as
51 # the libguestfs program is concerned, it looks like a full appliance
52 # is running.
53 #
54 # To use this script, you must set the environment variable
55 # LIBGUESTFS_QEMU=/path/to/contrib/guestfsd-in-wine.sh (ie. the path
56 # to this script).
57 #
58 # You can then run libguestfs test programs, and (hopefully!) they'll
59 # use the Windows guestfsd.exe, simulating calls using Wine.
60 #
61 # For example from the top build directory:
62 #
63 # LIBGUESTFS_QEMU=contrib/guestfsd-in-wine.sh fish/guestfish
64 #
65 # You might also need to set the environment variable LIBGUESTFS_PATH
66 # to point to an appliance.  The appliance will never be used, but
67 # libguestfs needs to find one.
68 #
69 # Another suggested environment variable is LIBGUESTFS_DEBUG=1 which
70 # will give you must more detail about what is going on.  Also look at
71 # the contents of the log file 'guestfsd-in-wine.log' after each run.
72 #
73 #----------------------------------------------------------------------
74
75 # Note that stdout & stderr messages will get eaten by libguestfs
76 # early on in the process.  Therefore write log messages to
77 # a log file.
78 exec 5>>guestfsd-in-wine.log
79 echo "Environment:" >&5
80 printenv | grep LIBGUESTFS >&5
81 echo "Command line:" >&5
82 echo "  $@" >&5
83
84 # We're called several times, first with -help and -version, and we
85 # have to pretend to be qemu!  (At least enough to trick libguestfs).
86 if [ "$1" = "-help" ]; then
87     echo -- "  -net user  "
88     echo -- "  -no-hpet  "
89     echo -- "  -rtc-td-hack  "
90     exit 0
91 elif [ "$1" = "-version" ]; then
92     echo -- "0.0.0"
93     exit 0
94 fi
95
96 # The interesting parameter is -append.
97 append=
98 while [ $# -gt 0 ]; do
99     if [ $1 = "-append" ]; then
100         append="$2"
101         shift
102     fi
103     shift
104 done
105 echo "Append parameter:" >&5
106 echo "  $append" >&5
107
108 # guestfs_vmchannel parameter.
109 vmchannel_param=$(echo "$append" | grep -Eo 'guestfs_vmchannel=[^[:space:]]+')
110 echo "Vmchannel parameter:" >&5
111 echo "  $vmchannel_param" >&5
112
113 # Port number.
114 port=$(echo "$vmchannel_param" | grep -Eo '[[:digit:]]+$')
115 echo "Port number:" >&5
116 echo "  $vmchannel_param" >&5
117
118 # Run guestfsd.exe.
119 echo "Command:" >&5
120 echo "  $guestfsd -f -v -c tcp:localhost:$port" >&5
121 $guestfsd -f -v -c tcp:127.0.0.1:$port