Skip to content

Commit

Permalink
Fix vm rootfs disk persistence issue (#1676)
Browse files Browse the repository at this point in the history
* Fix vm rootfs disk persistence issue

Fixes #1672

* apply CI
  • Loading branch information
muhamadazmy authored May 4, 2022
1 parent 1fe78b9 commit 0f3b255
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 4 additions & 2 deletions pkg/primitives/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,10 @@ func (p *Primitives) virtualMachineProvisionImpl(ctx context.Context, wl *gridty
return result, errors.Wrap(err, "disk does not exist")
}

//TODO: this should not happen if disk image was written before !!
// fs detection must be done here
//TODO: DiskWrite will not override the disk if it already has a partition table
// or a filesystem. this means that if later the disk is assigned to a new VM with
// a different flist it will have the same old operating system copied from previous
// setup.
if err = storage.DiskWrite(ctx, disk.ID.String(), imageInfo.ImagePath); err != nil {
return result, errors.Wrap(err, "failed to write image to disk")
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/storage/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,19 @@ func (s *Module) DiskFormat(name string) error {
return s.ensureFS(path)
}

// DiskWrite writes image to disk
// DiskWrite writes image to disk. Disk will not be changed
// if it already has a filesystem or partition table.
func (s *Module) DiskWrite(name string, image string) error {
path, err := s.findDisk(name)
if err != nil {
return errors.Wrapf(err, "couldn't find disk with id: %s", name)
}

if !s.isEmptyDisk(path) {
log.Debug().Str("disk", path).Msg("disk already has a filesystem. no write")
return nil
}

source, err := os.Open(image)
if err != nil {
return errors.Wrap(err, "failed to open image")
Expand Down Expand Up @@ -223,6 +230,14 @@ func (s *Module) ensureFS(disk string) error {
return errors.Wrapf(err, "unknown btrfs error '%s'", string(output))
}

// isEmptyDisk return true, if disk file has no partition table or filesystem
// else, returns false
func (s *Module) isEmptyDisk(disk string) bool {
err := exec.Command("blkid", disk).Run()

return err != nil
}

func (s *Module) safePath(base, id string) (string, error) {
path := filepath.Join(base, id)
// this to avoid passing an `injection` id like '../name'
Expand Down

0 comments on commit 0f3b255

Please sign in to comment.