]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/snapshot.c
Merge pull request #1649 from 0x0916/2017-06-27/wipe-lxc-network-references
[mirror_lxc.git] / src / tests / snapshot.c
1 /* liblxcapi
2 *
3 * Copyright © 2013 Serge Hallyn <serge.hallyn@ubuntu.com>.
4 * Copyright © 2013 Canonical Ltd.
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 */
19 #include <lxc/lxccontainer.h>
20
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include "lxc/lxc.h"
27
28 #define MYNAME "snapxxx1"
29 #define MYNAME2 "snapxxx3"
30 #define RESTNAME "snapxxx2"
31
32 static void try_to_remove(void)
33 {
34 struct lxc_container *c;
35 c = lxc_container_new(RESTNAME, NULL);
36 if (c) {
37 c->snapshot_destroy_all(c);
38 if (c->is_defined(c))
39 c->destroy(c);
40 lxc_container_put(c);
41 }
42 c = lxc_container_new(MYNAME2, NULL);
43 if (c) {
44 c->destroy_with_snapshots(c);
45 lxc_container_put(c);
46 }
47 c = lxc_container_new(MYNAME, NULL);
48 if (c) {
49 c->snapshot_destroy_all(c);
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 {
58 struct lxc_container *c, *c2 = NULL;
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);
73 (void) c->destroy_with_snapshots(c);
74 }
75 if (!c->set_config_item(c, "lxc.net.0.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) {
87 fprintf(stderr, "%s: %d: failed to create snapshot\n", __FILE__, __LINE__);
88 goto err;
89 }
90
91 // rootfs should be ${lxcpath}${lxcname}/snaps/snap0/rootfs
92 struct stat sb;
93 int ret;
94 char path[1024];
95 snprintf(path, 1024, "%s/%s/snaps/snap0/rootfs", lxc_get_global_config_item("lxc.lxcpath"), MYNAME);
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
124 if (!c->snapshot_destroy(c, "snap0")) {
125 fprintf(stderr, "%s: %d: failed to destroy snapshot\n", __FILE__, __LINE__);
126 goto err;
127 }
128
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);
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
171 good:
172 lxc_container_put(c);
173 try_to_remove();
174
175 printf("All tests passed\n");
176 exit(0);
177 err:
178 lxc_container_put(c);
179 try_to_remove();
180
181 fprintf(stderr, "Exiting on error\n");
182 exit(1);
183 }