]>
Commit | Line | Data |
---|---|---|
f5dd1d53 SH |
1 | /* liblxcapi |
2 | * | |
8cd80b50 SG |
3 | * Copyright © 2013 Serge Hallyn <serge.hallyn@ubuntu.com>. |
4 | * Copyright © 2013 Canonical Ltd. | |
f5dd1d53 SH |
5 | * |
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. | |
9 | * | |
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. | |
14 | * | |
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. | |
18 | */ | |
948955a2 | 19 | #include <lxc/lxccontainer.h> |
f5dd1d53 SH |
20 | |
21 | #include <errno.h> | |
22 | #include <stdlib.h> | |
4012c891 | 23 | #include <stdio.h> |
95ee490b SG |
24 | #include <string.h> |
25 | #include <sys/stat.h> | |
f2363e38 | 26 | #include "lxc/lxc.h" |
f5dd1d53 SH |
27 | |
28 | #define MYNAME "snapxxx1" | |
de269ee8 | 29 | #define MYNAME2 "snapxxx3" |
f5dd1d53 SH |
30 | #define RESTNAME "snapxxx2" |
31 | ||
0b98289e | 32 | static void try_to_remove(void) |
f5dd1d53 SH |
33 | { |
34 | struct lxc_container *c; | |
f5dd1d53 SH |
35 | c = lxc_container_new(RESTNAME, NULL); |
36 | if (c) { | |
18aa217b | 37 | c->snapshot_destroy_all(c); |
f5dd1d53 SH |
38 | if (c->is_defined(c)) |
39 | c->destroy(c); | |
40 | lxc_container_put(c); | |
41 | } | |
de269ee8 SH |
42 | c = lxc_container_new(MYNAME2, NULL); |
43 | if (c) { | |
18aa217b | 44 | c->destroy_with_snapshots(c); |
de269ee8 SH |
45 | lxc_container_put(c); |
46 | } | |
f5dd1d53 SH |
47 | c = lxc_container_new(MYNAME, NULL); |
48 | if (c) { | |
18aa217b | 49 | c->snapshot_destroy_all(c); |
f5dd1d53 SH |
50 | if (c->is_defined(c)) |
51 | c->destroy(c); | |
52 | lxc_container_put(c); | |
53 | } | |
54 | } | |
55 | ||
56 | int main(int argc, char *argv[]) | |
57 | { | |
de269ee8 | 58 | struct lxc_container *c, *c2 = NULL; |
f5dd1d53 SH |
59 | char *template = "busybox"; |
60 | ||
61 | if (argc > 1) | |
62 | template = argv[1]; | |
63 | ||
64 | try_to_remove(); | |
65 | c = lxc_container_new(MYNAME, NULL); | |
66 | if (!c) { | |
67 | fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__); | |
68 | exit(1); | |
69 | } | |
70 | ||
71 | if (c->is_defined(c)) { | |
72 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
18aa217b | 73 | (void) c->destroy_with_snapshots(c); |
f5dd1d53 SH |
74 | } |
75 | if (!c->set_config_item(c, "lxc.network.type", "empty")) { | |
76 | fprintf(stderr, "%s: %d: failed to set network type\n", __FILE__, __LINE__); | |
77 | goto err; | |
78 | } | |
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); | |
82 | goto err; | |
83 | } | |
84 | c->load_config(c, NULL); | |
85 | ||
86 | if (c->snapshot(c, NULL) != 0) { | |
de269ee8 | 87 | fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__); |
f5dd1d53 SH |
88 | goto err; |
89 | } | |
90 | ||
18aa217b | 91 | // rootfs should be ${lxcpath}${lxcname}/snaps/snap0/rootfs |
f5dd1d53 SH |
92 | struct stat sb; |
93 | int ret; | |
94 | char path[1024]; | |
18aa217b | 95 | snprintf(path, 1024, "%s/%s/snaps/snap0/rootfs", lxc_get_global_config_item("lxc.lxcpath"), MYNAME); |
f5dd1d53 SH |
96 | ret = stat(path, &sb); |
97 | if (ret != 0) { | |
98 | fprintf(stderr, "%s: %d: snapshot was not actually created\n", __FILE__, __LINE__); | |
99 | goto err; | |
100 | } | |
101 | ||
102 | struct lxc_snapshot *s; | |
103 | int i, n; | |
104 | ||
105 | n = c->snapshot_list(c, &s); | |
106 | if (n < 1) { | |
107 | fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__); | |
108 | goto err; | |
109 | } | |
110 | if (strcmp(s->name, "snap0") != 0) { | |
111 | fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__); | |
112 | goto err; | |
113 | } | |
114 | for (i=0; i<n; i++) { | |
115 | s[i].free(&s[i]); | |
116 | } | |
117 | free(s); | |
118 | ||
119 | if (!c->snapshot_restore(c, "snap0", RESTNAME)) { | |
120 | fprintf(stderr, "%s: %d: failed to restore snapshot\n", __FILE__, __LINE__); | |
121 | goto err; | |
122 | } | |
123 | ||
58b61f6d ÇO |
124 | if (!c->snapshot_destroy(c, "snap0")) { |
125 | fprintf(stderr, "%s: %d: failed to destroy snapshot\n", __FILE__, __LINE__); | |
126 | goto err; | |
127 | } | |
128 | ||
e60e630c SH |
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__); | |
132 | goto err; | |
133 | } | |
134 | lxc_container_put(c2); | |
de269ee8 SH |
135 | |
136 | c2 = c->clone(c, MYNAME2, NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL); | |
137 | if (!c2) { | |
138 | fprintf(stderr, "%d: %s overlayfs clone failed\n", __LINE__, MYNAME2); | |
139 | goto good; | |
140 | } | |
141 | ||
142 | if (c2->snapshot(c2, NULL) != 0) { | |
143 | fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__); | |
144 | goto err; | |
145 | } | |
146 | ||
147 | n = c2->snapshot_list(c2, &s); | |
148 | if (n < 1) { | |
149 | fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__); | |
150 | goto err; | |
151 | } | |
152 | if (strcmp(s->name, "snap0") != 0) { | |
153 | fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__); | |
154 | goto err; | |
155 | } | |
156 | for (i=0; i<n; i++) { | |
157 | s[i].free(&s[i]); | |
158 | } | |
159 | free(s); | |
160 | ||
161 | if (!c2->snapshot_restore(c2, "snap0", NULL)) { | |
162 | fprintf(stderr, "%s: %d: failed to restore overlayfs snapshot\n", __FILE__, __LINE__); | |
163 | goto err; | |
164 | } | |
165 | ||
166 | if (!c2->snapshot_destroy(c2, "snap0")) { | |
167 | fprintf(stderr, "%s: %d: failed to destroy overlayfs snapshot\n", __FILE__, __LINE__); | |
168 | goto err; | |
169 | } | |
170 | ||
de269ee8 | 171 | good: |
f5dd1d53 | 172 | lxc_container_put(c); |
58b61f6d | 173 | try_to_remove(); |
f5dd1d53 | 174 | |
58b61f6d ÇO |
175 | printf("All tests passed\n"); |
176 | exit(0); | |
f5dd1d53 SH |
177 | err: |
178 | lxc_container_put(c); | |
f5dd1d53 | 179 | try_to_remove(); |
58b61f6d ÇO |
180 | |
181 | fprintf(stderr, "Exiting on error\n"); | |
f5dd1d53 SH |
182 | exit(1); |
183 | } |