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.
21 #define __STDC_FORMAT_MACROS
32 #include <sys/types.h>
35 #include "lxccontainer.h"
36 #include "attach_options.h"
39 #include <sys/statvfs.h>
45 static int has_mount_properties(const char *path
, unsigned int flags
)
51 ret
= statvfs(path
, &sb
);
55 if ((sb
.f_flag
& flags
) == flags
)
65 static int rootfs_options(void *payload
)
69 ret
= has_mount_properties("/",
74 if (ret
== -EOPNOTSUPP
)
83 int main(int argc
, char *argv
[])
85 int fret
= EXIT_FAILURE
;
86 lxc_attach_options_t attach_options
= LXC_ATTACH_OPTIONS_DEFAULT
;
89 struct lxc_container
*c
;
91 c
= lxc_container_new("rootfs-options", NULL
);
93 lxc_error("%s", "Failed to create container \"rootfs-options\"");
97 if (c
->is_defined(c
)) {
98 lxc_error("%s\n", "Container \"rootfs-options\" is defined");
102 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
103 lxc_error("%s\n", "Failed to create busybox container \"rootfs-options\"");
107 if (!c
->is_defined(c
)) {
108 lxc_error("%s\n", "Container \"rootfs-options\" is not defined");
114 if (!c
->set_config_item(c
, "lxc.rootfs.options", "nodev,nosuid,ro")) {
115 lxc_error("%s\n", "Failed to set config item \"lxc.mount.auto=sys:mixed\"");
119 if (!c
->load_config(c
, NULL
)) {
120 lxc_error("%s\n", "Failed to load config for container \"rootfs-options\"");
124 if (!c
->want_daemonize(c
, true)) {
125 lxc_error("%s\n", "Failed to mark container \"rootfs-options\" daemonized");
129 if (!c
->startl(c
, 0, NULL
)) {
130 lxc_error("%s\n", "Failed to start container \"rootfs-options\" daemonized");
134 /* Leave some time for the container to write something to the log. */
137 ret
= c
->attach(c
, rootfs_options
, NULL
, &attach_options
, &pid
);
139 lxc_error("%s\n", "Failed to run function in container \"rootfs-options\"");
143 ret
= wait_for_pid(pid
);
145 lxc_error("%s\n", "Function \"rootfs-options\" failed");
152 if (c
->is_running(c
) && !c
->stop(c
))
153 lxc_error("%s\n", "Failed to stop container \"rootfs-options\"");
156 lxc_error("%s\n", "Failed to destroy container \"rootfs-options\"");
159 lxc_container_put(c
);