]>
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; | |
63f769f9 | 35 | |
f5dd1d53 SH |
36 | c = lxc_container_new(RESTNAME, NULL); |
37 | if (c) { | |
18aa217b | 38 | c->snapshot_destroy_all(c); |
f5dd1d53 SH |
39 | if (c->is_defined(c)) |
40 | c->destroy(c); | |
63f769f9 | 41 | |
f5dd1d53 SH |
42 | lxc_container_put(c); |
43 | } | |
63f769f9 | 44 | |
de269ee8 SH |
45 | c = lxc_container_new(MYNAME2, NULL); |
46 | if (c) { | |
18aa217b | 47 | c->destroy_with_snapshots(c); |
de269ee8 SH |
48 | lxc_container_put(c); |
49 | } | |
63f769f9 | 50 | |
f5dd1d53 SH |
51 | c = lxc_container_new(MYNAME, NULL); |
52 | if (c) { | |
18aa217b | 53 | c->snapshot_destroy_all(c); |
f5dd1d53 SH |
54 | if (c->is_defined(c)) |
55 | c->destroy(c); | |
63f769f9 | 56 | |
f5dd1d53 SH |
57 | lxc_container_put(c); |
58 | } | |
59 | } | |
60 | ||
61 | int main(int argc, char *argv[]) | |
62 | { | |
cf223131 CB |
63 | int i, n, ret; |
64 | char path[1024]; | |
65 | struct stat sb; | |
66 | struct lxc_snapshot *s; | |
de269ee8 | 67 | struct lxc_container *c, *c2 = NULL; |
f5dd1d53 SH |
68 | char *template = "busybox"; |
69 | ||
70 | if (argc > 1) | |
71 | template = argv[1]; | |
72 | ||
73 | try_to_remove(); | |
63f769f9 | 74 | |
f5dd1d53 SH |
75 | c = lxc_container_new(MYNAME, NULL); |
76 | if (!c) { | |
77 | fprintf(stderr, "%s: %d: failed to load first container\n", __FILE__, __LINE__); | |
78 | exit(1); | |
79 | } | |
80 | ||
81 | if (c->is_defined(c)) { | |
82 | fprintf(stderr, "%d: %s thought it was defined\n", __LINE__, MYNAME); | |
18aa217b | 83 | (void) c->destroy_with_snapshots(c); |
f5dd1d53 | 84 | } |
63f769f9 | 85 | |
7fa3f2e9 | 86 | if (!c->set_config_item(c, "lxc.net.0.type", "empty")) { |
f5dd1d53 SH |
87 | fprintf(stderr, "%s: %d: failed to set network type\n", __FILE__, __LINE__); |
88 | goto err; | |
89 | } | |
63f769f9 | 90 | |
f5dd1d53 | 91 | c->save_config(c, NULL); |
63f769f9 | 92 | |
f5dd1d53 SH |
93 | if (!c->createl(c, template, NULL, NULL, 0, NULL)) { |
94 | fprintf(stderr, "%s: %d: failed to create %s container\n", __FILE__, __LINE__, template); | |
95 | goto err; | |
96 | } | |
63f769f9 | 97 | |
f5dd1d53 SH |
98 | c->load_config(c, NULL); |
99 | ||
100 | if (c->snapshot(c, NULL) != 0) { | |
de269ee8 | 101 | fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__); |
f5dd1d53 SH |
102 | goto err; |
103 | } | |
104 | ||
18aa217b | 105 | // rootfs should be ${lxcpath}${lxcname}/snaps/snap0/rootfs |
cf223131 CB |
106 | ret = snprintf(path, 1024, "%s/%s/snaps/snap0/rootfs", lxc_get_global_config_item("lxc.lxcpath"), MYNAME); |
107 | if (ret < 0 || (size_t)ret >= 1024) { | |
108 | fprintf(stderr, "%s: %d: failed to create string\n", __FILE__, __LINE__); | |
109 | goto err; | |
110 | } | |
111 | ||
f5dd1d53 SH |
112 | ret = stat(path, &sb); |
113 | if (ret != 0) { | |
114 | fprintf(stderr, "%s: %d: snapshot was not actually created\n", __FILE__, __LINE__); | |
115 | goto err; | |
116 | } | |
117 | ||
f5dd1d53 SH |
118 | n = c->snapshot_list(c, &s); |
119 | if (n < 1) { | |
120 | fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__); | |
121 | goto err; | |
122 | } | |
63f769f9 | 123 | |
f5dd1d53 SH |
124 | if (strcmp(s->name, "snap0") != 0) { |
125 | fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__); | |
126 | goto err; | |
127 | } | |
63f769f9 | 128 | |
129 | for (i=0; i<n; i++) | |
f5dd1d53 | 130 | s[i].free(&s[i]); |
f5dd1d53 SH |
131 | free(s); |
132 | ||
133 | if (!c->snapshot_restore(c, "snap0", RESTNAME)) { | |
134 | fprintf(stderr, "%s: %d: failed to restore snapshot\n", __FILE__, __LINE__); | |
135 | goto err; | |
136 | } | |
137 | ||
58b61f6d ÇO |
138 | if (!c->snapshot_destroy(c, "snap0")) { |
139 | fprintf(stderr, "%s: %d: failed to destroy snapshot\n", __FILE__, __LINE__); | |
140 | goto err; | |
141 | } | |
142 | ||
e60e630c SH |
143 | c2 = lxc_container_new(RESTNAME, NULL); |
144 | if (!c2 || !c2->is_defined(c2)) { | |
145 | fprintf(stderr, "%s: %d: external snapshot restore failed\n", __FILE__, __LINE__); | |
146 | goto err; | |
147 | } | |
148 | lxc_container_put(c2); | |
de269ee8 SH |
149 | |
150 | c2 = c->clone(c, MYNAME2, NULL, LXC_CLONE_SNAPSHOT, "overlayfs", NULL, 0, NULL); | |
151 | if (!c2) { | |
152 | fprintf(stderr, "%d: %s overlayfs clone failed\n", __LINE__, MYNAME2); | |
153 | goto good; | |
154 | } | |
155 | ||
156 | if (c2->snapshot(c2, NULL) != 0) { | |
157 | fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__); | |
158 | goto err; | |
159 | } | |
160 | ||
161 | n = c2->snapshot_list(c2, &s); | |
162 | if (n < 1) { | |
163 | fprintf(stderr, "%s: %d: failed listing containers\n", __FILE__, __LINE__); | |
164 | goto err; | |
165 | } | |
63f769f9 | 166 | |
de269ee8 SH |
167 | if (strcmp(s->name, "snap0") != 0) { |
168 | fprintf(stderr, "%s: %d: snapshot had bad name\n", __FILE__, __LINE__); | |
169 | goto err; | |
170 | } | |
63f769f9 | 171 | |
172 | for (i=0; i<n; i++) | |
de269ee8 | 173 | s[i].free(&s[i]); |
de269ee8 SH |
174 | free(s); |
175 | ||
176 | if (!c2->snapshot_restore(c2, "snap0", NULL)) { | |
177 | fprintf(stderr, "%s: %d: failed to restore overlayfs snapshot\n", __FILE__, __LINE__); | |
178 | goto err; | |
179 | } | |
180 | ||
181 | if (!c2->snapshot_destroy(c2, "snap0")) { | |
182 | fprintf(stderr, "%s: %d: failed to destroy overlayfs snapshot\n", __FILE__, __LINE__); | |
183 | goto err; | |
184 | } | |
185 | ||
de269ee8 | 186 | good: |
f5dd1d53 | 187 | lxc_container_put(c); |
58b61f6d | 188 | try_to_remove(); |
f5dd1d53 | 189 | |
58b61f6d ÇO |
190 | printf("All tests passed\n"); |
191 | exit(0); | |
63f769f9 | 192 | |
f5dd1d53 SH |
193 | err: |
194 | lxc_container_put(c); | |
f5dd1d53 | 195 | try_to_remove(); |
58b61f6d ÇO |
196 | |
197 | fprintf(stderr, "Exiting on error\n"); | |
f5dd1d53 SH |
198 | exit(1); |
199 | } |