Add ability to inspect install disks and live CDs.
[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 #include <rpc/types.h>
23 #include <rpc/xdr.h>
24
25 #ifdef HAVE_PCRE
26 #include <pcre.h>
27 #endif
28
29 #define STREQ(a,b) (strcmp((a),(b)) == 0)
30 #define STRCASEEQ(a,b) (strcasecmp((a),(b)) == 0)
31 #define STRNEQ(a,b) (strcmp((a),(b)) != 0)
32 #define STRCASENEQ(a,b) (strcasecmp((a),(b)) != 0)
33 #define STREQLEN(a,b,n) (strncmp((a),(b),(n)) == 0)
34 #define STRCASEEQLEN(a,b,n) (strncasecmp((a),(b),(n)) == 0)
35 #define STRNEQLEN(a,b,n) (strncmp((a),(b),(n)) != 0)
36 #define STRCASENEQLEN(a,b,n) (strncasecmp((a),(b),(n)) != 0)
37 #define STRPREFIX(a,b) (strncmp((a),(b),strlen((b))) == 0)
38
39 #ifdef HAVE_GETTEXT
40 #include "gettext.h"
41 #define _(str) dgettext(PACKAGE, (str))
42 #define N_(str) dgettext(PACKAGE, (str))
43 #else
44 #define _(str) str
45 #define N_(str) str
46 #endif
47
48 #define TMP_TEMPLATE_ON_STACK(var)                        \
49   const char *ttos_tmpdir = guestfs_tmpdir ();            \
50   char var[strlen (ttos_tmpdir) + 32];                    \
51   sprintf (var, "%s/libguestfsXXXXXX", ttos_tmpdir)       \
52
53 #define UNIX_PATH_MAX 108
54
55 #ifndef MAX
56 #define MAX(a,b) ((a)>(b)?(a):(b))
57 #endif
58
59 #ifdef __APPLE__
60 #define xdr_uint32_t xdr_u_int32_t
61 #endif
62
63 /* Network configuration of the appliance.  Note these addresses are
64  * only meaningful within the context of the running appliance.  QEMU
65  * translates network connections to these magic addresses into
66  * userspace calls on the host (eg. connect(2)).  qemu-doc has a nice
67  * diagram which is also useful to refer to.
68  *
69  * NETWORK: The network.
70  *
71  * ROUTER: The address of the "host", ie. this library.
72  *
73  * [Note: If you change NETWORK and ROUTER then you also have to
74  * change the network configuration in appliance/init].
75  *
76  * GUESTFWD_ADDR, GUESTFWD_PORT: The guestfwd feature of qemu
77  * magically connects this pseudo-address to the guestfwd channel.  In
78  * typical Linux configurations of libguestfs, guestfwd is not
79  * actually used any more.
80  */
81 #define NETWORK "169.254.0.0/16"
82 #define ROUTER "169.254.2.2"
83
84 /* GuestFS handle and connection. */
85 enum state { CONFIG, LAUNCHING, READY, BUSY, NO_HANDLE };
86
87 struct guestfs_h
88 {
89   struct guestfs_h *next;       /* Linked list of open handles. */
90
91   /* State: see the state machine diagram in the man page guestfs(3). */
92   enum state state;
93
94   int fd[2];                    /* Stdin/stdout of qemu. */
95   int sock;                     /* Daemon communications socket. */
96   pid_t pid;                    /* Qemu PID. */
97   pid_t recoverypid;            /* Recovery process PID. */
98
99   struct timeval launch_t;      /* The time that we called guestfs_launch. */
100
101   char *tmpdir;                 /* Temporary directory containing socket. */
102
103   char *qemu_help, *qemu_version; /* Output of qemu -help, qemu -version. */
104
105   char **cmdline;               /* Qemu command line. */
106   int cmdline_size;
107
108   int verbose;
109   int trace;
110   int autosync;
111   int direct;
112   int recovery_proc;
113   int enable_network;
114
115   char *path;                   /* Path to kernel, initrd. */
116   char *qemu;                   /* Qemu binary. */
117   char *append;                 /* Append to kernel command line. */
118
119   int memsize;                  /* Size of RAM (megabytes). */
120
121   int selinux;                  /* selinux enabled? */
122
123   char *last_error;
124   int last_errnum;              /* errno, or 0 if there was no errno */
125
126   /* Callbacks. */
127   guestfs_abort_cb           abort_cb;
128   guestfs_error_handler_cb   error_cb;
129   void *                     error_cb_data;
130   guestfs_log_message_cb     log_message_cb;
131   void *                     log_message_cb_data;
132   guestfs_subprocess_quit_cb subprocess_quit_cb;
133   void *                     subprocess_quit_cb_data;
134   guestfs_launch_done_cb     launch_done_cb;
135   void *                     launch_done_cb_data;
136   guestfs_close_cb           close_cb;
137   void *                     close_cb_data;
138   guestfs_progress_cb        progress_cb;
139   void *                     progress_cb_data;
140
141   int msg_next_serial;
142
143   /* Information gathered by inspect_os.  Must be freed by calling
144    * guestfs___free_inspect_info.
145    */
146   struct inspect_fs *fses;
147   size_t nr_fses;
148
149   /* Private data area. */
150   struct hash_table *pda;
151 };
152
153 /* Per-filesystem data stored for inspect_os. */
154 enum inspect_fs_content {
155   FS_CONTENT_UNKNOWN = 0,
156   FS_CONTENT_LINUX_ROOT,
157   FS_CONTENT_WINDOWS_ROOT,
158   FS_CONTENT_LINUX_BOOT,
159   FS_CONTENT_LINUX_USR,
160   FS_CONTENT_LINUX_USR_LOCAL,
161   FS_CONTENT_LINUX_VAR,
162   FS_CONTENT_FREEBSD_ROOT,
163   FS_CONTENT_INSTALLER,
164 };
165
166 enum inspect_os_format {
167   OS_FORMAT_UNKNOWN = 0,
168   OS_FORMAT_INSTALLED,
169   OS_FORMAT_INSTALLER,
170   /* in future: supplemental disks */
171 };
172
173 enum inspect_os_type {
174   OS_TYPE_UNKNOWN = 0,
175   OS_TYPE_LINUX,
176   OS_TYPE_WINDOWS,
177   OS_TYPE_FREEBSD,
178 };
179
180 enum inspect_os_distro {
181   OS_DISTRO_UNKNOWN = 0,
182   OS_DISTRO_DEBIAN,
183   OS_DISTRO_FEDORA,
184   OS_DISTRO_REDHAT_BASED,
185   OS_DISTRO_RHEL,
186   OS_DISTRO_WINDOWS,
187   OS_DISTRO_PARDUS,
188   OS_DISTRO_ARCHLINUX,
189   OS_DISTRO_GENTOO,
190   OS_DISTRO_UBUNTU,
191   OS_DISTRO_MEEGO,
192   OS_DISTRO_LINUX_MINT,
193   OS_DISTRO_MANDRIVA,
194 };
195
196 enum inspect_os_package_format {
197   OS_PACKAGE_FORMAT_UNKNOWN = 0,
198   OS_PACKAGE_FORMAT_RPM,
199   OS_PACKAGE_FORMAT_DEB,
200   OS_PACKAGE_FORMAT_PACMAN,
201   OS_PACKAGE_FORMAT_EBUILD,
202   OS_PACKAGE_FORMAT_PISI
203 };
204
205 enum inspect_os_package_management {
206   OS_PACKAGE_MANAGEMENT_UNKNOWN = 0,
207   OS_PACKAGE_MANAGEMENT_YUM,
208   OS_PACKAGE_MANAGEMENT_UP2DATE,
209   OS_PACKAGE_MANAGEMENT_APT,
210   OS_PACKAGE_MANAGEMENT_PACMAN,
211   OS_PACKAGE_MANAGEMENT_PORTAGE,
212   OS_PACKAGE_MANAGEMENT_PISI,
213   OS_PACKAGE_MANAGEMENT_URPMI,
214 };
215
216 struct inspect_fs {
217   int is_root;
218   char *device;
219   int is_mountable;
220   int is_swap;
221   enum inspect_fs_content content;
222   enum inspect_os_type type;
223   enum inspect_os_distro distro;
224   enum inspect_os_package_format package_format;
225   enum inspect_os_package_management package_management;
226   char *product_name;
227   int major_version;
228   int minor_version;
229   char *arch;
230   char *hostname;
231   char *windows_systemroot;
232   enum inspect_os_format format;
233   int is_live_disk;
234   int is_netinst_disk;
235   int is_multipart_disk;
236   struct inspect_fstab_entry *fstab;
237   size_t nr_fstab;
238 };
239
240 struct inspect_fstab_entry {
241   char *device;
242   char *mountpoint;
243 };
244
245 struct guestfs_message_header;
246 struct guestfs_message_error;
247
248 extern void guestfs_error (guestfs_h *g, const char *fs, ...)
249   __attribute__((format (printf,2,3)));
250 extern void guestfs_error_errno (guestfs_h *g, int errnum, const char *fs, ...)
251   __attribute__((format (printf,3,4)));
252 extern void guestfs_perrorf (guestfs_h *g, const char *fs, ...)
253   __attribute__((format (printf,2,3)));
254 extern void *guestfs_safe_realloc (guestfs_h *g, void *ptr, int nbytes);
255 extern char *guestfs_safe_strdup (guestfs_h *g, const char *str);
256 extern char *guestfs_safe_strndup (guestfs_h *g, const char *str, size_t n);
257 extern void *guestfs_safe_memdup (guestfs_h *g, void *ptr, size_t size);
258 extern char *guestfs_safe_asprintf (guestfs_h *g, const char *fs, ...)
259   __attribute__((format (printf,2,3)));
260 extern void guestfs___print_timestamped_argv (guestfs_h *g, const char *argv[]);
261 extern void guestfs___print_timestamped_message (guestfs_h *g, const char *fs, ...);
262 extern void guestfs___free_inspect_info (guestfs_h *g);
263 extern int guestfs___set_busy (guestfs_h *g);
264 extern int guestfs___end_busy (guestfs_h *g);
265 extern int guestfs___send (guestfs_h *g, int proc_nr, uint64_t progress_hint, uint64_t optargs_bitmask, xdrproc_t xdrp, char *args);
266 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);
267 extern int guestfs___send_file (guestfs_h *g, const char *filename);
268 extern int guestfs___recv_file (guestfs_h *g, const char *filename);
269 extern int guestfs___send_to_daemon (guestfs_h *g, const void *v_buf, size_t n);
270 extern int guestfs___recv_from_daemon (guestfs_h *g, uint32_t *size_rtn, void **buf_rtn);
271 extern int guestfs___accept_from_daemon (guestfs_h *g);
272 extern int guestfs___build_appliance (guestfs_h *g, char **kernel, char **initrd, char **appliance);
273 extern void guestfs___print_BufferIn (FILE *out, const char *buf, size_t buf_size);
274 extern void guestfs___print_BufferOut (FILE *out, const char *buf, size_t buf_size);
275 #ifdef HAVE_PCRE
276 extern int guestfs___match (guestfs_h *g, const char *str, const pcre *re);
277 extern char *guestfs___match1 (guestfs_h *g, const char *str, const pcre *re);
278 extern int guestfs___match2 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2);
279 extern int guestfs___match3 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2, char **ret3);
280 #endif
281 extern int guestfs___feature_available (guestfs_h *g, const char *feature);
282 extern void guestfs___free_string_list (char **);
283 extern int guestfs___checkpoint_cmdline (guestfs_h *g);
284 extern void guestfs___rollback_cmdline (guestfs_h *g, int pos);
285
286 #define error(g,...) guestfs_error_errno((g),0,__VA_ARGS__)
287 #define perrorf guestfs_perrorf
288 #define safe_calloc guestfs_safe_calloc
289 #define safe_malloc guestfs_safe_malloc
290 #define safe_realloc guestfs_safe_realloc
291 #define safe_strdup guestfs_safe_strdup
292 #define safe_strndup guestfs_safe_strndup
293 #define safe_memdup guestfs_safe_memdup
294 #define safe_asprintf guestfs_safe_asprintf
295 #ifdef HAVE_PCRE
296 #define match guestfs___match
297 #define match1 guestfs___match1
298 #define match2 guestfs___match2
299 #define match3 guestfs___match3
300 #endif
301
302 #endif /* GUESTFS_INTERNAL_H_ */