/usr/share/gocode/src/github.com/lxc/lxd/lxd/storage.go is in golang-github-lxc-lxd-dev 2.0.2-0ubuntu1~16.04.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"syscall"
"time"
"github.com/gorilla/websocket"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/logging"
log "gopkg.in/inconshreveable/log15.v2"
)
/* Some interesting filesystems */
const (
filesystemSuperMagicTmpfs = 0x01021994
filesystemSuperMagicExt4 = 0xEF53
filesystemSuperMagicXfs = 0x58465342
filesystemSuperMagicNfs = 0x6969
filesystemSuperMagicZfs = 0x2fc12fc1
)
/*
* filesystemDetect returns the filesystem on which
* the passed-in path sits
*/
func filesystemDetect(path string) (string, error) {
fs := syscall.Statfs_t{}
err := syscall.Statfs(path, &fs)
if err != nil {
return "", err
}
switch fs.Type {
case filesystemSuperMagicBtrfs:
return "btrfs", nil
case filesystemSuperMagicZfs:
return "zfs", nil
case filesystemSuperMagicTmpfs:
return "tmpfs", nil
case filesystemSuperMagicExt4:
return "ext4", nil
case filesystemSuperMagicXfs:
return "xfs", nil
case filesystemSuperMagicNfs:
return "nfs", nil
default:
shared.Debugf("Unknown backing filesystem type: 0x%x", fs.Type)
return string(fs.Type), nil
}
}
// storageRsyncCopy copies a directory using rsync (with the --devices option).
func storageRsyncCopy(source string, dest string) (string, error) {
if err := os.MkdirAll(dest, 0755); err != nil {
return "", err
}
rsyncVerbosity := "-q"
if debug {
rsyncVerbosity = "-vi"
}
output, err := exec.Command(
"rsync",
"-a",
"-HAX",
"--devices",
"--delete",
"--checksum",
"--numeric-ids",
rsyncVerbosity,
shared.AddSlash(source),
dest).CombinedOutput()
return string(output), err
}
// storageType defines the type of a storage
type storageType int
const (
storageTypeBtrfs storageType = iota
storageTypeZfs
storageTypeLvm
storageTypeDir
storageTypeMock
)
func storageTypeToString(sType storageType) string {
switch sType {
case storageTypeBtrfs:
return "btrfs"
case storageTypeZfs:
return "zfs"
case storageTypeLvm:
return "lvm"
case storageTypeMock:
return "mock"
}
return "dir"
}
type MigrationStorageSourceDriver interface {
/* snapshots for this container, if any */
Snapshots() []container
/* send any bits of the container/snapshots that are possible while the
* container is still running.
*/
SendWhileRunning(conn *websocket.Conn) error
/* send the final bits (e.g. a final delta snapshot for zfs, btrfs, or
* do a final rsync) of the fs after the container has been
* checkpointed. This will only be called when a container is actually
* being live migrated.
*/
SendAfterCheckpoint(conn *websocket.Conn) error
/* Called after either success or failure of a migration, can be used
* to clean up any temporary snapshots, etc.
*/
Cleanup()
}
type storage interface {
Init(config map[string]interface{}) (storage, error)
GetStorageType() storageType
GetStorageTypeName() string
GetStorageTypeVersion() string
// ContainerCreate creates an empty container (no rootfs/metadata.yaml)
ContainerCreate(container container) error
// ContainerCreateFromImage creates a container from a image.
ContainerCreateFromImage(container container, imageFingerprint string) error
ContainerCanRestore(container container, sourceContainer container) error
ContainerDelete(container container) error
ContainerCopy(container container, sourceContainer container) error
ContainerStart(container container) error
ContainerStop(container container) error
ContainerRename(container container, newName string) error
ContainerRestore(container container, sourceContainer container) error
ContainerSetQuota(container container, size int64) error
ContainerGetUsage(container container) (int64, error)
ContainerSnapshotCreate(
snapshotContainer container, sourceContainer container) error
ContainerSnapshotDelete(snapshotContainer container) error
ContainerSnapshotRename(snapshotContainer container, newName string) error
ContainerSnapshotStart(container container) error
ContainerSnapshotStop(container container) error
/* for use in migrating snapshots */
ContainerSnapshotCreateEmpty(snapshotContainer container) error
ImageCreate(fingerprint string) error
ImageDelete(fingerprint string) error
MigrationType() MigrationFSType
// Get the pieces required to migrate the source. This contains a list
// of the "object" (i.e. container or snapshot, depending on whether or
// not it is a snapshot name) to be migrated in order, and a channel
// for arguments of the specific migration command. We use a channel
// here so we don't have to invoke `zfs send` or `rsync` or whatever
// and keep its stdin/stdout open for each snapshot during the course
// of migration, we can do it lazily.
//
// N.B. that the order here important: e.g. in btrfs/zfs, snapshots
// which are parents of other snapshots should be sent first, to save
// as much transfer as possible. However, the base container is always
// sent as the first object, since that is the grandparent of every
// snapshot.
//
// We leave sending containers which are snapshots of other containers
// already present on the target instance as an exercise for the
// enterprising developer.
MigrationSource(container container) (MigrationStorageSourceDriver, error)
MigrationSink(live bool, container container, objects []container, conn *websocket.Conn) error
}
func newStorage(d *Daemon, sType storageType) (storage, error) {
var nilmap map[string]interface{}
return newStorageWithConfig(d, sType, nilmap)
}
func newStorageWithConfig(d *Daemon, sType storageType, config map[string]interface{}) (storage, error) {
if d.MockMode {
return d.Storage, nil
}
var s storage
switch sType {
case storageTypeBtrfs:
if d.Storage != nil && d.Storage.GetStorageType() == storageTypeBtrfs {
return d.Storage, nil
}
s = &storageLogWrapper{w: &storageBtrfs{d: d}}
case storageTypeZfs:
if d.Storage != nil && d.Storage.GetStorageType() == storageTypeZfs {
return d.Storage, nil
}
s = &storageLogWrapper{w: &storageZfs{d: d}}
case storageTypeLvm:
if d.Storage != nil && d.Storage.GetStorageType() == storageTypeLvm {
return d.Storage, nil
}
s = &storageLogWrapper{w: &storageLvm{d: d}}
default:
if d.Storage != nil && d.Storage.GetStorageType() == storageTypeDir {
return d.Storage, nil
}
s = &storageLogWrapper{w: &storageDir{d: d}}
}
return s.Init(config)
}
func storageForFilename(d *Daemon, filename string) (storage, error) {
var filesystem string
var err error
config := make(map[string]interface{})
storageType := storageTypeDir
if d.MockMode {
return newStorageWithConfig(d, storageTypeMock, config)
}
if shared.PathExists(filename) {
filesystem, err = filesystemDetect(filename)
if err != nil {
return nil, fmt.Errorf("couldn't detect filesystem for '%s': %v", filename, err)
}
}
if shared.PathExists(filename + ".lv") {
storageType = storageTypeLvm
lvPath, err := os.Readlink(filename + ".lv")
if err != nil {
return nil, fmt.Errorf("couldn't read link dest for '%s': %v", filename+".lv", err)
}
vgname := filepath.Base(filepath.Dir(lvPath))
config["vgName"] = vgname
} else if shared.PathExists(filename + ".zfs") {
storageType = storageTypeZfs
} else if shared.PathExists(filename+".btrfs") || filesystem == "btrfs" {
storageType = storageTypeBtrfs
}
return newStorageWithConfig(d, storageType, config)
}
func storageForImage(d *Daemon, imgInfo *shared.ImageInfo) (storage, error) {
imageFilename := shared.VarPath("images", imgInfo.Fingerprint)
return storageForFilename(d, imageFilename)
}
type storageShared struct {
sType storageType
sTypeName string
sTypeVersion string
log shared.Logger
}
func (ss *storageShared) initShared() error {
ss.log = logging.AddContext(
shared.Log,
log.Ctx{"driver": fmt.Sprintf("storage/%s", ss.sTypeName)},
)
return nil
}
func (ss *storageShared) GetStorageType() storageType {
return ss.sType
}
func (ss *storageShared) GetStorageTypeName() string {
return ss.sTypeName
}
func (ss *storageShared) GetStorageTypeVersion() string {
return ss.sTypeVersion
}
func (ss *storageShared) shiftRootfs(c container) error {
dpath := c.Path()
rpath := c.RootfsPath()
shared.Log.Debug("Shifting root filesystem",
log.Ctx{"container": c.Name(), "rootfs": rpath})
idmapset := c.IdmapSet()
if idmapset == nil {
return fmt.Errorf("IdmapSet of container '%s' is nil", c.Name())
}
err := idmapset.ShiftRootfs(rpath)
if err != nil {
shared.Debugf("Shift of rootfs %s failed: %s", rpath, err)
return err
}
/* Set an acl so the container root can descend the container dir */
// TODO: i changed this so it calls ss.setUnprivUserAcl, which does
// the acl change only if the container is not privileged, think thats right.
return ss.setUnprivUserAcl(c, dpath)
}
func (ss *storageShared) setUnprivUserAcl(c container, destPath string) error {
idmapset := c.IdmapSet()
// Skip for privileged containers
if idmapset == nil {
return nil
}
// Make sure the map is valid. Skip if container uid 0 == host uid 0
uid, _ := idmapset.ShiftIntoNs(0, 0)
switch uid {
case -1:
return fmt.Errorf("Container doesn't have a uid 0 in its map")
case 0:
return nil
}
// Attempt to set a POSIX ACL first. Fallback to chmod if the fs doesn't support it.
acl := fmt.Sprintf("%d:rx", uid)
_, err := exec.Command("setfacl", "-m", acl, destPath).CombinedOutput()
if err != nil {
_, err := exec.Command("chmod", "+x", destPath).CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to chmod the container path.")
}
}
return nil
}
type storageLogWrapper struct {
w storage
log shared.Logger
}
func (lw *storageLogWrapper) Init(config map[string]interface{}) (storage, error) {
_, err := lw.w.Init(config)
lw.log = logging.AddContext(
shared.Log,
log.Ctx{"driver": fmt.Sprintf("storage/%s", lw.w.GetStorageTypeName())},
)
lw.log.Info("Init")
return lw, err
}
func (lw *storageLogWrapper) GetStorageType() storageType {
return lw.w.GetStorageType()
}
func (lw *storageLogWrapper) GetStorageTypeName() string {
return lw.w.GetStorageTypeName()
}
func (lw *storageLogWrapper) GetStorageTypeVersion() string {
return lw.w.GetStorageTypeVersion()
}
func (lw *storageLogWrapper) ContainerCreate(container container) error {
lw.log.Debug(
"ContainerCreate",
log.Ctx{
"name": container.Name(),
"isPrivileged": container.IsPrivileged()})
return lw.w.ContainerCreate(container)
}
func (lw *storageLogWrapper) ContainerCreateFromImage(
container container, imageFingerprint string) error {
lw.log.Debug(
"ContainerCreateFromImage",
log.Ctx{
"imageFingerprint": imageFingerprint,
"name": container.Name(),
"isPrivileged": container.IsPrivileged()})
return lw.w.ContainerCreateFromImage(container, imageFingerprint)
}
func (lw *storageLogWrapper) ContainerCanRestore(container container, sourceContainer container) error {
lw.log.Debug("ContainerCanRestore", log.Ctx{"container": container.Name()})
return lw.w.ContainerCanRestore(container, sourceContainer)
}
func (lw *storageLogWrapper) ContainerDelete(container container) error {
lw.log.Debug("ContainerDelete", log.Ctx{"container": container.Name()})
return lw.w.ContainerDelete(container)
}
func (lw *storageLogWrapper) ContainerCopy(
container container, sourceContainer container) error {
lw.log.Debug(
"ContainerCopy",
log.Ctx{
"container": container.Name(),
"source": sourceContainer.Name()})
return lw.w.ContainerCopy(container, sourceContainer)
}
func (lw *storageLogWrapper) ContainerStart(container container) error {
lw.log.Debug("ContainerStart", log.Ctx{"container": container.Name()})
return lw.w.ContainerStart(container)
}
func (lw *storageLogWrapper) ContainerStop(container container) error {
lw.log.Debug("ContainerStop", log.Ctx{"container": container.Name()})
return lw.w.ContainerStop(container)
}
func (lw *storageLogWrapper) ContainerRename(
container container, newName string) error {
lw.log.Debug(
"ContainerRename",
log.Ctx{
"container": container.Name(),
"newName": newName})
return lw.w.ContainerRename(container, newName)
}
func (lw *storageLogWrapper) ContainerRestore(
container container, sourceContainer container) error {
lw.log.Debug(
"ContainerRestore",
log.Ctx{
"container": container.Name(),
"source": sourceContainer.Name()})
return lw.w.ContainerRestore(container, sourceContainer)
}
func (lw *storageLogWrapper) ContainerSetQuota(
container container, size int64) error {
lw.log.Debug(
"ContainerSetQuota",
log.Ctx{
"container": container.Name(),
"size": size})
return lw.w.ContainerSetQuota(container, size)
}
func (lw *storageLogWrapper) ContainerGetUsage(
container container) (int64, error) {
lw.log.Debug(
"ContainerGetUsage",
log.Ctx{
"container": container.Name()})
return lw.w.ContainerGetUsage(container)
}
func (lw *storageLogWrapper) ContainerSnapshotCreate(
snapshotContainer container, sourceContainer container) error {
lw.log.Debug("ContainerSnapshotCreate",
log.Ctx{
"snapshotContainer": snapshotContainer.Name(),
"sourceContainer": sourceContainer.Name()})
return lw.w.ContainerSnapshotCreate(snapshotContainer, sourceContainer)
}
func (lw *storageLogWrapper) ContainerSnapshotCreateEmpty(snapshotContainer container) error {
lw.log.Debug("ContainerSnapshotCreateEmpty",
log.Ctx{
"snapshotContainer": snapshotContainer.Name()})
return lw.w.ContainerSnapshotCreateEmpty(snapshotContainer)
}
func (lw *storageLogWrapper) ContainerSnapshotDelete(
snapshotContainer container) error {
lw.log.Debug("ContainerSnapshotDelete",
log.Ctx{"snapshotContainer": snapshotContainer.Name()})
return lw.w.ContainerSnapshotDelete(snapshotContainer)
}
func (lw *storageLogWrapper) ContainerSnapshotRename(
snapshotContainer container, newName string) error {
lw.log.Debug("ContainerSnapshotRename",
log.Ctx{
"snapshotContainer": snapshotContainer.Name(),
"newName": newName})
return lw.w.ContainerSnapshotRename(snapshotContainer, newName)
}
func (lw *storageLogWrapper) ContainerSnapshotStart(container container) error {
lw.log.Debug("ContainerSnapshotStart", log.Ctx{"container": container.Name()})
return lw.w.ContainerSnapshotStart(container)
}
func (lw *storageLogWrapper) ContainerSnapshotStop(container container) error {
lw.log.Debug("ContainerSnapshotStop", log.Ctx{"container": container.Name()})
return lw.w.ContainerSnapshotStop(container)
}
func (lw *storageLogWrapper) ImageCreate(fingerprint string) error {
lw.log.Debug(
"ImageCreate",
log.Ctx{"fingerprint": fingerprint})
return lw.w.ImageCreate(fingerprint)
}
func (lw *storageLogWrapper) ImageDelete(fingerprint string) error {
lw.log.Debug("ImageDelete", log.Ctx{"fingerprint": fingerprint})
return lw.w.ImageDelete(fingerprint)
}
func (lw *storageLogWrapper) MigrationType() MigrationFSType {
return lw.w.MigrationType()
}
func (lw *storageLogWrapper) MigrationSource(container container) (MigrationStorageSourceDriver, error) {
lw.log.Debug("MigrationSource", log.Ctx{"container": container.Name()})
return lw.w.MigrationSource(container)
}
func (lw *storageLogWrapper) MigrationSink(live bool, container container, objects []container, conn *websocket.Conn) error {
objNames := []string{}
for _, obj := range objects {
objNames = append(objNames, obj.Name())
}
lw.log.Debug("MigrationSink", log.Ctx{
"live": live,
"container": container.Name(),
"objects": objNames,
})
return lw.w.MigrationSink(live, container, objects, conn)
}
func ShiftIfNecessary(container container, srcIdmap *shared.IdmapSet) error {
dstIdmap := container.IdmapSet()
if dstIdmap == nil {
dstIdmap = new(shared.IdmapSet)
}
if !reflect.DeepEqual(srcIdmap, dstIdmap) {
var jsonIdmap string
if srcIdmap != nil {
idmapBytes, err := json.Marshal(srcIdmap.Idmap)
if err != nil {
return err
}
jsonIdmap = string(idmapBytes)
} else {
jsonIdmap = "[]"
}
err := container.ConfigKeySet("volatile.last_state.idmap", jsonIdmap)
if err != nil {
return err
}
}
return nil
}
type rsyncStorageSourceDriver struct {
container container
snapshots []container
}
func (s rsyncStorageSourceDriver) Snapshots() []container {
return s.snapshots
}
func (s rsyncStorageSourceDriver) SendWhileRunning(conn *websocket.Conn) error {
toSend := append([]container{s.container}, s.snapshots...)
for _, send := range toSend {
path := send.Path()
if err := RsyncSend(shared.AddSlash(path), conn); err != nil {
return err
}
}
return nil
}
func (s rsyncStorageSourceDriver) SendAfterCheckpoint(conn *websocket.Conn) error {
/* resync anything that changed between our first send and the checkpoint */
return RsyncSend(shared.AddSlash(s.container.Path()), conn)
}
func (s rsyncStorageSourceDriver) Cleanup() {
/* no-op */
}
func rsyncMigrationSource(container container) (MigrationStorageSourceDriver, error) {
snapshots, err := container.Snapshots()
if err != nil {
return nil, err
}
return rsyncStorageSourceDriver{container, snapshots}, nil
}
func rsyncMigrationSink(live bool, container container, snapshots []container, conn *websocket.Conn) error {
/* the first object is the actual container */
if err := RsyncRecv(shared.AddSlash(container.Path()), conn); err != nil {
return err
}
if len(snapshots) > 0 {
err := os.MkdirAll(shared.VarPath(fmt.Sprintf("snapshots/%s", container.Name())), 0700)
if err != nil {
return err
}
}
for _, snap := range snapshots {
if err := RsyncRecv(shared.AddSlash(snap.Path()), conn); err != nil {
return err
}
}
if live {
/* now receive the final sync */
if err := RsyncRecv(shared.AddSlash(container.Path()), conn); err != nil {
return err
}
}
return nil
}
// Useful functions for unreliable backends
func tryExec(name string, arg ...string) ([]byte, error) {
var err error
var output []byte
for i := 0; i < 20; i++ {
output, err = exec.Command(name, arg...).CombinedOutput()
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
return output, err
}
func tryMount(src string, dst string, fs string, flags uintptr, options string) error {
var err error
for i := 0; i < 20; i++ {
err = syscall.Mount(src, dst, fs, flags, options)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
if err != nil {
return err
}
return nil
}
func tryUnmount(path string, flags int) error {
var err error
for i := 0; i < 20; i++ {
err = syscall.Unmount(path, flags)
if err == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
if err != nil {
return err
}
return nil
}
|