]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/dir.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
35 lxc_log_define(dir
, lxc
);
37 /* For a simple directory bind mount, we substitute the old container name and
40 int dir_clonepaths(struct lxc_storage
*orig
, struct lxc_storage
*new,
41 const char *oldname
, const char *cname
, const char *oldpath
,
42 const char *lxcpath
, int snap
, uint64_t newsize
,
43 struct lxc_conf
*conf
)
45 const char *src_no_prefix
;
50 ERROR("Directories cannot be snapshotted");
54 if (!orig
->dest
|| !orig
->src
)
57 len
= strlen(lxcpath
) + strlen(cname
) + strlen("rootfs") + 4 + 3;
58 new->src
= malloc(len
);
60 ERROR("Failed to allocate memory");
64 ret
= snprintf(new->src
, len
, "dir:%s/%s/rootfs", lxcpath
, cname
);
65 if (ret
< 0 || (size_t)ret
>= len
) {
66 ERROR("Failed to create string");
70 src_no_prefix
= lxc_storage_get_path(new->src
, new->type
);
71 new->dest
= strdup(src_no_prefix
);
73 ERROR("Failed to duplicate string \"%s\"", new->src
);
77 TRACE("Created new path \"%s\" for dir storage driver", new->dest
);
81 int dir_create(struct lxc_storage
*bdev
, const char *dest
, const char *n
,
82 struct bdev_specs
*specs
)
90 if (specs
&& specs
->dir
)
95 len
+= strlen(src
) + 1;
96 bdev
->src
= malloc(len
);
98 ERROR("Failed to allocate memory");
102 ret
= snprintf(bdev
->src
, len
, "dir:%s", src
);
103 if (ret
< 0 || (size_t)ret
>= len
) {
104 ERROR("Failed to create string");
108 bdev
->dest
= strdup(dest
);
110 ERROR("Failed to duplicate string \"%s\"", dest
);
114 ret
= mkdir_p(dest
, 0755);
116 ERROR("Failed to create directory \"%s\"", dest
);
119 TRACE("Created directory \"%s\"", dest
);
124 int dir_destroy(struct lxc_storage
*orig
)
129 src
= lxc_storage_get_path(orig
->src
, orig
->src
);
131 ret
= lxc_rmdir_onedev(src
, NULL
);
133 ERROR("Failed to delete \"%s\"", src
);
140 bool dir_detect(const char *path
)
145 if (!strncmp(path
, "dir:", 4))
148 ret
= stat(path
, &statbuf
);
149 if (ret
== -1 && errno
== EPERM
) {
150 SYSERROR("dir_detect: failed to look at \"%s\"", path
);
154 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
160 int dir_mount(struct lxc_storage
*bdev
)
163 unsigned long mflags
= 0, mntflags
= 0, pflags
= 0;
167 if (strcmp(bdev
->type
, "dir"))
170 if (!bdev
->src
|| !bdev
->dest
)
173 ret
= parse_mntopts(bdev
->mntopts
, &mntflags
, &mntdata
);
175 ERROR("Failed to parse mount options \"%s\"", bdev
->mntopts
);
180 ret
= parse_propagationopts(bdev
->mntopts
, &pflags
);
182 ERROR("Failed to parse propagation options \"%s\"", bdev
->mntopts
);
187 src
= lxc_storage_get_path(bdev
->src
, bdev
->type
);
189 ret
= mount(src
, bdev
->dest
, "bind", MS_BIND
| MS_REC
| mntflags
| pflags
, mntdata
);
190 if ((0 == ret
) && (mntflags
& MS_RDONLY
)) {
191 DEBUG("Remounting \"%s\" on \"%s\" readonly",
192 src
? src
: "(none)", bdev
->dest
? bdev
->dest
: "(none)");
193 mflags
= add_required_remount_flags(src
, bdev
->dest
, MS_BIND
| MS_REC
| mntflags
| pflags
| MS_REMOUNT
);
194 ret
= mount(src
, bdev
->dest
, "bind", mflags
, mntdata
);
198 SYSERROR("Failed to mount \"%s\" on \"%s\"", src
, bdev
->dest
);
203 TRACE("Mounted \"%s\" on \"%s\"", src
, bdev
->dest
);
208 int dir_umount(struct lxc_storage
*bdev
)
210 if (strcmp(bdev
->type
, "dir"))
213 if (!bdev
->src
|| !bdev
->dest
)
216 return umount(bdev
->dest
);