/
rsync.go
463 lines (380 loc) · 10.2 KB
/
rsync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package rsync
import (
"bytes"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"time"
"github.com/pborman/uuid"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/ioprogress"
"github.com/lxc/lxd/shared/linux"
"github.com/lxc/lxd/shared/logger"
"github.com/lxc/lxd/shared/version"
)
// Debug controls additional debugging in rsync output.
var Debug bool
// RunWrapper is an optional function that's used to wrap rsync, useful for confinement like AppArmor.
var RunWrapper func(cmd *exec.Cmd, source string, destination string) (func(), error)
// rsync is a wrapper for the rsync command which will respect RunWrapper.
func rsync(args ...string) (string, error) {
if len(args) < 2 {
return "", fmt.Errorf("rsync call expects a minimum of two arguments (source and destination)")
}
// Setup the command.
cmd := exec.Command("rsync", args...)
var stderr bytes.Buffer
cmd.Stderr = &stderr
var stdout bytes.Buffer
cmd.Stdout = &stdout
// Call the wrapper if defined.
if RunWrapper != nil {
source := args[len(args)-2]
destination := args[len(args)-1]
cleanup, err := RunWrapper(cmd, source, destination)
if err != nil {
return "", err
}
defer cleanup()
}
// Run the command.
err := cmd.Run()
if err != nil {
return stdout.String(), shared.NewRunError("rsync", args, err, &stdout, &stderr)
}
return stdout.String(), nil
}
// LocalCopy copies a directory using rsync (with the --devices option).
func LocalCopy(source string, dest string, bwlimit string, xattrs bool, rsyncArgs ...string) (string, error) {
err := os.MkdirAll(dest, 0755)
if err != nil {
return "", err
}
rsyncVerbosity := "-q"
if Debug {
rsyncVerbosity = "-vi"
}
args := []string{
"-a",
"-HA",
"--sparse",
"--devices",
"--delete",
"--checksum",
"--numeric-ids",
}
if xattrs {
args = append(args, "--xattrs")
if AtLeast("3.1.3") {
args = append(args, "--filter=-x security.selinux")
}
}
if bwlimit != "" {
args = append(args, "--bwlimit", bwlimit)
}
if len(rsyncArgs) > 0 {
args = append(args, rsyncArgs...)
}
args = append(args,
rsyncVerbosity,
shared.AddSlash(source),
dest)
msg, err := rsync(args...)
if err != nil {
runError, ok := err.(shared.RunError)
if ok {
exitError, ok := runError.Unwrap().(*exec.ExitError)
if ok {
if exitError.ExitCode() == 24 {
return msg, nil
}
}
}
return msg, err
}
return msg, nil
}
func sendSetup(name string, path string, bwlimit string, execPath string, features []string, rsyncArgs ...string) (*exec.Cmd, net.Conn, io.ReadCloser, error) {
/*
* The way rsync works, it invokes a subprocess that does the actual
* talking (given to it by a -E argument). Since there isn't an easy
* way for us to capture this process' stdin/stdout, we just use netcat
* and write to/from a unix socket.
*
* In principle we don't need this socket. It seems to me that some
* clever invocation of rsync --server --sender and usage of that
* process' stdin/stdout could work around the need for this socket,
* but I couldn't get it to work. Another option would be to look at
* the spawned process' first child and read/write from its
* stdin/stdout, but that also seemed messy. In any case, this seems to
* work just fine.
*/
auds := fmt.Sprintf("@lxd/%s", uuid.New())
// We simply copy a part of the uuid if it's longer than the allowed
// maximum. That should be safe enough for our purposes.
if len(auds) > linux.ABSTRACT_UNIX_SOCK_LEN-1 {
auds = auds[:linux.ABSTRACT_UNIX_SOCK_LEN-1]
}
l, err := net.Listen("unix", auds)
if err != nil {
return nil, nil, nil, err
}
defer func() { _ = l.Close() }()
/*
* Here, the path /tmp/foo is ignored. Since we specify localhost,
* rsync thinks we are syncing to a remote host (in this case, the
* other end of the lxd websocket), and so the path specified on the
* --server instance of rsync takes precedence.
*/
rsyncCmd := fmt.Sprintf("%s netcat %s %s --", execPath, auds, name)
args := []string{
"-ar",
"--devices",
"--numeric-ids",
"--partial",
"--sparse",
}
if bwlimit != "" {
args = append(args, "--bwlimit", bwlimit)
}
if len(features) > 0 {
args = append(args, rsyncFeatureArgs(features)...)
}
if len(rsyncArgs) > 0 {
args = append(args, rsyncArgs...)
}
args = append(args, []string{
path,
"localhost:/tmp/foo",
"-e",
rsyncCmd}...)
cmd := exec.Command("rsync", args...)
// Call the wrapper if defined.
if RunWrapper != nil {
cleanup, err := RunWrapper(cmd, path, "")
if err != nil {
return nil, nil, nil, err
}
defer cleanup()
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, nil, nil, err
}
err = cmd.Start()
if err != nil {
return nil, nil, nil, err
}
var conn *net.Conn
chConn := make(chan *net.Conn, 1)
go func() {
conn, err := l.Accept()
if err != nil {
chConn <- nil
return
}
chConn <- &conn
}()
select {
case conn = <-chConn:
if conn == nil {
output, _ := io.ReadAll(stderr)
_ = cmd.Process.Kill()
_ = cmd.Wait()
return nil, nil, nil, fmt.Errorf("Failed to connect to rsync socket (%s)", string(output))
}
case <-time.After(10 * time.Second):
output, _ := io.ReadAll(stderr)
_ = cmd.Process.Kill()
_ = cmd.Wait()
return nil, nil, nil, fmt.Errorf("rsync failed to spawn after 10s (%s)", string(output))
}
return cmd, *conn, stderr, nil
}
// Send sets up the sending half of an rsync, to recursively send the
// directory pointed to by path over the websocket.
func Send(name string, path string, conn io.ReadWriteCloser, tracker *ioprogress.ProgressTracker, features []string, bwlimit string, execPath string, rsyncArgs ...string) error {
cmd, netcatConn, stderr, err := sendSetup(name, path, bwlimit, execPath, features, rsyncArgs...)
if err != nil {
return err
}
// Setup progress tracker.
readNetcatPipe := io.ReadCloser(netcatConn)
if tracker != nil {
readNetcatPipe = &ioprogress.ProgressReader{
ReadCloser: netcatConn,
Tracker: tracker,
}
}
// Forward from netcat to target.
chCopyNetcat := make(chan error, 1)
go func() {
_, err := io.Copy(conn, readNetcatPipe)
chCopyNetcat <- err
_ = readNetcatPipe.Close()
_ = netcatConn.Close()
_ = conn.Close() // sends barrier message.
}()
// Forward from target to netcat.
writeNetcatPipe := io.WriteCloser(netcatConn)
chCopyTarget := make(chan error, 1)
go func() {
_, err := io.Copy(writeNetcatPipe, conn)
chCopyTarget <- err
_ = writeNetcatPipe.Close()
}()
// Wait for rsync to complete.
output, err := io.ReadAll(stderr)
if err != nil {
_ = cmd.Process.Kill()
logger.Errorf("Rsync stderr read failed: %s: %v", path, err)
}
err = cmd.Wait()
errs := []error{}
chCopyNetcatErr := <-chCopyNetcat
chCopyTargetErr := <-chCopyTarget
if err != nil {
errs = append(errs, err)
// Try to get more info about the error.
if chCopyNetcatErr != nil {
errs = append(errs, chCopyNetcatErr)
}
if chCopyTargetErr != nil {
errs = append(errs, chCopyTargetErr)
}
}
if len(errs) > 0 {
return fmt.Errorf("Rsync send failed: %s, %s: %v (%s)", name, path, errs, string(output))
}
return nil
}
// Recv sets up the receiving half of the websocket to rsync (the other
// half set up by rsync.Send), putting the contents in the directory specified
// by path.
func Recv(path string, conn io.ReadWriteCloser, tracker *ioprogress.ProgressTracker, features []string) error {
args := []string{
"--server",
"-vlogDtpre.iLsfx",
"--numeric-ids",
"--devices",
"--partial",
"--sparse",
}
if len(features) > 0 {
args = append(args, rsyncFeatureArgs(features)...)
}
args = append(args, []string{".", path}...)
cmd := exec.Command("rsync", args...)
// Call the wrapper if defined.
if RunWrapper != nil {
cleanup, err := RunWrapper(cmd, "", path)
if err != nil {
return err
}
defer cleanup()
}
// Forward from rsync to source.
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
chCopyRsync := make(chan error, 1)
go func() {
_, err := io.Copy(conn, stdout)
_ = stdout.Close()
_ = conn.Close() // sends barrier message.
chCopyRsync <- err
}()
// Forward from source to rsync.
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
readSourcePipe := io.ReadCloser(conn)
if tracker != nil {
readSourcePipe = &ioprogress.ProgressReader{
ReadCloser: conn,
Tracker: tracker,
}
}
chCopySource := make(chan error, 1)
go func() {
_, err := io.Copy(stdin, readSourcePipe)
_ = stdin.Close()
chCopySource <- err
}()
stderr, err := cmd.StderrPipe()
if err != nil {
_ = cmd.Process.Kill()
logger.Errorf("Rsync stderr read failed: %s: %v", path, err)
}
err = cmd.Start()
if err != nil {
return err
}
output, err := io.ReadAll(stderr)
if err != nil {
logger.Errorf("Rsync stderr read failed: %s: %v", path, err)
}
err = cmd.Wait()
errs := []error{}
chCopyRsyncErr := <-chCopyRsync
chCopySourceErr := <-chCopySource
if err != nil {
errs = append(errs, err)
// Try to get more info about the error.
if chCopyRsyncErr != nil {
errs = append(errs, chCopyRsyncErr)
}
if chCopySourceErr != nil {
errs = append(errs, chCopySourceErr)
}
}
if len(errs) > 0 {
return fmt.Errorf("Rsync receive failed: %s: %v (%s)", path, errs, string(output))
}
return nil
}
func rsyncFeatureArgs(features []string) []string {
args := []string{}
if shared.StringInSlice("xattrs", features) {
args = append(args, "--xattrs")
if AtLeast("3.1.3") {
args = append(args, "--filter=-x security.selinux")
}
}
if shared.StringInSlice("delete", features) {
args = append(args, "--delete")
}
if shared.StringInSlice("compress", features) {
args = append(args, "--compress")
args = append(args, "--compress-level=2")
}
return args
}
// AtLeast compares the local version to a minimum version.
func AtLeast(min string) bool {
// Parse the current version.
out, err := shared.RunCommand("rsync", "--version")
if err != nil {
return false
}
fields := strings.Split(strings.Split(out, "\n")[0], " ")
if len(fields) < 3 {
return false
}
versionStr := strings.TrimPrefix(fields[1], "version ")
ver, err := version.Parse(versionStr)
if err != nil {
return false
}
// Load minium version.
minVer, err := version.NewDottedVersion(min)
if err != nil {
return false
}
return ver.Compare(minVer) >= 0
}