Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sources/rootfs: Support local image files #571

Merged
merged 2 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ It needs to be one of
* voidlinux-http

The `url` field defines the URL or mirror of the rootfs image.
Although this field is not required, most downloaders will need it.
Although this field is not required, most downloaders will need it. The `rootfs-http` downloader also supports local image files when prefixed with `file://`, e.g. `url: file:///home/user/image.tar.gz` or `url: file:///home/user/image.squashfs`.

The `keys` field is a list of GPG keys.
These keys can be listed as fingerprints or armored keys.
Expand Down
27 changes: 22 additions & 5 deletions sources/rootfs-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sources

import (
"fmt"
"net/url"
"path"
"path/filepath"

Expand All @@ -15,17 +16,33 @@ type rootfs struct {

// Run downloads a tarball.
func (s *rootfs) Run() error {
fpath, err := shared.DownloadHash(s.definition.Image, s.definition.Source.URL, "", nil)
URL, err := url.Parse(s.definition.Source.URL)
if err != nil {
return fmt.Errorf("Failed to download %q: %w", s.definition.Source.URL, err)
return fmt.Errorf("Failed to parse URL: %w", err)
}

s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, path.Base(s.definition.Source.URL)))
var fpath string
var filename string

if URL.Scheme == "file" {
fpath = filepath.Dir(URL.Path)
filename = filepath.Base(URL.Path)
} else {
fpath, err = shared.DownloadHash(s.definition.Image, s.definition.Source.URL, "", nil)
if err != nil {
return fmt.Errorf("Failed to download %q: %w", s.
definition.Source.URL, err)
}

filename = path.Base(s.definition.Source.URL)
}

s.logger.Infow("Unpacking image", "file", filepath.Join(fpath, filename))

// Unpack
err = lxd.Unpack(filepath.Join(fpath, path.Base(s.definition.Source.URL)), s.rootfsDir, false, false, nil)
err = lxd.Unpack(filepath.Join(fpath, filename), s.rootfsDir, false, false, nil)
if err != nil {
return fmt.Errorf("Failed to unpack %q: %w", filepath.Join(fpath, path.Base(s.definition.Source.URL)), err)
return fmt.Errorf("Failed to unpack %q: %w", filepath.Join(fpath, filename), err)
}

return nil
Expand Down