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>
30 #include <utils/linked_list.h>
37 #define PERME (S_IRWXU | S_IRWXG)
38 #define PERM (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
40 #define MASTER_DIR "master"
41 #define DIFF_DIR "diff"
42 #define UNION_DIR "union"
43 #define MEMORY_FILE "mem"
44 #define KERNEL_FILE "linux"
45 #define LOG_FILE "boot.log"
46 #define NOTIFY_FILE "notify"
49 typedef struct private_guest_t private_guest_t
;
51 struct private_guest_t
{
52 /** implemented public interface */
54 /** name of the guest */
56 /** directory of guest */
58 /** directory name of guest */
60 /** amount of memory for guest, in MB */
62 /** pid of guest child process */
66 /** log file for console 0 */
68 /** FUSE cowfs instance */
70 /** mconsole to control running UML */
72 /** list of interfaces attached to the guest */
73 linked_list_t
*ifaces
;
76 ENUM(guest_state_names
, GUEST_STOPPED
, GUEST_STOPPING
,
85 * Implementation of guest_t.get_name.
87 static char* get_name(private_guest_t
*this)
93 * Implementation of guest_t.create_iface.
95 static iface_t
* create_iface(private_guest_t
*this, char *name
)
100 if (this->state
!= GUEST_RUNNING
)
102 DBG1("guest '%s' not running, unable to add interface", this->name
);
106 iterator
= this->ifaces
->create_iterator(this->ifaces
, TRUE
);
107 while (iterator
->iterate(iterator
, (void**)&iface
))
109 if (streq(name
, iface
->get_guestif(iface
)))
111 DBG1("guest '%s' already has an interface '%s'", this->name
, name
);
112 iterator
->destroy(iterator
);
116 iterator
->destroy(iterator
);
118 iface
= iface_create(this->name
, name
, this->mconsole
);
121 this->ifaces
->insert_last(this->ifaces
, iface
);
127 * Implementation of guest_t.create_iface_iterator.
129 static iterator_t
* create_iface_iterator(private_guest_t
*this)
131 return this->ifaces
->create_iterator(this->ifaces
, TRUE
);
135 * Implementation of guest_t.get_state.
137 static guest_state_t
get_state(private_guest_t
*this)
143 * Implementation of guest_t.get_pid.
145 static pid_t
get_pid(private_guest_t
*this)
151 * write format string to a buffer, and advance buffer position
153 static char* write_arg(char **pos
, size_t *left
, char *format
, ...)
159 va_start(args
, format
);
160 len
= vsnprintf(*pos
, *left
, format
, args
);
173 * Implementation of get_t.close_console.
175 static char* get_console(private_guest_t
*this, int console
)
177 if (this->state
== GUEST_RUNNING
)
179 return this->mconsole
->get_console_pts(this->mconsole
, console
);
185 * Implementation of guest_t.stop.
187 static void stop(private_guest_t
*this)
189 if (this->state
!= GUEST_STOPPED
)
191 this->state
= GUEST_STOPPING
;
192 this->ifaces
->destroy_offset(this->ifaces
, offsetof(iface_t
, destroy
));
193 this->ifaces
= linked_list_create();
194 kill(this->pid
, SIGINT
);
195 waitpid(this->pid
, NULL
, 0);
196 this->state
= GUEST_STOPPED
;
201 * Implementation of guest_t.start.
203 static bool start(private_guest_t
*this)
210 size_t left
= sizeof(buf
);
212 if (this->state
!= GUEST_STOPPED
)
214 DBG1("unable to start guest in state %N", guest_state_names
, this->state
);
217 this->state
= GUEST_STARTING
;
219 notify
= write_arg(&pos
, &left
, "%s/%s", this->dirname
, NOTIFY_FILE
);
221 args
[i
++] = write_arg(&pos
, &left
, "%s/%s", this->dirname
, KERNEL_FILE
);
222 args
[i
++] = write_arg(&pos
, &left
, "root=/dev/root");
223 args
[i
++] = write_arg(&pos
, &left
, "rootfstype=hostfs");
224 args
[i
++] = write_arg(&pos
, &left
, "rootflags=%s/%s", this->dirname
, UNION_DIR
);
225 args
[i
++] = write_arg(&pos
, &left
, "uml_dir=%s", this->dirname
);
226 args
[i
++] = write_arg(&pos
, &left
, "umid=%s", this->name
);
227 args
[i
++] = write_arg(&pos
, &left
, "mem=%dM", this->mem
);
228 args
[i
++] = write_arg(&pos
, &left
, "mconsole=notify:%s", notify
);
229 args
[i
++] = write_arg(&pos
, &left
, "con=pts");
230 args
[i
++] = write_arg(&pos
, &left
, "con0=none,fd:%d", this->bootlog
);
237 dup2(open("/dev/null", 0), 0);
238 dup2(this->bootlog
, 1);
239 dup2(this->bootlog
, 2);
240 execvp(args
[0], args
);
241 DBG1("starting UML kernel '%s' failed: %m", args
[0]);
244 this->state
= GUEST_STOPPED
;
250 this->mconsole
= mconsole_create(notify
);
251 if (this->mconsole
== NULL
)
253 DBG1("opening mconsole at '%s' failed, stopping guest", buf
);
258 this->state
= GUEST_RUNNING
;
263 * Implementation of guest_t.load_template.
265 static bool load_template(private_guest_t
*this, char *path
)
272 return this->cowfs
->set_overlay(this->cowfs
, NULL
);
275 len
= snprintf(dir
, sizeof(dir
), "%s/%s", path
, this->name
);
276 if (len
< 0 || len
>= sizeof(dir
))
280 if (access(dir
, F_OK
) != 0)
282 if (mkdir(dir
, PERME
) != 0)
284 DBG1("creating overlay for guest '%s' failed: %m", this->name
);
288 return this->cowfs
->set_overlay(this->cowfs
, dir
);
292 * Implementation of guest_t.sigchild.
294 static void sigchild(private_guest_t
*this)
296 if (this->state
!= GUEST_STOPPING
)
297 { /* collect zombie if uml crashed */
298 waitpid(this->pid
, NULL
, WNOHANG
);
300 DESTROY_IF(this->mconsole
);
301 this->mconsole
= NULL
;
302 this->state
= GUEST_STOPPED
;
306 * umount the union filesystem
308 static bool umount_unionfs(private_guest_t
*this)
312 this->cowfs
->destroy(this->cowfs
);
320 * mount the union filesystem
322 static bool mount_unionfs(private_guest_t
*this)
324 char master
[PATH_MAX
];
326 char mount
[PATH_MAX
];
328 if (this->cowfs
== NULL
)
330 snprintf(master
, sizeof(master
), "%s/%s", this->dirname
, MASTER_DIR
);
331 snprintf(diff
, sizeof(diff
), "%s/%s", this->dirname
, DIFF_DIR
);
332 snprintf(mount
, sizeof(mount
), "%s/%s", this->dirname
, UNION_DIR
);
334 this->cowfs
= cowfs_create(master
, diff
, mount
);
344 * open logfile for boot messages
346 static int open_bootlog(private_guest_t
*this)
350 fd
= openat(this->dir
, LOG_FILE
, O_WRONLY
| O_CREAT
, PERM
);
353 DBG1("opening bootlog failed, using stdout");
360 * load memory configuration from file
362 int loadmem(private_guest_t
*this)
367 file
= fdopen(openat(this->dir
, MEMORY_FILE
, O_RDONLY
, PERM
), "r");
370 if (fscanf(file
, "%d", &mem
) <= 0)
380 * save memory configuration to file
382 bool savemem(private_guest_t
*this, int mem
)
387 file
= fdopen(openat(this->dir
, MEMORY_FILE
, O_RDWR
| O_CREAT
| O_TRUNC
,
391 if (fprintf(file
, "%d", mem
) > 0)
401 * Implementation of guest_t.destroy.
403 static void destroy(private_guest_t
*this)
406 umount_unionfs(this);
407 if (this->bootlog
> 1)
409 close(this->bootlog
);
421 * generic guest constructor
423 static private_guest_t
*guest_create_generic(char *parent
, char *name
,
427 private_guest_t
*this = malloc_thing(private_guest_t
);
429 this->public.get_name
= (void*)get_name
;
430 this->public.get_pid
= (pid_t(*)(guest_t
*))get_pid
;
431 this->public.get_state
= (guest_state_t(*)(guest_t
*))get_state
;
432 this->public.create_iface
= (iface_t
*(*)(guest_t
*,char*))create_iface
;
433 this->public.create_iface_iterator
= (iterator_t
*(*)(guest_t
*))create_iface_iterator
;
434 this->public.start
= (void*)start
;
435 this->public.stop
= (void*)stop
;
436 this->public.get_console
= (char*(*)(guest_t
*,int))get_console
;
437 this->public.load_template
= (bool(*)(guest_t
*, char *path
))load_template
;
438 this->public.sigchild
= (void(*)(guest_t
*))sigchild
;
439 this->public.destroy
= (void*)destroy
;
441 if (*parent
== '/' || getcwd(cwd
, sizeof(cwd
)) == NULL
)
443 asprintf(&this->dirname
, "%s/%s", parent
, name
);
447 asprintf(&this->dirname
, "%s/%s/%s", cwd
, parent
, name
);
451 mkdir(this->dirname
, PERME
);
453 this->dir
= open(this->dirname
, O_DIRECTORY
, PERME
);
456 DBG1("opening guest directory '%s' failed: %m", this->dirname
);
463 this->state
= GUEST_STOPPED
;
464 this->mconsole
= NULL
;
465 this->ifaces
= linked_list_create();
467 this->bootlog
= open_bootlog(this);
468 this->name
= strdup(name
);
475 * create a symlink to old called new in our working dir
477 static bool make_symlink(private_guest_t
*this, char *old
, char *new)
482 if (*old
== '/' || getcwd(cwd
, sizeof(cwd
)) == NULL
)
484 snprintf(buf
, sizeof(buf
), "%s", old
);
488 snprintf(buf
, sizeof(buf
), "%s/%s", cwd
, old
);
490 return symlinkat(buf
, this->dir
, new) == 0;
495 * create the guest instance, including required dirs and mounts
497 guest_t
*guest_create(char *parent
, char *name
, char *kernel
,
498 char *master
, int mem
)
500 private_guest_t
*this = guest_create_generic(parent
, name
, TRUE
);
507 if (!make_symlink(this, master
, MASTER_DIR
) ||
508 !make_symlink(this, kernel
, KERNEL_FILE
))
510 DBG1("creating master/kernel symlink failed: %m");
515 if (mkdirat(this->dir
, UNION_DIR
, PERME
) != 0 ||
516 mkdirat(this->dir
, DIFF_DIR
, PERME
) != 0)
518 DBG1("unable to create directories for '%s': %m", name
);
524 if (!savemem(this, mem
))
530 if (!mount_unionfs(this))
536 return &this->public;
540 * load an already created guest
542 guest_t
*guest_load(char *parent
, char *name
)
544 private_guest_t
*this = guest_create_generic(parent
, name
, FALSE
);
551 this->mem
= loadmem(this);
554 DBG1("unable to open memory configuration file: %m", name
);
559 if (!mount_unionfs(this))
565 return &this->public;