]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/rsync.c
tree-wide: use "ptmx" and "pts" as terminal terms
[mirror_lxc.git] / src / lxc / storage / rsync.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <grp.h>
7 #include <sched.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mount.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15
16 #include "config.h"
17 #include "log.h"
18 #include "rsync.h"
19 #include "storage.h"
20 #include "syscall_wrappers.h"
21 #include "utils.h"
22
23 lxc_log_define(rsync, lxc);
24
25 int lxc_storage_rsync_exec_wrapper(void *data)
26 {
27 struct rsync_data *arg = data;
28 return lxc_rsync(arg);
29 }
30
31 int lxc_rsync_exec_wrapper(void *data)
32 {
33 struct rsync_data_char *args = data;
34
35 if (!lxc_switch_uid_gid(0, 0))
36 return -1;
37
38 if (!lxc_setgroups(0, NULL))
39 return -1;
40
41 return lxc_rsync_exec(args->src, args->dest);
42 }
43
44 int lxc_rsync_exec(const char *src, const char *dest)
45 {
46 int ret;
47 size_t l;
48 char *s;
49
50 l = strlen(src) + 2;
51 s = malloc(l);
52 if (!s)
53 return -1;
54
55 ret = snprintf(s, l, "%s", src);
56 if (ret < 0 || (size_t)ret >= l) {
57 free(s);
58 return -1;
59 }
60
61 s[l - 2] = '/';
62 s[l - 1] = '\0';
63
64 execlp("rsync", "rsync", "-aHXS", "--delete", s, dest, (char *)NULL);
65 free(s);
66 return -1;
67 }
68
69 int lxc_rsync(struct rsync_data *data)
70 {
71 int ret;
72 const char *dest, *src;
73 struct lxc_storage *orig = data->orig, *new = data->new;
74
75 ret = unshare(CLONE_NEWNS);
76 if (ret < 0) {
77 SYSERROR("Failed to unshare CLONE_NEWNS");
78 return -1;
79 }
80
81 ret = detect_shared_rootfs();
82 if (ret) {
83 ret = mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL);
84 if (ret < 0)
85 SYSERROR("Failed to make \"/\" a slave mount");
86 }
87
88 ret = orig->ops->mount(orig);
89 if (ret < 0) {
90 ERROR("Failed mounting \"%s\" on \"%s\"", orig->src, orig->dest);
91 return -1;
92 }
93
94 ret = new->ops->mount(new);
95 if (ret < 0) {
96 ERROR("Failed mounting \"%s\" onto \"%s\"", new->src, new->dest);
97 return -1;
98 }
99
100 if (!lxc_switch_uid_gid(0, 0))
101 return -1;
102
103 if (!lxc_setgroups(0, NULL))
104 return -1;
105
106 src = lxc_storage_get_path(orig->dest, orig->type);
107 dest = lxc_storage_get_path(new->dest, new->type);
108
109 ret = lxc_rsync_exec(src, dest);
110 if (ret < 0) {
111 ERROR("Failed to rsync from \"%s\" into \"%s\"", src, dest);
112 return -1;
113 }
114
115 return 0;
116 }