forked from canonical/lxd
/
migrate.go
607 lines (501 loc) · 13.4 KB
/
migrate.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// Package migration provides the primitives for migration in LXD.
//
// See https://github.com/lxc/lxd/blob/master/specs/migration.md for a complete
// description.
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/golang/protobuf/proto"
"github.com/gorilla/websocket"
"gopkg.in/lxc/go-lxc.v2"
"github.com/lxc/lxd"
"github.com/lxc/lxd/shared"
)
type migrationFields struct {
live bool
controlSecret string
controlConn *websocket.Conn
criuSecret string
criuConn *websocket.Conn
fsSecret string
fsConn *websocket.Conn
container container
}
func (c *migrationFields) send(m proto.Message) error {
w, err := c.controlConn.NextWriter(websocket.BinaryMessage)
if err != nil {
return err
}
defer w.Close()
data, err := proto.Marshal(m)
if err != nil {
return err
}
return shared.WriteAll(w, data)
}
func findCriu(host string) error {
_, err := exec.LookPath("criu")
if err != nil {
return fmt.Errorf("Couldn't find criu binary on the %s server. Is it installed in LXD's path?", host)
}
return nil
}
func (c *migrationFields) recv(m proto.Message) error {
mt, r, err := c.controlConn.NextReader()
if err != nil {
return err
}
if mt != websocket.BinaryMessage {
return fmt.Errorf("Only binary messages allowed")
}
buf, err := ioutil.ReadAll(r)
if err != nil {
return err
}
return proto.Unmarshal(buf, m)
}
func (c *migrationFields) disconnect() {
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
if c.controlConn != nil {
c.controlConn.WriteMessage(websocket.CloseMessage, closeMsg)
}
if c.fsConn != nil {
c.fsConn.WriteMessage(websocket.CloseMessage, closeMsg)
}
if c.criuConn != nil {
c.criuConn.WriteMessage(websocket.CloseMessage, closeMsg)
}
}
func (c *migrationFields) sendControl(err error) {
message := ""
if err != nil {
message = err.Error()
}
msg := MigrationControl{
Success: proto.Bool(err == nil),
Message: proto.String(message),
}
c.send(&msg)
if err != nil {
c.disconnect()
}
}
func (c *migrationFields) controlChannel() <-chan MigrationControl {
ch := make(chan MigrationControl)
go func() {
msg := MigrationControl{}
err := c.recv(&msg)
if err != nil {
shared.Debugf("Got error reading migration control socket %s", err)
close(ch)
return
}
ch <- msg
}()
return ch
}
func CollectCRIULogFile(c container, imagesDir string, function string, method string) error {
t := time.Now().Format(time.RFC3339)
newPath := shared.LogPath(c.Name(), fmt.Sprintf("%s_%s_%s.log", function, method, t))
return shared.FileCopy(filepath.Join(imagesDir, fmt.Sprintf("%s.log", method)), newPath)
}
func GetCRIULogErrors(imagesDir string, method string) string {
f, err := os.Open(path.Join(imagesDir, fmt.Sprintf("%s.log", method)))
if err != nil {
return fmt.Sprintf("Problem accessing CRIU log: %s", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
ret := []string{}
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Error") {
ret = append(ret, scanner.Text())
}
}
return strings.Join(ret, "\n")
}
type migrationSourceWs struct {
migrationFields
allConnected chan bool
}
func NewMigrationSource(c container) (shared.OperationWebsocket, error) {
ret := migrationSourceWs{migrationFields{container: c}, make(chan bool, 1)}
var err error
ret.controlSecret, err = shared.RandomCryptoString()
if err != nil {
return nil, err
}
ret.fsSecret, err = shared.RandomCryptoString()
if err != nil {
return nil, err
}
if c.IsRunning() {
if err := findCriu("source"); err != nil {
return nil, err
}
ret.live = true
ret.criuSecret, err = shared.RandomCryptoString()
if err != nil {
return nil, err
}
} else {
c.StorageStart()
}
return &ret, nil
}
func (s *migrationSourceWs) Metadata() interface{} {
secrets := shared.Jmap{
"control": s.controlSecret,
"fs": s.fsSecret,
}
if s.criuSecret != "" {
secrets["criu"] = s.criuSecret
}
return secrets
}
func (s *migrationSourceWs) Connect(secret string, r *http.Request, w http.ResponseWriter) error {
var conn **websocket.Conn
switch secret {
case s.controlSecret:
conn = &s.controlConn
case s.criuSecret:
conn = &s.criuConn
case s.fsSecret:
conn = &s.fsConn
default:
/* If we didn't find the right secret, the user provided a bad one,
* which 403, not 404, since this operation actually exists */
return os.ErrPermission
}
c, err := shared.WebsocketUpgrader.Upgrade(w, r, nil)
if err != nil {
return err
}
*conn = c
if s.controlConn != nil && (!s.live || s.criuConn != nil) && s.fsConn != nil {
s.allConnected <- true
}
return nil
}
func (s *migrationSourceWs) Do(id string) shared.OperationResult {
<-s.allConnected
criuType := CRIUType_CRIU_RSYNC.Enum()
if !s.live {
criuType = nil
defer s.container.StorageStop()
}
idmaps := make([]*IDMapType, 0)
idmapset := s.container.IdmapSet()
if idmapset != nil {
for _, ctnIdmap := range idmapset.Idmap {
idmap := IDMapType{
Isuid: proto.Bool(ctnIdmap.Isuid),
Isgid: proto.Bool(ctnIdmap.Isgid),
Hostid: proto.Int(ctnIdmap.Hostid),
Nsid: proto.Int(ctnIdmap.Nsid),
Maprange: proto.Int(ctnIdmap.Maprange),
}
idmaps = append(idmaps, &idmap)
}
}
header := MigrationHeader{
Fs: MigrationFSType_RSYNC.Enum(),
Criu: criuType,
Idmap: idmaps,
}
if err := s.send(&header); err != nil {
s.sendControl(err)
return shared.OperationError(err)
}
if err := s.recv(&header); err != nil {
s.sendControl(err)
return shared.OperationError(err)
}
if *header.Fs != MigrationFSType_RSYNC {
err := fmt.Errorf("Formats other than rsync not understood")
s.sendControl(err)
return shared.OperationError(err)
}
if s.live {
if header.Criu == nil {
err := fmt.Errorf("Got no CRIU socket type for live migration")
s.sendControl(err)
return shared.OperationError(err)
} else if *header.Criu != CRIUType_CRIU_RSYNC {
err := fmt.Errorf("Formats other than criu rsync not understood")
s.sendControl(err)
return shared.OperationError(err)
}
checkpointDir, err := ioutil.TempDir("", "lxd_migration_")
if err != nil {
s.sendControl(err)
return shared.OperationError(err)
}
defer os.RemoveAll(checkpointDir)
opts := lxc.CheckpointOptions{Stop: true, Directory: checkpointDir, Verbose: true}
err = s.container.Checkpoint(opts)
if err2 := CollectCRIULogFile(s.container, checkpointDir, "migration", "dump"); err2 != nil {
shared.Debugf("Error collecting checkpoint log file %s", err)
}
if err != nil {
log := GetCRIULogErrors(checkpointDir, "dump")
err = fmt.Errorf("checkpoint failed:\n%s", log)
s.sendControl(err)
return shared.OperationError(err)
}
/*
* We do the serially right now, but there's really no reason for us
* to; since we have separate websockets, we can do it in parallel if
* we wanted to. However, assuming we're network bound, there's really
* no reason to do these in parallel. In the future when we're using
* p.haul's protocol, it will make sense to do these in parallel.
*/
if err := RsyncSend(shared.AddSlash(checkpointDir), s.criuConn); err != nil {
s.sendControl(err)
return shared.OperationError(err)
}
}
fsDir := s.container.RootfsPath()
if err := RsyncSend(shared.AddSlash(fsDir), s.fsConn); err != nil {
s.sendControl(err)
return shared.OperationError(err)
}
msg := MigrationControl{}
if err := s.recv(&msg); err != nil {
s.disconnect()
return shared.OperationError(err)
}
// TODO: should we add some config here about automatically restarting
// the container migrate failure? What about the failures above?
if !*msg.Success {
return shared.OperationError(fmt.Errorf(*msg.Message))
}
return shared.OperationSuccess
}
type migrationSink struct {
migrationFields
url string
dialer websocket.Dialer
}
type MigrationSinkArgs struct {
Url string
Dialer websocket.Dialer
Container container
Secrets map[string]string
}
func NewMigrationSink(args *MigrationSinkArgs) (func() error, error) {
sink := migrationSink{
migrationFields{container: args.Container},
args.Url,
args.Dialer,
}
var ok bool
sink.controlSecret, ok = args.Secrets["control"]
if !ok {
return nil, fmt.Errorf("Missing control secret")
}
sink.fsSecret, ok = args.Secrets["fs"]
if !ok {
return nil, fmt.Errorf("Missing fs secret")
}
sink.criuSecret, ok = args.Secrets["criu"]
sink.live = ok
if err := findCriu("destination"); sink.live && err != nil {
return nil, err
}
return sink.do, nil
}
func (c *migrationSink) connectWithSecret(secret string) (*websocket.Conn, error) {
query := url.Values{"secret": []string{secret}}
// TODO: we shouldn't assume this is a HTTP URL
url := c.url + "?" + query.Encode()
return lxd.WebsocketDial(c.dialer, url)
}
func (c *migrationSink) do() error {
var err error
c.controlConn, err = c.connectWithSecret(c.controlSecret)
if err != nil {
return err
}
defer c.disconnect()
c.fsConn, err = c.connectWithSecret(c.fsSecret)
if err != nil {
c.sendControl(err)
return err
}
if c.live {
c.criuConn, err = c.connectWithSecret(c.criuSecret)
if err != nil {
c.sendControl(err)
return err
}
}
// For now, we just ignore whatever the server sends us. We only
// support RSYNC, so that's what we respond with.
header := MigrationHeader{}
if err := c.recv(&header); err != nil {
c.sendControl(err)
return err
}
criuType := CRIUType_CRIU_RSYNC.Enum()
if !c.live {
criuType = nil
}
resp := MigrationHeader{Fs: MigrationFSType_RSYNC.Enum(), Criu: criuType}
if err := c.send(&resp); err != nil {
c.sendControl(err)
return err
}
restore := make(chan error)
go func(c *migrationSink) {
imagesDir := ""
srcIdmap := new(shared.IdmapSet)
dstIdmap := c.container.IdmapSet()
if dstIdmap == nil {
dstIdmap = new(shared.IdmapSet)
}
if c.live {
var err error
imagesDir, err = ioutil.TempDir("", "lxd_migration_")
if err != nil {
os.RemoveAll(imagesDir)
c.sendControl(err)
return
}
defer func() {
err := CollectCRIULogFile(c.container, imagesDir, "migration", "restore")
/*
* If the checkpoint fails, we won't have any log to collect,
* so don't warn about that.
*/
if err != nil && !os.IsNotExist(err) {
shared.Debugf("Error collectiong migration log file %s", err)
}
os.RemoveAll(imagesDir)
}()
if err := RsyncRecv(shared.AddSlash(imagesDir), c.criuConn); err != nil {
restore <- err
os.RemoveAll(imagesDir)
c.sendControl(err)
return
}
/*
* For unprivileged containers we need to shift the
* perms on the images images so that they can be
* opened by the process after it is in its user
* namespace.
*/
if dstIdmap != nil {
if err := dstIdmap.ShiftRootfs(imagesDir); err != nil {
restore <- err
os.RemoveAll(imagesDir)
c.sendControl(err)
return
}
}
}
fsDir := c.container.RootfsPath()
if err := RsyncRecv(shared.AddSlash(fsDir), c.fsConn); err != nil {
restore <- err
c.sendControl(err)
return
}
for _, idmap := range header.Idmap {
e := shared.IdmapEntry{
Isuid: *idmap.Isuid,
Isgid: *idmap.Isgid,
Nsid: int(*idmap.Nsid),
Hostid: int(*idmap.Hostid),
Maprange: int(*idmap.Maprange)}
srcIdmap.Idmap = shared.Extend(srcIdmap.Idmap, e)
}
if !reflect.DeepEqual(srcIdmap, dstIdmap) {
if err := srcIdmap.UnshiftRootfs(shared.VarPath("containers", c.container.Name())); err != nil {
restore <- err
c.sendControl(err)
return
}
if err := dstIdmap.ShiftRootfs(shared.VarPath("containers", c.container.Name())); err != nil {
restore <- err
c.sendControl(err)
return
}
}
if c.live {
err := c.container.StartFromMigration(imagesDir)
if err != nil {
log := GetCRIULogErrors(imagesDir, "restore")
err = fmt.Errorf("restore failed:\n%s", log)
}
restore <- err
} else {
restore <- nil
}
}(c)
source := c.controlChannel()
for {
select {
case err = <-restore:
c.sendControl(err)
return err
case msg, ok := <-source:
if !ok {
c.disconnect()
return fmt.Errorf("Got error reading source")
}
if !*msg.Success {
c.disconnect()
return fmt.Errorf(*msg.Message)
} else {
// The source can only tell us it failed (e.g. if
// checkpointing failed). We have to tell the source
// whether or not the restore was successful.
shared.Debugf("Unknown message %v from source", msg)
}
}
}
}
/*
* Similar to forkstart, this is called when lxd is invoked as:
*
* lxd forkmigrate <container> <lxcpath> <path_to_config> <path_to_criu_images>
*
* liblxc's restore() sets up the processes in such a way that the monitor ends
* up being a child of the process that calls it, in our case lxd. However, we
* really want the monitor to be daemonized, so we fork again. Additionally, we
* want to fork for the same reasons we do forkstart (i.e. reduced memory
* footprint when we fork tasks that will never free golang's memory, etc.)
*/
func MigrateContainer(args []string) error {
if len(args) != 5 {
return fmt.Errorf("Bad arguments %q", args)
}
name := args[1]
lxcpath := args[2]
configPath := args[3]
imagesDir := args[4]
defer os.Remove(configPath)
c, err := lxc.NewContainer(name, lxcpath)
if err != nil {
return err
}
if err := c.LoadConfigFile(configPath); err != nil {
return err
}
return c.Restore(lxc.RestoreOptions{
Directory: imagesDir,
Verbose: true,
})
}