Skip to content

Commit

Permalink
[testing] Revert "Fix persistent rootfs data corruption"
Browse files Browse the repository at this point in the history
This reverts commit 7b61c95.
  • Loading branch information
Ivan Shvedunov committed Oct 11, 2018
1 parent d757062 commit 5308331
Showing 1 changed file with 4 additions and 41 deletions.
45 changes: 4 additions & 41 deletions pkg/libvirttools/persistentroot_volumesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ import (
"encoding/hex"
"fmt"

"github.com/golang/glog"
libvirtxml "github.com/libvirt/libvirt-go-xml"
digest "github.com/opencontainers/go-digest"
"golang.org/x/sys/unix"

"github.com/Mirantis/virtlet/pkg/blockdev"
"github.com/Mirantis/virtlet/pkg/metadata/types"
Expand Down Expand Up @@ -56,33 +54,24 @@ func (v *persistentRootVolume) dmPath() string {
}

func (v *persistentRootVolume) copyImageToDev(imagePath string) error {
syncFiles(imagePath, v.dev.HostPath, v.dmPath())
if _, err := v.owner.Commander().Command("qemu-img", "convert", "-O", "raw", imagePath, v.dmPath()).Run(nil); err != nil {
return err
}
syncFiles(v.dmPath())
return nil
_, err := v.owner.Commander().Command("qemu-img", "convert", "-O", "raw", imagePath, v.dmPath()).Run(nil)
return err
}

func (v *persistentRootVolume) Setup() (*libvirtxml.DomainDisk, *libvirtxml.DomainFilesystem, error) {
glog.V(4).Infof("Persistent rootfs setup on %q", v.dev.HostPath)
imagePath, imageDigest, imageSize, err := v.owner.ImageManager().GetImagePathDigestAndVirtualSize(v.config.Image)
if err != nil {
glog.V(4).Infof("Persistent rootfs setup on %q: image info error %v", v.dev.HostPath, err)
return nil, nil, err
}

if imageDigest.Algorithm() != digest.SHA256 {
glog.V(4).Infof("Persistent rootfs setup on %q: image info error %v", v.dev.HostPath, err)
return nil, nil, fmt.Errorf("unsupported digest algorithm %q", imageDigest.Algorithm())
}
imageHash, err := hex.DecodeString(imageDigest.Hex())
if err != nil {
glog.V(4).Infof("Persistent rootfs setup on %q: bad digest hex: %q", v.dev.HostPath, imageDigest.Hex())
return nil, nil, fmt.Errorf("bad digest hex: %q", imageDigest.Hex())
}
if len(imageHash) != sha256.Size {
glog.V(4).Infof("Persistent rootfs setup on %q: bad digest size: %q", v.dev.HostPath, imageDigest.Hex())
return nil, nil, fmt.Errorf("bad digest size: %q", imageDigest.Hex())
}

Expand All @@ -92,21 +81,14 @@ func (v *persistentRootVolume) Setup() (*libvirtxml.DomainDisk, *libvirtxml.Doma
headerMatches, err := ldh.EnsureDevHeaderMatches(v.dev.HostPath, hash)

if err == nil {
glog.V(4).Infof("Persistent rootfs setup on %q: headerMatches: %v", v.dev.HostPath, headerMatches)
err = ldh.Map(v.dev.HostPath, v.dmName(), imageSize)
}

if err == nil {
if headerMatches {
glog.V(4).Infof("Persistent rootfs setup on %q: header matches image %q, not overwriting", v.dev.HostPath, imagePath)
} else {
glog.V(4).Infof("Persistent rootfs setup on %q: writing image from %q", v.dev.HostPath, imagePath)
err = v.copyImageToDev(imagePath)
}
if err == nil && !headerMatches {
err = v.copyImageToDev(imagePath)
}

if err != nil {
glog.V(4).Infof("Persistent rootfs setup on %q: error: %v", v.dev.HostPath, err)
return nil, nil, err
}

Expand All @@ -120,22 +102,3 @@ func (v *persistentRootVolume) Setup() (*libvirtxml.DomainDisk, *libvirtxml.Doma
func (v *persistentRootVolume) Teardown() error {
return v.devHandler().Unmap(v.dmName())
}

func syncFiles(paths ...string) error {
// https://www.redhat.com/archives/libguestfs/2012-July/msg00009.html
unix.Sync()
for _, p := range paths {
fd, err := unix.Open(p, unix.O_RDWR|unix.O_SYNC, 0)
if err != nil {
return err
}
if err := unix.Fsync(fd); err != nil {
unix.Close(fd)
return err
}
if err := unix.Close(fd); err != nil {
return err
}
}
return nil
}

0 comments on commit 5308331

Please sign in to comment.