From 2a83612ffbf3af80314fc381b74162657ed837b8 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Tue, 7 Feb 2017 14:50:00 -0700 Subject: [PATCH] matchbox/http: Update grub endpoint to use profile kernel args --- Documentation/network-setup.md | 13 +++++++++- matchbox/http/grub.go | 13 +++++++--- matchbox/http/grub_test.go | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 matchbox/http/grub_test.go diff --git a/Documentation/network-setup.md b/Documentation/network-setup.md index 6762b4ea..0548eafc 100644 --- a/Documentation/network-setup.md +++ b/Documentation/network-setup.md @@ -57,7 +57,18 @@ You can chainload from a menu entry or use other [iPXE commands](http://ipxe.org ## GRUB -Needs docs. +Needs more docs. + +`grub-mknetdir --net-directory=/var/lib/tftpboot` + +/var/lib/tftpboot/boot/grub/grub.cfg: +```ini +insmod i386-pc/http.mod +set root=http,matchbox.foo:8080 +configfile /grub +``` + +Make sure to replace variables in the example config files; instead of iPXE variables, use GRUB variables. Check the [GRUB2 manual](https://www.gnu.org/software/grub/manual/grub.html#Network). ### Configuring DHCP diff --git a/matchbox/http/grub.go b/matchbox/http/grub.go index 600b8fa6..ab2e8a93 100644 --- a/matchbox/http/grub.go +++ b/matchbox/http/grub.go @@ -10,12 +10,19 @@ import ( ) var grubTemplate = template.Must(template.New("GRUB2 config").Parse(`default=0 +fallback=1 timeout=1 -menuentry "CoreOS" { +menuentry "CoreOS (EFI)" { echo "Loading kernel" -linuxefi "{{.Kernel}}"{{range $key, $value := .Cmdline}} {{if $value}}"{{$key}}={{$value}}"{{else}}"{{$key}}"{{end}}{{end}} +linuxefi "{{.Kernel}}"{{range $arg := .Args}} {{$arg}}{{end}}{{range $key, $value := .Cmdline}} {{if $value}}"{{$key}}={{$value}}"{{else}}"{{$key}}"{{end}}{{end}} echo "Loading initrd" -initrdefi {{ range $element := .Initrd }}"{{$element}}" {{end}} +initrdefi {{ range $element := .Initrd }} "{{$element}}"{{end}} +} +menuentry "CoreOS (BIOS)" { +echo "Loading kernel" +linux "{{.Kernel}}"{{range $arg := .Args}} {{$arg}}{{end}}{{range $key, $value := .Cmdline}} {{if $value}}"{{$key}}={{$value}}"{{else}}"{{$key}}"{{end}}{{end}} +echo "Loading initrd" +initrd {{ range $element := .Initrd }} "{{$element}}"{{end}} } `)) diff --git a/matchbox/http/grub_test.go b/matchbox/http/grub_test.go new file mode 100644 index 00000000..9c62503f --- /dev/null +++ b/matchbox/http/grub_test.go @@ -0,0 +1,43 @@ +package http + +import ( + "net/http" + "net/http/httptest" + "testing" + + "context" + logtest "github.com/Sirupsen/logrus/hooks/test" + "github.com/stretchr/testify/assert" + + fake "github.com/coreos/matchbox/matchbox/storage/testfakes" +) + +func TestGrubHandler(t *testing.T) { + logger, _ := logtest.NewNullLogger() + srv := NewServer(&Config{Logger: logger}) + h := srv.grubHandler() + ctx := withProfile(context.Background(), fake.Profile) + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/", nil) + h.ServeHTTP(ctx, w, req) + // assert that: + // - the Profile's NetBoot config is rendered as a GRUB2 config + expectedScript := `default=0 +fallback=1 +timeout=1 +menuentry "CoreOS (EFI)" { +echo "Loading kernel" +linuxefi "/image/kernel" a=b c +echo "Loading initrd" +initrdefi "/image/initrd_a" "/image/initrd_b" +} +menuentry "CoreOS (BIOS)" { +echo "Loading kernel" +linux "/image/kernel" a=b c +echo "Loading initrd" +initrd "/image/initrd_a" "/image/initrd_b" +} +` + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, expectedScript, w.Body.String()) +}