mirror of
https://github.com/linka-cloud/d2vm.git
synced 2025-12-14 15:03:12 +00:00
add grub-efi support
* tests: increase timeout * ci: split e2e tests Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
77
grub.go
77
grub.go
@@ -17,89 +17,54 @@ package d2vm
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
exec2 "os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"go.linka.cloud/d2vm/pkg/exec"
|
||||
)
|
||||
|
||||
const grubCfg = `GRUB_DEFAULT=0
|
||||
GRUB_HIDDEN_TIMEOUT=0
|
||||
GRUB_HIDDEN_TIMEOUT_QUIET=true
|
||||
GRUB_TIMEOUT=0
|
||||
GRUB_CMDLINE_LINUX_DEFAULT="%s"
|
||||
GRUB_CMDLINE_LINUX=""
|
||||
GRUB_TERMINAL=console
|
||||
`
|
||||
|
||||
type grub struct {
|
||||
name string
|
||||
c Config
|
||||
r OSRelease
|
||||
*grubCommon
|
||||
}
|
||||
|
||||
func (g grub) Validate(fs BootFS) error {
|
||||
switch fs {
|
||||
case BootFSFat32:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("grub only supports fat32 boot filesystem due to grub-efi")
|
||||
}
|
||||
}
|
||||
|
||||
func (g grub) Setup(ctx context.Context, dev, root string, cmdline string) error {
|
||||
logrus.Infof("setting up grub bootloader")
|
||||
if err := os.WriteFile(filepath.Join(root, "etc", "default", "grub"), []byte(fmt.Sprintf(grubCfg, cmdline)), perm); err != nil {
|
||||
clean, err := g.prepare(ctx, dev, root, cmdline)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(root, "boot", g.name), os.ModePerm); err != nil {
|
||||
defer clean()
|
||||
if err := g.install(ctx, "--target=x86_64-efi", "--efi-directory=/boot", "--no-nvram", "--removable", "--no-floppy"); err != nil {
|
||||
return err
|
||||
}
|
||||
mounts := []string{"dev", "proc", "sys"}
|
||||
var unmounts []string
|
||||
defer func() {
|
||||
for _, v := range unmounts {
|
||||
if err := exec.Run(ctx, "umount", filepath.Join(root, v)); err != nil {
|
||||
logrus.Errorf("failed to unmount /%s: %s", v, err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
for _, v := range mounts {
|
||||
if err := exec.Run(ctx, "mount", "-o", "bind", "/"+v, filepath.Join(root, v)); err != nil {
|
||||
return err
|
||||
}
|
||||
unmounts = append(unmounts, v)
|
||||
}
|
||||
|
||||
if err := exec.Run(ctx, "chroot", root, g.name+"-install", "--target=i386-pc", "--boot-directory", "/boot", dev); err != nil {
|
||||
if err := g.install(ctx, "--target=i386-pc", "--boot-directory=/boot", dev); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := exec.Run(ctx, "chroot", root, g.name+"-mkconfig", "-o", "/boot/"+g.name+"/grub.cfg"); err != nil {
|
||||
if err := g.mkconfig(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type grubBootloaderProvider struct {
|
||||
type grubProvider struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
func (g grubBootloaderProvider) New(c Config, r OSRelease) (Bootloader, error) {
|
||||
name := "grub"
|
||||
if r.ID == "centos" {
|
||||
name = "grub2"
|
||||
}
|
||||
if _, err := exec2.LookPath("grub-install"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := exec2.LookPath("grub-mkconfig"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return grub{
|
||||
name: name,
|
||||
c: c,
|
||||
r: r,
|
||||
}, nil
|
||||
func (g grubProvider) New(c Config, r OSRelease) (Bootloader, error) {
|
||||
return grub{grubCommon: newGrubCommon(c, r)}, nil
|
||||
}
|
||||
|
||||
func (g grubBootloaderProvider) Name() string {
|
||||
func (g grubProvider) Name() string {
|
||||
return "grub"
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterBootloaderProvider(grubBootloaderProvider{})
|
||||
RegisterBootloaderProvider(grubProvider{})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user