2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include <sys/types.h>
29 #include <utils/linked_list.h>
36 #define PERME (S_IRWXU | S_IRWXG)
37 #define PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
39 typedef struct private_guest_t private_guest_t
;
41 struct private_guest_t
{
42 /** implemented public interface */
44 /** name of the guest */
46 /** directory of guest */
48 /** directory name of guest */
50 /** amount of memory for guest, in MB */
52 /** pid of guest child process */
56 /** log file for console 0 */
58 /** FUSE cowfs instance */
60 /** mconsole to control running UML */
62 /** list of interfaces attached to the guest */
63 linked_list_t
*ifaces
;
66 ENUM(guest_state_names
, GUEST_STOPPED
, GUEST_STOPPING
,
75 * Implementation of guest_t.get_name.
77 static char* get_name(private_guest_t
*this)
83 * Implementation of guest_t.create_iface.
85 static iface_t
* create_iface(private_guest_t
*this, char *name
)
90 if (this->state
!= GUEST_RUNNING
)
92 DBG1("guest '%s' not running, unable to add interface", this->name
);
96 iterator
= this->ifaces
->create_iterator(this->ifaces
, TRUE
);
97 while (iterator
->iterate(iterator
, (void**)&iface
))
99 if (streq(name
, iface
->get_guestif(iface
)))
101 DBG1("guest '%s' already has an interface '%s'", this->name
, name
);
102 iterator
->destroy(iterator
);
106 iterator
->destroy(iterator
);
108 iface
= iface_create(this->name
, name
, this->mconsole
);
111 this->ifaces
->insert_last(this->ifaces
, iface
);
117 * Implementation of guest_t.create_iface_iterator.
119 static iterator_t
* create_iface_iterator(private_guest_t
*this)
121 return this->ifaces
->create_iterator(this->ifaces
, TRUE
);
125 * Implementation of guest_t.get_state.
127 static guest_state_t
get_state(private_guest_t
*this)
133 * Implementation of guest_t.get_pid.
135 static pid_t
get_pid(private_guest_t
*this)
141 * write format string to a buffer, and advance buffer position
143 static char* write_arg(char **pos
, size_t *left
, char *format
, ...)
149 va_start(args
, format
);
150 len
= vsnprintf(*pos
, *left
, format
, args
);
163 * Implementation of guest_t.stop.
165 static void stop(private_guest_t
*this)
167 if (this->state
!= GUEST_STOPPED
)
169 this->state
= GUEST_STOPPING
;
170 this->ifaces
->destroy_offset(this->ifaces
, offsetof(iface_t
, destroy
));
171 this->ifaces
= linked_list_create();
172 kill(this->pid
, SIGINT
);
173 while (this->state
== GUEST_STOPPING
)
181 * Implementation of guest_t.start.
183 static bool start(private_guest_t
*this, char *kernel
)
190 size_t left
= sizeof(buf
);
192 if (this->state
!= GUEST_STOPPED
)
194 DBG1("unable to start guest in state %N", guest_state_names
, this->state
);
197 this->state
= GUEST_STARTING
;
199 notify
= write_arg(&pos
, &left
, "%s/%s", this->dirname
, NOTIFY_FILE
);
201 args
[i
++] = write_arg(&pos
, &left
, "%s/%s", this->dirname
, KERNEL_FILE
);
202 args
[i
++] = write_arg(&pos
, &left
, "root=/dev/root");
203 args
[i
++] = write_arg(&pos
, &left
, "rootfstype=hostfs");
204 args
[i
++] = write_arg(&pos
, &left
, "rootflags=%s/%s", this->dirname
, UNION_DIR
);
205 args
[i
++] = write_arg(&pos
, &left
, "uml_dir=%s", this->dirname
);
206 args
[i
++] = write_arg(&pos
, &left
, "umid=%s", this->name
);
207 args
[i
++] = write_arg(&pos
, &left
, "mem=%dM", this->mem
);
208 args
[i
++] = write_arg(&pos
, &left
, "mconsole=notify:%s", notify
);
209 /*args[i++] = write_arg(&pos, &left, "con=pts");*/
210 args
[i
++] = write_arg(&pos
, &left
, "con0=null,fd:%d", this->bootlog
);
211 /*args[i++] = write_arg(&pos, &left, "con1=fd:0,fd:1");*/
212 args
[i
++] = write_arg(&pos
, &left
, "con2=null,null");
213 args
[i
++] = write_arg(&pos
, &left
, "con3=null,null");
214 args
[i
++] = write_arg(&pos
, &left
, "con4=null,null");
215 args
[i
++] = write_arg(&pos
, &left
, "con5=null,null");
216 args
[i
++] = write_arg(&pos
, &left
, "con6=null,null");
223 dup2(open("/dev/null", 0), 0);
224 dup2(open("/dev/null", 0), 1);
225 dup2(open("/dev/null", 0), 2);
226 execvp(args
[0], args
);
227 DBG1("starting UML kernel '%s' failed: %m", args
[0]);
230 this->state
= GUEST_STOPPED
;
236 this->mconsole
= mconsole_create(notify
);
237 if (this->mconsole
== NULL
)
239 DBG1("opening mconsole at '%s' failed, stopping guest", buf
);
243 this->state
= GUEST_RUNNING
;
248 * Implementation of guest_t.sigchild.
250 static void sigchild(private_guest_t
*this)
252 waitpid(this->pid
, NULL
, WNOHANG
);
253 DESTROY_IF(this->mconsole
);
254 this->mconsole
= NULL
;
255 this->state
= GUEST_STOPPED
;
259 * umount the union filesystem
261 static bool umount_unionfs(private_guest_t
*this)
265 this->cowfs
->destroy(this->cowfs
);
273 * mount the union filesystem
275 static bool mount_unionfs(private_guest_t
*this)
277 char master
[PATH_MAX
];
279 char mount
[PATH_MAX
];
281 if (this->cowfs
== NULL
)
283 snprintf(master
, sizeof(master
), "%s/%s", this->dirname
, MASTER_DIR
);
284 snprintf(diff
, sizeof(diff
), "%s/%s", this->dirname
, DIFF_DIR
);
285 snprintf(mount
, sizeof(mount
), "%s/%s", this->dirname
, UNION_DIR
);
287 this->cowfs
= cowfs_create(master
, diff
, mount
);
297 * open logfile for boot messages
299 static int open_bootlog(private_guest_t
*this)
303 fd
= openat(this->dir
, LOG_FILE
, O_WRONLY
| O_CREAT
, PERM
);
306 DBG1("opening bootlog failed, using stdout");
313 * load memory configuration from file
315 int loadmem(private_guest_t
*this)
320 file
= fdopen(openat(this->dir
, MEMORY_FILE
, O_RDONLY
, PERM
), "r");
323 if (fscanf(file
, "%d", &mem
) <= 0)
333 * save memory configuration to file
335 bool savemem(private_guest_t
*this, int mem
)
340 file
= fdopen(openat(this->dir
, MEMORY_FILE
, O_RDWR
| O_CREAT
| O_TRUNC
,
344 if (fprintf(file
, "%d", mem
) > 0)
354 * Implementation of guest_t.destroy.
356 static void destroy(private_guest_t
*this)
359 umount_unionfs(this);
360 if (this->bootlog
> 1)
362 close(this->bootlog
);
374 * generic guest constructor
376 static private_guest_t
*guest_create_generic(char *parent
, char *name
,
380 private_guest_t
*this = malloc_thing(private_guest_t
);
382 this->public.get_name
= (void*)get_name
;
383 this->public.get_pid
= (pid_t(*)(guest_t
*))get_pid
;
384 this->public.get_state
= (guest_state_t(*)(guest_t
*))get_state
;
385 this->public.create_iface
= (iface_t
*(*)(guest_t
*,char*))create_iface
;
386 this->public.create_iface_iterator
= (iterator_t
*(*)(guest_t
*))create_iface_iterator
;
387 this->public.start
= (void*)start
;
388 this->public.stop
= (void*)stop
;
389 this->public.sigchild
= (void(*)(guest_t
*))sigchild
;
390 this->public.destroy
= (void*)destroy
;
392 if (*parent
== '/' || getcwd(cwd
, sizeof(cwd
)) == NULL
)
394 asprintf(&this->dirname
, "%s/%s", parent
, name
);
398 asprintf(&this->dirname
, "%s/%s/%s", cwd
, parent
, name
);
402 mkdir(this->dirname
, PERME
);
404 this->dir
= open(this->dirname
, O_DIRECTORY
, PERME
);
407 DBG1("opening guest directory '%s' failed: %m", this->dirname
);
414 this->state
= GUEST_STOPPED
;
415 this->mconsole
= NULL
;
416 this->ifaces
= linked_list_create();
418 this->bootlog
= open_bootlog(this);
419 this->name
= strdup(name
);
426 * create a symlink to old called new in our working dir
428 static bool make_symlink(private_guest_t
*this, char *old
, char *new)
433 if (*old
== '/' || getcwd(cwd
, sizeof(cwd
)) == NULL
)
435 snprintf(buf
, sizeof(buf
), "%s", old
);
439 snprintf(buf
, sizeof(buf
), "%s/%s", cwd
, old
);
441 return symlinkat(buf
, this->dir
, new) == 0;
446 * create the guest instance, including required dirs and mounts
448 guest_t
*guest_create(char *parent
, char *name
, char *kernel
,
449 char *master
, int mem
)
451 private_guest_t
*this = guest_create_generic(parent
, name
, TRUE
);
458 if (!make_symlink(this, master
, MASTER_DIR
) ||
459 !make_symlink(this, kernel
, KERNEL_FILE
))
461 DBG1("creating master/kernel symlink failed: %m");
466 if (mkdirat(this->dir
, UNION_DIR
, PERME
) != 0 ||
467 mkdirat(this->dir
, DIFF_DIR
, PERME
) != 0)
469 DBG1("unable to create directories for '%s': %m", name
);
475 if (!savemem(this, mem
))
481 if (!mount_unionfs(this))
487 return &this->public;
491 * load an already created guest
493 guest_t
*guest_load(char *parent
, char *name
)
495 private_guest_t
*this = guest_create_generic(parent
, name
, FALSE
);
502 this->mem
= loadmem(this);
505 DBG1("unable to open memory configuration file: %m", name
);
510 if (!mount_unionfs(this))
516 return &this->public;