aarch64 EFI image management
This commit is contained in:
parent
e7605dc289
commit
50df5197d4
6 changed files with 156 additions and 33 deletions
|
|
@ -1,10 +1,13 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"compress/bzip2"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/AlexSSD7/linsk/constants"
|
||||
"github.com/AlexSSD7/linsk/imgbuilder"
|
||||
|
|
@ -41,7 +44,7 @@ func (s *Storage) CheckDownloadBaseImage() (string, error) {
|
|||
}
|
||||
|
||||
// Image doesn't exist. Download one.
|
||||
err := s.download(constants.GetAlpineBaseImageURL(), constants.GetAlpineBaseImageHash(), baseImagePath)
|
||||
err := s.download(constants.GetAlpineBaseImageURL(), constants.GetAlpineBaseImageHash(), baseImagePath, nil)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "download base alpine image")
|
||||
}
|
||||
|
|
@ -62,34 +65,30 @@ func (s *Storage) GetVMImagePath() string {
|
|||
return filepath.Join(s.path, constants.GetVMImageTags()+".qcow2")
|
||||
}
|
||||
|
||||
func (s *Storage) GetAarch64EFIImagePath() string {
|
||||
return filepath.Join(s.path, constants.GetAarch64EFIImageName())
|
||||
}
|
||||
|
||||
func (s *Storage) BuildVMImageWithInterruptHandler(showBuilderVMDisplay bool, overwrite bool) error {
|
||||
var overwriting bool
|
||||
vmImagePath := s.GetVMImagePath()
|
||||
_, err := os.Stat(vmImagePath)
|
||||
removed, err := checkExistsOrRemove(vmImagePath, overwrite)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return errors.Wrap(err, "stat vm image path")
|
||||
}
|
||||
} else {
|
||||
if overwrite {
|
||||
overwriting = true
|
||||
err = os.Remove(vmImagePath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "remove existing vm image")
|
||||
}
|
||||
} else {
|
||||
return ErrImageAlreadyExists
|
||||
}
|
||||
return errors.Wrap(err, "check exists or remove")
|
||||
}
|
||||
|
||||
baseImagePath, err := s.CheckDownloadBaseImage()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "check download base image")
|
||||
return errors.Wrap(err, "check/download base image")
|
||||
}
|
||||
|
||||
s.logger.Info("Building VM image", "tags", constants.GetAlpineBaseImageTags(), "overwriting", overwriting, "dst", vmImagePath)
|
||||
biosPath, err := s.CheckDownloadCPUArchSpecifics()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "check/download cpu arch specifics")
|
||||
}
|
||||
|
||||
buildCtx, err := imgbuilder.NewBuildContext(s.logger.With("subcaller", "imgbuilder"), baseImagePath, vmImagePath, showBuilderVMDisplay)
|
||||
s.logger.Info("Building VM image", "tags", constants.GetAlpineBaseImageTags(), "overwriting", removed, "dst", vmImagePath)
|
||||
|
||||
buildCtx, err := imgbuilder.NewBuildContext(s.logger.With("subcaller", "imgbuilder"), baseImagePath, vmImagePath, showBuilderVMDisplay, biosPath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "create new img build context")
|
||||
}
|
||||
|
|
@ -116,3 +115,44 @@ func (s *Storage) CheckVMImageExists() (string, error) {
|
|||
func (s *Storage) DataDirPath() string {
|
||||
return s.path
|
||||
}
|
||||
|
||||
func (s *Storage) CheckDownloadCPUArchSpecifics() (string, error) {
|
||||
if runtime.GOARCH == "arm64" {
|
||||
p, err := s.CheckDownloadAarch64EFIImage()
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "check/download aarch64 efi image")
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (s *Storage) CheckDownloadAarch64EFIImage() (string, error) {
|
||||
efiImagePath := s.GetAarch64EFIImagePath()
|
||||
_, err := os.Stat(efiImagePath)
|
||||
if err != nil {
|
||||
if !errors.Is(err, os.ErrNotExist) {
|
||||
return "", errors.Wrap(err, "stat base image path")
|
||||
}
|
||||
|
||||
// EFI image doesn't exist. Download one.
|
||||
err := s.download(constants.GetAarch64EFIImageBZ2URL(), constants.GetAarch64EFIImageHash(), efiImagePath, func(r io.Reader) io.Reader {
|
||||
return bzip2.NewReader(r)
|
||||
})
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "download base alpine image")
|
||||
}
|
||||
|
||||
return efiImagePath, nil
|
||||
}
|
||||
|
||||
// EFI image exists. Ensure that the hash is correct.
|
||||
err = validateFileHash(efiImagePath, constants.GetAarch64EFIImageHash())
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "validate hash of existing image")
|
||||
}
|
||||
|
||||
return efiImagePath, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue