New APIs: set-network and get-network to enable network support.
[libguestfs.git] / src / guestfs-internal.h
1 /* libguestfs
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef GUESTFS_INTERNAL_H_
20 #define GUESTFS_INTERNAL_H_
21
22 #define STREQ(a,b) (strcmp((a),(b)) == 0)
23 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
24 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
25 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
26 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
27 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
28 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
29 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
30 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
31
32 #ifdef HAVE_GETTEXT
33 #include "gettext.h"
34 #define _(str) dgettext(PACKAGE, (str))
35 #define N_(str) dgettext(PACKAGE, (str))
36 #else
37 #define _(str) str
38 #define N_(str) str
39 #endif
40
41 #define UNIX_PATH_MAX 108
42
43 #ifndef MAX
44 #define MAX(a,b) ((a)>(b)?(a):(b))
45 #endif
46
47 #ifdef __APPLE__
48 #define xdr_uint32_t xdr_u_int32_t
49 #endif
50
51 /* Network configuration of the appliance.  Note these addresses are
52  * only meaningful within the context of the running appliance.  QEMU
53  * translates network connections to these magic addresses into
54  * userspace calls on the host (eg. connect(2)).  qemu-doc has a nice
55  * diagram which is also useful to refer to.
56  *
57  * NETWORK: The network.
58  *
59  * ROUTER: The address of the "host", ie. this library.
60  *
61  * [Note: If you change NETWORK and ROUTER then you also have to
62  * change the network configuration in appliance/init].
63  *
64  * GUESTFWD_ADDR, GUESTFWD_PORT: The guestfwd feature of qemu
65  * magically connects this pseudo-address to the guestfwd channel.  In
66  * typical Linux configurations of libguestfs, guestfwd is not
67  * actually used any more.
68  */
69 #define NETWORK "169.254.0.0/16"
70 #define ROUTER "169.254.2.2"
71
72 /* GuestFS handle and connection. */
73 enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
74
75 struct guestfs_h
76 {
77   struct guestfs_h *next;       /* Linked list of open handles. */
78
79   /* State: see the state machine diagram in the man page guestfs(3). */
80   enum state state;
81
82   int fd[2];                    /* Stdin/stdout of qemu. */
83   int sock;                     /* Daemon communications socket. */
84   pid_t pid;                    /* Qemu PID. */
85   pid_t recoverypid;            /* Recovery process PID. */
86
87   struct timeval launch_t;      /* The time that we called guestfs_launch. */
88
89   char *tmpdir;                 /* Temporary directory containing socket. */
90
91   char *qemu_help, *qemu_version; /* Output of qemu -help, qemu -version. */
92
93   char **cmdline;               /* Qemu command line. */
94   int cmdline_size;
95
96   int verbose;
97   int trace;
98   int autosync;
99   int direct;
100   int recovery_proc;
101   int enable_network;
102
103   char *path;                   /* Path to kernel, initrd. */
104   char *qemu;                   /* Qemu binary. */
105   char *append;                 /* Append to kernel command line. */
106
107   int memsize;                  /* Size of RAM (megabytes). */
108
109   int selinux;                  /* selinux enabled? */
110
111   char *last_error;
112
113   /* Callbacks. */
114   guestfs_abort_cb           abort_cb;
115   guestfs_error_handler_cb   error_cb;
116   void *                     error_cb_data;
117   guestfs_log_message_cb     log_message_cb;
118   void *                     log_message_cb_data;
119   guestfs_subprocess_quit_cb subprocess_quit_cb;
120   void *                     subprocess_quit_cb_data;
121   guestfs_launch_done_cb     launch_done_cb;
122   void *                     launch_done_cb_data;
123   guestfs_close_cb           close_cb;
124   void *                     close_cb_data;
125
126   int msg_next_serial;
127
128   /* Information gathered by inspect_os.  Must be freed by calling
129    * guestfs___free_inspect_info.
130    */
131   struct inspect_fs *fses;
132   size_t nr_fses;
133 };
134
135 /* Per-filesystem data stored for inspect_os. */
136 enum inspect_fs_content {
137   FS_CONTENT_UNKNOWN = 0,
138   FS_CONTENT_LINUX_ROOT,
139   FS_CONTENT_WINDOWS_ROOT,
140   FS_CONTENT_LINUX_BOOT,
141   FS_CONTENT_LINUX_USR,
142   FS_CONTENT_LINUX_USR_LOCAL,
143   FS_CONTENT_LINUX_VAR,
144 };
145
146 enum inspect_os_type {
147   OS_TYPE_UNKNOWN = 0,
148   OS_TYPE_LINUX,
149   OS_TYPE_WINDOWS,
150 };
151
152 enum inspect_os_distro {
153   OS_DISTRO_UNKNOWN = 0,
154   OS_DISTRO_DEBIAN,
155   OS_DISTRO_FEDORA,
156   OS_DISTRO_REDHAT_BASED,
157   OS_DISTRO_RHEL,
158   OS_DISTRO_WINDOWS,
159 };
160
161 struct inspect_fs {
162   int is_root;
163   char *device;
164   int is_mountable;
165   int is_swap;
166   enum inspect_fs_content content;
167   enum inspect_os_type type;
168   enum inspect_os_distro distro;
169   char *product_name;
170   int major_version;
171   int minor_version;
172   char *arch;
173   struct inspect_fstab_entry *fstab;
174   size_t nr_fstab;
175 };
176
177 struct inspect_fstab_entry {
178   char *device;
179   char *mountpoint;
180 };
181
182 struct guestfs_message_header;
183 struct guestfs_message_error;
184
185 extern void guestfs_error (guestfs_h *g, const char *fs, ...)
186   __attribute__((format (printf,2,3)));
187 extern void guestfs_perrorf (guestfs_h *g, const char *fs, ...)
188   __attribute__((format (printf,2,3)));
189 extern void *guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes);
190 extern char *guestfs_safe_strdup (guestfs_h *g, const char *str);
191 extern char *guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n);
192 extern void *guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size);
193 extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...);
194 extern const char *guestfs___tmpdir (void);
195 extern void guestfs___free_inspect_info (guestfs_h *g);
196 extern int guestfs___set_busy (guestfs_h *g);
197 extern int guestfs___end_busy (guestfs_h *g);
198 extern int guestfs___send (guestfs_h *g, int proc_nr, xdrproc_t xdrp, char *args);
199 extern int guestfs___recv (guestfs_h *g, const char *fn, struct guestfs_message_header *hdr, struct guestfs_message_error *err, xdrproc_t xdrp, char *ret);
200 extern int guestfs___send_file (guestfs_h *g, const char *filename);
201 extern int guestfs___recv_file (guestfs_h *g, const char *filename);
202 extern int guestfs___send_to_daemon (guestfs_h *g, const void *v_buf, size_t n);
203 extern int guestfs___recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn);
204 extern int guestfs___accept_from_daemon (guestfs_h *g);
205 extern int guestfs___build_appliance (guestfs_h *g, char **kernel, char **initrd, char **appliance);
206
207 #define error guestfs_error
208 #define perrorf guestfs_perrorf
209 #define safe_malloc guestfs_safe_malloc
210 #define safe_realloc guestfs_safe_realloc
211 #define safe_strdup guestfs_safe_strdup
212 #define safe_strndup guestfs_safe_strndup
213 #define safe_memdup guestfs_safe_memdup
214
215 #endif /* GUESTFS_INTERNAL_H_ */