diff --git a/Dockerfile b/Dockerfile index ff0008e..50dd8e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -FROM golang:1.25 as builder +FROM golang:1.25 AS builder WORKDIR /d2vm diff --git a/bootloader.go b/bootloader.go index ed824e0..6346569 100644 --- a/bootloader.go +++ b/bootloader.go @@ -40,4 +40,5 @@ type BootloaderProvider interface { type Bootloader interface { Validate(fs BootFS) error Setup(ctx context.Context, dev, root, cmdline string) error + Static() bool } diff --git a/builder.go b/builder.go index 8ed0910..be859a3 100644 --- a/builder.go +++ b/builder.go @@ -165,6 +165,11 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size uint64, return nil, err } + if !bl.Static() { + config.Kernel = "" + config.Initrd = "" + } + if size == 0 { size = 10 * uint64(datasize.GB) } diff --git a/config.go b/config.go index 12da528..10b7b96 100644 --- a/config.go +++ b/config.go @@ -60,11 +60,21 @@ type Config struct { } func (c Config) Cmdline(root Root, args ...string) string { - var r string - if root != nil { - r = fmt.Sprintf("root=%s", root.String()) + cmdline := "net.ifnames=0 rootfstype=ext4 console=tty0 console=ttyS0,115200n8" + if c.Kernel == "" && c.Initrd == "" { + return cmdline } - return fmt.Sprintf("ro initrd=%s %s net.ifnames=0 rootfstype=ext4 console=tty0 console=ttyS0,115200n8 %s", c.Initrd, r, strings.Join(args, " ")) + parts := []string{"ro"} + if root != nil { + parts = append(parts, fmt.Sprintf("root=%s", root.String())) + } + if c.Initrd != "" { + parts = append(parts, fmt.Sprintf("initrd=%s", c.Initrd)) + } + if len(args) != 0 { + parts = append(parts, args...) + } + return strings.Join(append(parts, cmdline), " ") } func (r OSRelease) Config() (Config, error) { diff --git a/grub.go b/grub.go index e54ae97..88f1344 100644 --- a/grub.go +++ b/grub.go @@ -57,14 +57,14 @@ type grubProvider struct { config Config } -func (g grubProvider) New(c Config, r OSRelease, arch string) (Bootloader, error) { +func (g grubProvider) New(_ Config, r OSRelease, arch string) (Bootloader, error) { if arch != "x86_64" { return nil, fmt.Errorf("grub is only supported for amd64") } if err := checkGrubEFISupport(r); err != nil { return nil, err } - return grub{grubCommon: newGrubCommon(c, r)}, nil + return grub{grubCommon: newGrubCommon(r)}, nil } func (g grubProvider) Name() string { diff --git a/grub_bios.go b/grub_bios.go index 57143ee..563ad0c 100644 --- a/grub_bios.go +++ b/grub_bios.go @@ -45,15 +45,13 @@ func (g grubBios) Setup(ctx context.Context, dev, root string, cmdline string) e return nil } -type grubBiosProvider struct { - config Config -} +type grubBiosProvider struct{} -func (g grubBiosProvider) New(c Config, r OSRelease, arch string) (Bootloader, error) { +func (g grubBiosProvider) New(_ Config, r OSRelease, arch string) (Bootloader, error) { if arch != "x86_64" { return nil, fmt.Errorf("grub-bios is only supported for amd64") } - return grubBios{grubCommon: newGrubCommon(c, r)}, nil + return grubBios{grubCommon: newGrubCommon(r)}, nil } func (g grubBiosProvider) Name() string { diff --git a/grub_common.go b/grub_common.go index cb25d3f..35f0efd 100644 --- a/grub_common.go +++ b/grub_common.go @@ -38,20 +38,18 @@ GRUB_ENABLE_BLSCFG=false type grubCommon struct { name string - c Config r OSRelease root string dev string } -func newGrubCommon(c Config, r OSRelease) *grubCommon { +func newGrubCommon(r OSRelease) *grubCommon { name := "grub" if r.ID == ReleaseCentOS || r.ID == ReleaseAlmaLinux || r.ID == ReleaseRocky { name = "grub2" } return &grubCommon{ name: name, - c: c, r: r, } } @@ -102,3 +100,7 @@ func (g *grubCommon) mkconfig(ctx context.Context) error { } return exec.Run(ctx, "chroot", g.root, g.name+"-mkconfig", "-o", "/boot/"+g.name+"/grub.cfg") } + +func (g *grubCommon) Static() bool { + return false +} diff --git a/grub_efi.go b/grub_efi.go index c544c97..9e85030 100644 --- a/grub_efi.go +++ b/grub_efi.go @@ -53,15 +53,13 @@ func (g grubEFI) Setup(ctx context.Context, dev, root string, cmdline string) er return nil } -type grubEFIProvider struct { - config Config -} +type grubEFIProvider struct{} -func (g grubEFIProvider) New(c Config, r OSRelease, arch string) (Bootloader, error) { +func (g grubEFIProvider) New(_ Config, r OSRelease, arch string) (Bootloader, error) { if err := checkGrubEFISupport(r); err != nil { return nil, err } - return grubEFI{grubCommon: newGrubCommon(c, r), arch: arch}, nil + return grubEFI{grubCommon: newGrubCommon(r), arch: arch}, nil } func (g grubEFIProvider) Name() string { diff --git a/syslinux.go b/syslinux.go index 759cba5..d888635 100644 --- a/syslinux.go +++ b/syslinux.go @@ -91,6 +91,10 @@ func (s syslinuxProvider) New(c Config, _ OSRelease, arch string) (Bootloader, e }, nil } +func (s syslinux) Static() bool { + return true +} + func (s syslinuxProvider) Name() string { return "syslinux" }