Skip to content

Commit

Permalink
Merge pull request #723 from monstermunchkin/issues/656-vyos
Browse files Browse the repository at this point in the history
sources: Add VyOS
  • Loading branch information
stgraber authored Jun 13, 2023
2 parents 6d7e8fc + a432a20 commit d1477e2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions shared/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ func (d *Definition) Validate() error {
"funtoo-http",
"rootfs-http",
"rockylinux-http",
"vyos-http",
}

if !shared.StringInSlice(strings.TrimSpace(d.Source.Downloader), validDownloaders) {
Expand Down
3 changes: 2 additions & 1 deletion sources/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var downloaders = map[string]func() downloader{
"fedora-http": func() downloader { return &fedora{} },
"funtoo-http": func() downloader { return &funtoo{} },
"gentoo-http": func() downloader { return &gentoo{} },
"openeuler-http": func() downloader { return &openEuler{} },
"opensuse-http": func() downloader { return &opensuse{} },
"openwrt-http": func() downloader { return &openwrt{} },
"oraclelinux-http": func() downloader { return &oraclelinux{} },
Expand All @@ -45,7 +46,7 @@ var downloaders = map[string]func() downloader{
"springdalelinux-http": func() downloader { return &springdalelinux{} },
"ubuntu-http": func() downloader { return &ubuntu{} },
"voidlinux-http": func() downloader { return &voidlinux{} },
"openeuler-http": func() downloader { return &openEuler{} },
"vyos-http": func() downloader { return &vyos{} },
}

// Load loads and initializes a downloader.
Expand Down
88 changes: 88 additions & 0 deletions sources/vyos-http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package sources

import (
"fmt"
"os"
"path/filepath"

"golang.org/x/sys/unix"

"github.com/lxc/distrobuilder/shared"
)

type vyos struct {
common
}

func (s *vyos) Run() error {
isoURL := "https://s3-us.vyos.io/rolling/current/vyos-rolling-latest.iso"

fpath, err := s.DownloadHash(s.definition.Image, isoURL, "", nil)
if err != nil {
return fmt.Errorf("Failed downloading ISO: %w", err)
}

err = s.unpackISO(filepath.Join(fpath, "vyos-rolling-latest.iso"), s.rootfsDir)
if err != nil {
return fmt.Errorf("Failed unpacking ISO: %w", err)
}

return nil
}

func (s *vyos) unpackISO(filePath string, rootfsDir string) error {
isoDir, err := os.MkdirTemp(s.cacheDir, "temp_")
if err != nil {
return fmt.Errorf("Failed creating temporary directory: %w", err)
}

defer os.RemoveAll(isoDir)

squashfsDir, err := os.MkdirTemp(s.cacheDir, "temp_")
if err != nil {
return fmt.Errorf("Failed creating temporary directory: %w", err)
}

defer os.RemoveAll(squashfsDir)

// this is easier than doing the whole loop thing ourselves
err = shared.RunCommand(s.ctx, nil, nil, "mount", "-o", "ro", filePath, isoDir)
if err != nil {
return fmt.Errorf("Failed mounting %q: %w", filePath, err)
}

defer func() {
_ = unix.Unmount(isoDir, 0)
}()

squashfsImage := filepath.Join(isoDir, "live", "filesystem.squashfs")

// The squashfs.img contains an image containing the rootfs, so first
// mount squashfs.img
err = shared.RunCommand(s.ctx, nil, nil, "mount", "-o", "ro", squashfsImage, squashfsDir)
if err != nil {
return fmt.Errorf("Failed mounting %q: %w", squashfsImage, err)
}

defer func() {
_ = unix.Unmount(squashfsDir, 0)
}()

// Remove rootfsDir otherwise rsync will copy the content into the directory
// itself
err = os.RemoveAll(rootfsDir)
if err != nil {
return fmt.Errorf("Failed removing directory %q: %w", rootfsDir, err)
}

s.logger.WithField("file", squashfsImage).Info("Unpacking root image")

// Since rootfs is read-only, we need to copy it to a temporary rootfs
// directory in order to create the minimal rootfs.
err = shared.RsyncLocal(s.ctx, squashfsDir+"/", rootfsDir)
if err != nil {
return fmt.Errorf("Failed running rsync: %w", err)
}

return nil
}

0 comments on commit d1477e2

Please sign in to comment.