]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/snapshot.c
3 * Copyright © 2013 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2013 Canonical Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <lxc/lxccontainer.h>
28 #define MYNAME "snapxxx1"
29 #define MYNAME2 "snapxxx3"
30 #define RESTNAME "snapxxx2"
32 static void try_to_remove(void)
34 struct lxc_container
*c
;
35 c
= lxc_container_new(RESTNAME
, NULL
);
37 c
->snapshot_destroy_all(c
);
42 c
= lxc_container_new(MYNAME2
, NULL
);
44 c
->destroy_with_snapshots(c
);
47 c
= lxc_container_new(MYNAME
, NULL
);
49 c
->snapshot_destroy_all(c
);
56 int main(int argc
, char *argv
[])
58 struct lxc_container
*c
, *c2
= NULL
;
59 char *template = "busybox";
65 c
= lxc_container_new(MYNAME
, NULL
);
67 fprintf(stderr
, "%s: %d: failed to load first container\n", __FILE__
, __LINE__
);
71 if (c
->is_defined(c
)) {
72 fprintf(stderr
, "%d: %s thought it was defined\n", __LINE__
, MYNAME
);
73 (void) c
->destroy_with_snapshots(c
);
75 if (!c
->set_config_item(c
, "lxc.network.type", "empty")) {
76 fprintf(stderr
, "%s: %d: failed to set network type\n", __FILE__
, __LINE__
);
79 c
->save_config(c
, NULL
);
80 if (!c
->createl(c
, template, NULL
, NULL
, 0, NULL
)) {
81 fprintf(stderr
, "%s: %d: failed to create %s container\n", __FILE__
, __LINE__
, template);
84 c
->load_config(c
, NULL
);
86 if (c
->snapshot(c
, NULL
) != 0) {
87 fprintf(stderr
, "%s: %d: failed to create snapshot\n", __FILE__
, __LINE__
);
91 // rootfs should be ${lxcpath}${lxcname}/snaps/snap0/rootfs
95 snprintf(path
, 1024, "%s/%s/snaps/snap0/rootfs", lxc_get_global_config_item("lxc.lxcpath"), MYNAME
);
96 ret
= stat(path
, &sb
);
98 fprintf(stderr
, "%s: %d: snapshot was not actually created\n", __FILE__
, __LINE__
);
102 struct lxc_snapshot
*s
;
105 n
= c
->snapshot_list(c
, &s
);
107 fprintf(stderr
, "%s: %d: failed listing containers\n", __FILE__
, __LINE__
);
110 if (strcmp(s
->name
, "snap0") != 0) {
111 fprintf(stderr
, "%s: %d: snapshot had bad name\n", __FILE__
, __LINE__
);
114 for (i
=0; i
<n
; i
++) {
119 if (!c
->snapshot_restore(c
, "snap0", RESTNAME
)) {
120 fprintf(stderr
, "%s: %d: failed to restore snapshot\n", __FILE__
, __LINE__
);
124 if (!c
->snapshot_destroy(c
, "snap0")) {
125 fprintf(stderr
, "%s: %d: failed to destroy snapshot\n", __FILE__
, __LINE__
);
129 c2
= lxc_container_new(RESTNAME
, NULL
);
130 if (!c2
|| !c2
->is_defined(c2
)) {
131 fprintf(stderr
, "%s: %d: external snapshot restore failed\n", __FILE__
, __LINE__
);
134 lxc_container_put(c2
);
136 c2
= c
->clone(c
, MYNAME2
, NULL
, LXC_CLONE_SNAPSHOT
, "overlayfs", NULL
, 0, NULL
);
138 fprintf(stderr
, "%d: %s overlayfs clone failed\n", __LINE__
, MYNAME2
);
142 if (c2
->snapshot(c2
, NULL
) != 0) {
143 fprintf(stderr
, "%s: %d: failed to create snapshot\n", __FILE__
, __LINE__
);
147 n
= c2
->snapshot_list(c2
, &s
);
149 fprintf(stderr
, "%s: %d: failed listing containers\n", __FILE__
, __LINE__
);
152 if (strcmp(s
->name
, "snap0") != 0) {
153 fprintf(stderr
, "%s: %d: snapshot had bad name\n", __FILE__
, __LINE__
);
156 for (i
=0; i
<n
; i
++) {
161 if (!c2
->snapshot_restore(c2
, "snap0", NULL
)) {
162 fprintf(stderr
, "%s: %d: failed to restore overlayfs snapshot\n", __FILE__
, __LINE__
);
166 if (!c2
->snapshot_destroy(c2
, "snap0")) {
167 fprintf(stderr
, "%s: %d: failed to destroy overlayfs snapshot\n", __FILE__
, __LINE__
);
172 lxc_container_put(c
);
175 printf("All tests passed\n");
178 lxc_container_put(c
);
181 fprintf(stderr
, "Exiting on error\n");