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 PID_FILE "pid"
45 #define KERNEL_FILE "linux"
46 #define LOG_FILE "boot.log"
47 #define NOTIFY_FILE "notify"
50 typedef struct private_guest_t private_guest_t
;
52 struct private_guest_t
{
53 /** implemented public interface */
55 /** name of the guest */
57 /** directory of guest */
59 /** directory name of guest */
61 /** amount of memory for guest, in MB */
63 /** pid of guest child process */
67 /** FUSE cowfs instance */
69 /** mconsole to control running UML */
71 /** list of interfaces attached to the guest */
72 linked_list_t
*ifaces
;
75 ENUM(guest_state_names
, GUEST_STOPPED
, GUEST_STOPPING
,
84 * Implementation of guest_t.get_name.
86 static char* get_name(private_guest_t
*this)
92 * Implementation of guest_t.create_iface.
94 static iface_t
* create_iface(private_guest_t
*this, char *name
)
96 enumerator_t
*enumerator
;
99 if (this->state
!= GUEST_RUNNING
)
101 DBG1("guest '%s' not running, unable to add interface", this->name
);
105 enumerator
= this->ifaces
->create_enumerator(this->ifaces
);
106 while (enumerator
->enumerate(enumerator
, (void**)&iface
))
108 if (streq(name
, iface
->get_guestif(iface
)))
110 DBG1("guest '%s' already has an interface '%s'", this->name
, name
);
111 enumerator
->destroy(enumerator
);
115 enumerator
->destroy(enumerator
);
117 iface
= iface_create(this->name
, name
, this->mconsole
);
120 this->ifaces
->insert_last(this->ifaces
, iface
);
126 * Implementation of guest_t.destroy_iface.
128 static void destroy_iface(private_guest_t
*this, iface_t
*iface
)
130 enumerator_t
*enumerator
;
133 enumerator
= this->ifaces
->create_enumerator(this->ifaces
);
134 while (enumerator
->enumerate(enumerator
, (void**)¤t
))
136 if (current
== iface
)
138 this->ifaces
->remove_at(this->ifaces
, enumerator
);
139 current
->destroy(current
);
143 enumerator
->destroy(enumerator
);
147 * Implementation of guest_t.create_iface_enumerator.
149 static enumerator_t
* create_iface_enumerator(private_guest_t
*this)
151 return this->ifaces
->create_enumerator(this->ifaces
);
155 * Implementation of guest_t.get_state.
157 static guest_state_t
get_state(private_guest_t
*this)
163 * Implementation of guest_t.get_pid.
165 static pid_t
get_pid(private_guest_t
*this)
171 * write format string to a buffer, and advance buffer position
173 static char* write_arg(char **pos
, size_t *left
, char *format
, ...)
179 va_start(args
, format
);
180 len
= vsnprintf(*pos
, *left
, format
, args
);
193 * Implementation of guest_t.stop.
195 static void stop(private_guest_t
*this, idle_function_t idle
)
197 if (this->state
!= GUEST_STOPPED
)
199 this->state
= GUEST_STOPPING
;
200 this->ifaces
->destroy_offset(this->ifaces
, offsetof(iface_t
, destroy
));
201 this->ifaces
= linked_list_create();
202 kill(this->pid
, SIGINT
);
203 while (this->state
!= GUEST_STOPPED
)
214 unlinkat(this->dir
, PID_FILE
, 0);
221 void savepid(private_guest_t
*this)
225 file
= fdopen(openat(this->dir
, PID_FILE
, O_RDWR
| O_CREAT
| O_TRUNC
,
229 fprintf(file
, "%d", this->pid
);
235 * Implementation of guest_t.start.
237 static bool start(private_guest_t
*this, invoke_function_t invoke
, void* data
,
238 idle_function_t idle
)
245 size_t left
= sizeof(buf
);
247 memset(args
, 0, sizeof(args
));
249 if (this->state
!= GUEST_STOPPED
)
251 DBG1("unable to start guest in state %N", guest_state_names
, this->state
);
254 this->state
= GUEST_STARTING
;
256 notify
= write_arg(&pos
, &left
, "%s/%s", this->dirname
, NOTIFY_FILE
);
258 args
[i
++] = write_arg(&pos
, &left
, "%s/%s", this->dirname
, KERNEL_FILE
);
259 args
[i
++] = write_arg(&pos
, &left
, "root=/dev/root");
260 args
[i
++] = write_arg(&pos
, &left
, "rootfstype=hostfs");
261 args
[i
++] = write_arg(&pos
, &left
, "rootflags=%s/%s", this->dirname
, UNION_DIR
);
262 args
[i
++] = write_arg(&pos
, &left
, "uml_dir=%s", this->dirname
);
263 args
[i
++] = write_arg(&pos
, &left
, "umid=%s", this->name
);
264 args
[i
++] = write_arg(&pos
, &left
, "mem=%dM", this->mem
);
265 args
[i
++] = write_arg(&pos
, &left
, "mconsole=notify:%s", notify
);
266 args
[i
++] = write_arg(&pos
, &left
, "con=null");
268 this->pid
= invoke(data
, &this->public, args
, i
);
271 this->state
= GUEST_STOPPED
;
277 this->mconsole
= mconsole_create(notify
, idle
);
278 if (this->mconsole
== NULL
)
280 DBG1("opening mconsole at '%s' failed, stopping guest", buf
);
285 this->state
= GUEST_RUNNING
;
290 * Implementation of guest_t.load_template.
292 static bool load_template(private_guest_t
*this, char *path
)
300 return this->cowfs
->set_overlay(this->cowfs
, NULL
);
303 len
= snprintf(dir
, sizeof(dir
), "%s/%s", path
, this->name
);
304 if (len
< 0 || len
>= sizeof(dir
))
308 if (access(dir
, F_OK
) != 0)
310 if (mkdir(dir
, PERME
) != 0)
312 DBG1("creating overlay for guest '%s' failed: %m", this->name
);
316 if (!this->cowfs
->set_overlay(this->cowfs
, dir
))
320 while (this->ifaces
->remove_last(this->ifaces
, (void**)&iface
) == SUCCESS
)
322 iface
->destroy(iface
);
328 * Implementation of guest_t.sigchild.
330 static void sigchild(private_guest_t
*this)
332 DESTROY_IF(this->mconsole
);
333 this->mconsole
= NULL
;
334 this->state
= GUEST_STOPPED
;
338 * umount the union filesystem
340 static bool umount_unionfs(private_guest_t
*this)
344 this->cowfs
->destroy(this->cowfs
);
352 * mount the union filesystem
354 static bool mount_unionfs(private_guest_t
*this)
356 char master
[PATH_MAX
];
358 char mount
[PATH_MAX
];
360 if (this->cowfs
== NULL
)
362 snprintf(master
, sizeof(master
), "%s/%s", this->dirname
, MASTER_DIR
);
363 snprintf(diff
, sizeof(diff
), "%s/%s", this->dirname
, DIFF_DIR
);
364 snprintf(mount
, sizeof(mount
), "%s/%s", this->dirname
, UNION_DIR
);
366 this->cowfs
= cowfs_create(master
, diff
, mount
);
376 * load memory configuration from file
378 int loadmem(private_guest_t
*this)
383 file
= fdopen(openat(this->dir
, MEMORY_FILE
, O_RDONLY
, PERM
), "r");
386 if (fscanf(file
, "%d", &mem
) <= 0)
396 * save memory configuration to file
398 bool savemem(private_guest_t
*this, int mem
)
403 file
= fdopen(openat(this->dir
, MEMORY_FILE
, O_RDWR
| O_CREAT
| O_TRUNC
,
407 if (fprintf(file
, "%d", mem
) > 0)
417 * Implementation of guest_t.destroy.
419 static void destroy(private_guest_t
*this)
422 umount_unionfs(this);
433 * generic guest constructor
435 static private_guest_t
*guest_create_generic(char *parent
, char *name
,
439 private_guest_t
*this = malloc_thing(private_guest_t
);
441 this->public.get_name
= (void*)get_name
;
442 this->public.get_pid
= (pid_t(*)(guest_t
*))get_pid
;
443 this->public.get_state
= (guest_state_t(*)(guest_t
*))get_state
;
444 this->public.create_iface
= (iface_t
*(*)(guest_t
*,char*))create_iface
;
445 this->public.destroy_iface
= (void(*)(guest_t
*,iface_t
*))destroy_iface
;
446 this->public.create_iface_enumerator
= (enumerator_t
*(*)(guest_t
*))create_iface_enumerator
;
447 this->public.start
= (void*)start
;
448 this->public.stop
= (void*)stop
;
449 this->public.load_template
= (bool(*)(guest_t
*, char *path
))load_template
;
450 this->public.sigchild
= (void(*)(guest_t
*))sigchild
;
451 this->public.destroy
= (void*)destroy
;
453 if (*parent
== '/' || getcwd(cwd
, sizeof(cwd
)) == NULL
)
455 asprintf(&this->dirname
, "%s/%s", parent
, name
);
459 asprintf(&this->dirname
, "%s/%s/%s", cwd
, parent
, name
);
463 mkdir(this->dirname
, PERME
);
465 this->dir
= open(this->dirname
, O_DIRECTORY
, PERME
);
468 DBG1("opening guest directory '%s' failed: %m", this->dirname
);
474 this->state
= GUEST_STOPPED
;
475 this->mconsole
= NULL
;
476 this->ifaces
= linked_list_create();
478 this->name
= strdup(name
);
485 * create a symlink to old called new in our working dir
487 static bool make_symlink(private_guest_t
*this, char *old
, char *new)
492 if (*old
== '/' || getcwd(cwd
, sizeof(cwd
)) == NULL
)
494 snprintf(buf
, sizeof(buf
), "%s", old
);
498 snprintf(buf
, sizeof(buf
), "%s/%s", cwd
, old
);
500 return symlinkat(buf
, this->dir
, new) == 0;
505 * create the guest instance, including required dirs and mounts
507 guest_t
*guest_create(char *parent
, char *name
, char *kernel
,
508 char *master
, int mem
)
510 private_guest_t
*this = guest_create_generic(parent
, name
, TRUE
);
517 if (!make_symlink(this, master
, MASTER_DIR
) ||
518 !make_symlink(this, kernel
, KERNEL_FILE
))
520 DBG1("creating master/kernel symlink failed: %m");
525 if (mkdirat(this->dir
, UNION_DIR
, PERME
) != 0 ||
526 mkdirat(this->dir
, DIFF_DIR
, PERME
) != 0)
528 DBG1("unable to create directories for '%s': %m", name
);
534 if (!savemem(this, mem
))
540 if (!mount_unionfs(this))
546 return &this->public;
550 * load an already created guest
552 guest_t
*guest_load(char *parent
, char *name
)
554 private_guest_t
*this = guest_create_generic(parent
, name
, FALSE
);
561 this->mem
= loadmem(this);
564 DBG1("unable to open memory configuration file: %m", name
);
569 if (!mount_unionfs(this))
575 return &this->public;