*: Fix tarballs and add compression

Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
This commit is contained in:
Thomas Hipp 2018-03-07 10:44:33 +01:00
parent a5efaa581b
commit f70b4107b0
No known key found for this signature in database
GPG Key ID: 993408D1137B7D51
4 changed files with 50 additions and 11 deletions

View File

@ -83,7 +83,7 @@ func (c *cmdLXD) run(cmd *cobra.Command, args []string) error {
}
}
err := img.Build(c.flagType == "unified")
err := img.Build(c.flagType == "unified", c.flagCompression)
if err != nil {
return fmt.Errorf("Failed to create LXD image: %s", err)
}

View File

@ -73,7 +73,7 @@ func (l *LXCImage) Build() error {
return err
}
err = shared.Pack(filepath.Join(l.targetDir, "rootfs.tar.xz"), l.sourceDir,
err = shared.Pack(filepath.Join(l.targetDir, "rootfs.tar"), "xz", l.sourceDir,
"--transform", "s,^./,rootfs/,", ".")
if err != nil {
return err
@ -143,7 +143,7 @@ func (l *LXCImage) packMetadata() error {
files = append(files, "templates")
}
err := shared.Pack(filepath.Join(l.targetDir, "meta.tar.xz"),
err := shared.Pack(filepath.Join(l.targetDir, "meta.tar"), "xz",
filepath.Join(l.cacheDir, "metadata"), files...)
if err != nil {
return fmt.Errorf("Failed to create metadata: %s", err)

View File

@ -41,7 +41,7 @@ func NewLXDImage(sourceDir, targetDir, cacheDir string,
}
// Build creates a LXD image.
func (l *LXDImage) Build(unified bool) error {
func (l *LXDImage) Build(unified bool, compression string) error {
err := l.createMetadata()
if err != nil {
return nil
@ -86,12 +86,20 @@ func (l *LXDImage) Build(unified bool) error {
fname = "lxd"
}
paths = append(paths, "rootfs")
err = shared.Pack(filepath.Join(l.targetDir, fmt.Sprintf("%s.tar.xz", fname)),
l.sourceDir, paths...)
// Add the rootfs to the tarball, prefix all files with "rootfs"
err = shared.Pack(filepath.Join(l.targetDir, fmt.Sprintf("%s.tar", fname)),
"", l.sourceDir, "--transform", "s,^./,rootfs/,", ".")
if err != nil {
return err
}
// Add the metadata to the tarball which is located in the cache directory
err = shared.PackUpdate(filepath.Join(l.targetDir, fmt.Sprintf("%s.tar", fname)),
compression, l.cacheDir, paths...)
if err != nil {
return err
}
} else {
// Create rootfs as squashfs.
err = shared.RunCommand("mksquashfs", l.sourceDir,
@ -101,7 +109,8 @@ func (l *LXDImage) Build(unified bool) error {
}
// Create metadata tarball.
err = shared.Pack(filepath.Join(l.targetDir, "lxd.tar.xz"), l.cacheDir, paths...)
err = shared.Pack(filepath.Join(l.targetDir, "lxd.tar"), compression,
l.cacheDir, paths...)
if err != nil {
return err
}

View File

@ -120,9 +120,39 @@ func CreateGPGKeyring(keyserver string, keys []string) (string, error) {
return filepath.Join(gpgDir, "distrobuilder.gpg"), nil
}
// Pack creates an xz-compressed tarball.
func Pack(filename, path string, args ...string) error {
return RunCommand("tar", append([]string{"-cJf", filename, "-C", path}, args...)...)
// Pack creates an uncompressed tarball.
func Pack(filename, compression, path string, args ...string) error {
err := RunCommand("tar", append([]string{"-cf", filename, "-C", path}, args...)...)
if err != nil {
return err
}
return compressTarball(filename, compression)
}
// PackUpdate updates an existing tarball.
func PackUpdate(filename, compression, path string, args ...string) error {
err := RunCommand("tar", append([]string{"-uf", filename, "-C", path}, args...)...)
if err != nil {
return err
}
return compressTarball(filename, compression)
}
// compressTarball compresses a tarball, or not.
func compressTarball(filename, compression string) error {
switch compression {
case "lzop":
// lzo does not remove the uncompressed file per default
defer os.Remove(filename)
fallthrough
case "bzip2", "xz", "lzip", "lzma", "gzip":
return RunCommand(compression, "-f", filename)
}
// Do not compress
return nil
}
//GetExpiryDate returns an expiry date based on the creationDate and format.