Skip to content

Commit

Permalink
fix: quote ISO kernel args for GRUB
Browse files Browse the repository at this point in the history
Use GRUB quoting function to the kernel args passed to Talos.

This fixes passing `${variable}` to `talos.config=` kernel argument.

Also fix a problem with `ONBUILD` being exected for `imager` image.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
(cherry picked from commit 2c55550)
  • Loading branch information
smira committed Apr 11, 2023
1 parent 3600b64 commit 1f3c849
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 26 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ FROM install-artifacts-${INSTALLER_ARCH} AS install-artifacts
COPY --from=pkg-grub / /
COPY --from=unicode-pf2 /usr/share/grub/unicode.pf2 /usr/share/grub/unicode.pf2

FROM alpine:3.17.2 AS installer
FROM alpine:3.17.2 AS installer-image
RUN apk add --no-cache --update --no-scripts \
bash \
cpio \
Expand All @@ -686,6 +686,8 @@ ENV VERSION ${TAG}
LABEL "alpha.talos.dev/version"="${VERSION}"
LABEL org.opencontainers.image.source https://github.com/siderolabs/talos
ENTRYPOINT ["/bin/installer"]

FROM installer-image AS installer
ONBUILD RUN apk add --no-cache --update \
cpio \
squashfs-tools \
Expand All @@ -706,7 +708,7 @@ ONBUILD RUN find /rootfs \
&& rm -rf /initramfs
ONBUILD WORKDIR /

FROM installer AS imager
FROM installer-image AS imager

# The test target performs tests on the source code.

Expand Down
4 changes: 2 additions & 2 deletions cmd/installer/cmd/grub.iso.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ terminal_output console
menuentry "Talos ISO" {
set gfxmode=auto
set gfxpayload=text
linux /boot/vmlinuz {{ .Cmdline }}
linux /boot/vmlinuz {{ quote .Cmdline }}
initrd /boot/initramfs.xz
}

menuentry "Reset Talos installation" {
set gfxmode=auto
set gfxpayload=text
linux /boot/vmlinuz {{ .Cmdline }} talos.experimental.wipe=system
linux /boot/vmlinuz {{ quote .Cmdline }} talos.experimental.wipe=system
initrd /boot/initramfs.xz
}
7 changes: 6 additions & 1 deletion cmd/installer/cmd/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/spf13/cobra"

"github.com/siderolabs/talos/cmd/installer/pkg"
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime/v1alpha1/bootloader/grub"
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime/v1alpha1/platform/metal"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/kernel"
Expand Down Expand Up @@ -111,7 +112,11 @@ func runISOCmd() error {

var grubCfg bytes.Buffer

tmpl, err := template.New("grub.cfg").Parse(string(isoGrubCfg))
tmpl, err := template.New("grub.cfg").
Funcs(template.FuncMap{
"quote": grub.Quote,
}).
Parse(string(isoGrubCfg))
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func parseEntries(conf []byte) (map[BootLabel]MenuEntry, error) {
}

func parseConfBlock(block []byte) (linux, cmdline, initrd string, err error) {
block = []byte(unquote(string(block)))
block = []byte(Unquote(string(block)))

linuxMatches := linuxRegex.FindAllSubmatch(block, -1)
if len(linuxMatches) != 1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (c *Config) Encode(wr io.Writer) error {
}

t := template.Must(template.New("grub").Funcs(template.FuncMap{
"quote": quote,
"quote": Quote,
}).Parse(confTemplate))

return t.Execute(wr, c)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
"strings"
)

// quote according to (incomplete) GRUB quoting rules.
// Quote according to (incomplete) GRUB quoting rules.
//
// See https://www.gnu.org/software/grub/manual/grub/html_node/Shell_002dlike-scripting.html
func quote(s string) string {
func Quote(s string) string {
for _, c := range `\{}&$|;<>"` {
s = strings.ReplaceAll(s, string(c), `\`+string(c))
}

return s
}

// unquote according to (incomplete) GRUB quoting rules.
func unquote(s string) string {
// Unquote according to (incomplete) GRUB quoting rules.
func Unquote(s string) string {
for _, c := range `{}&$|;<>\"` {
s = strings.ReplaceAll(s, `\`+string(c), string(c))
}
Expand Down

0 comments on commit 1f3c849

Please sign in to comment.