3 * Copyright © 2021 Christian Brauner <christian.brauner@ubuntu.com>.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/types.h>
33 #include "lxccontainer.h"
34 #include "attach_options.h"
37 #include <sys/statvfs.h>
43 static int is_read_only(const char *path
)
49 ret
= statvfs(path
, &sb
);
53 return (sb
.f_flag
& MS_RDONLY
) > 0;
59 static int sys_mixed(void *payload
)
63 ret
= is_read_only("/sys");
64 if (ret
== -EOPNOTSUPP
)
70 if (is_read_only("/sys/devices/virtual/net"))
76 int main(int argc
, char *argv
[])
78 int fret
= EXIT_FAILURE
;
79 lxc_attach_options_t attach_options
= LXC_ATTACH_OPTIONS_DEFAULT
;
82 struct lxc_container
*c
;
84 c
= lxc_container_new("sys-mixed", NULL
);
86 lxc_error("%s", "Failed to create container \"sys-mixed\"");
90 if (c
->is_defined(c
)) {
91 lxc_error("%s\n", "Container \"sys-mixed\" is defined");
95 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
96 lxc_error("%s\n", "Failed to create busybox container \"sys-mixed\"");
100 if (!c
->is_defined(c
)) {
101 lxc_error("%s\n", "Container \"sys-mixed\" is not defined");
107 if (!c
->load_config(c
, NULL
)) {
108 lxc_error("%s\n", "Failed to load config for container \"sys-mixed\"");
112 if (!c
->set_config_item(c
, "lxc.mount.auto", "sys:mixed")) {
113 lxc_error("%s\n", "Failed to set config item \"lxc.mount.auto=sys:mixed\"");
117 if (!c
->want_daemonize(c
, true)) {
118 lxc_error("%s\n", "Failed to mark container \"sys-mixed\" daemonized");
122 if (!c
->startl(c
, 0, NULL
)) {
123 lxc_error("%s\n", "Failed to start container \"sys-mixed\" daemonized");
127 /* Leave some time for the container to write something to the log. */
130 ret
= c
->attach(c
, sys_mixed
, NULL
, &attach_options
, &pid
);
132 lxc_error("%s\n", "Failed to run function in container \"sys-mixed\"");
136 ret
= wait_for_pid(pid
);
138 lxc_error("%s\n", "Failed to run function in container \"sys-mixed\"");
145 if (c
->is_running(c
) && !c
->stop(c
))
146 lxc_error("%s\n", "Failed to stop container \"sys-mixed\"");
149 lxc_error("%s\n", "Failed to destroy container \"sys-mixed\"");
152 lxc_container_put(c
);