Skip to content

Commit

Permalink
Merge pull request #183 from beringresearch/img-compress
Browse files Browse the repository at this point in the history
compress disk image on publish
  • Loading branch information
idroz authored Apr 4, 2024
2 parents f861380 + e17aa9d commit 6181343
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ func importMachine(cmd *cobra.Command, args []string) {
os.RemoveAll(tempArchive)
log.Fatal("unable to import: " + err.Error())
}

err = machineConfig.DecompressQemuDiskImage()
if err != nil {
os.RemoveAll(targetDir)
os.RemoveAll(tempArchive)
log.Fatal("unable to import: " + err.Error())
}
}

func decryptArchive(archive string) error {
Expand Down
6 changes: 6 additions & 0 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ func publish(cmd *cobra.Command, args []string) {
continue
}

err = machineConfig.CompressQemuDiskImage()
if err != nil {
errs[i] = utils.CmdResult{Name: vmName, Err: err}
continue
}

files := []string{}
for _, f := range fileInfo {
if !utils.StringSliceContains([]string{"alpine.qmp", "alpine.sock", "alpine.pid"}, f.Name()) {
Expand Down
56 changes: 56 additions & 0 deletions qemu/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,62 @@ func (c *MachineConfig) Launch() error {
return nil
}

// CompressQemuDiskImage compresses the QEMU Disk image and overwrites it
func (c *MachineConfig) CompressQemuDiskImage() error {
if !utils.CommandExists("qemu-img") {
return errors.New("qemu-img is not available on $PATH. ensure qemu is installed")
}

cmd := exec.Command("qemu-img", "convert", "-c", "-O", "qcow2", filepath.Join(c.Location, c.Image),
filepath.Join(c.Location, c.Image+"_compressed.qcow2"))

err := cmd.Run()
if err != nil {
return err
}

err = os.Remove(filepath.Join(c.Location, c.Image))
if err != nil {
return err
}

err = os.Rename(filepath.Join(c.Location, c.Image+"_compressed.qcow2"),
filepath.Join(c.Location, c.Image))
if err != nil {
return err
}

return nil
}

// DecompressQemuDiskImage decompresses the QEMU Disk image and overwrites it
func (c *MachineConfig) DecompressQemuDiskImage() error {
if !utils.CommandExists("qemu-img") {
return errors.New("qemu-img is not available on $PATH. ensure qemu is installed")
}

cmd := exec.Command("qemu-img", "convert", "-O", "qcow2", "-p", filepath.Join(c.Location, c.Image),
filepath.Join(c.Location, c.Image+"_decompressed.qcow2"))

err := cmd.Run()
if err != nil {
return err
}

err = os.Remove(filepath.Join(c.Location, c.Image))
if err != nil {
return err
}

err = os.Rename(filepath.Join(c.Location, c.Image+"_decompressed.qcow2"),
filepath.Join(c.Location, c.Image))
if err != nil {
return err
}

return nil
}

// ResizeQemuDiskImage resizes a qcow2 disk image
func (c *MachineConfig) ResizeQemuDiskImage() error {
if !utils.CommandExists("qemu-img") {
Expand Down

0 comments on commit 6181343

Please sign in to comment.