fix: quote ISO kernel args for GRUB

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>
This commit is contained in:
Andrey Smirnov 2023-04-06 21:12:07 +04:00
parent 319d76e389
commit 2c55550a66
No known key found for this signature in database
GPG Key ID: 7B26396447AB6DFD
7 changed files with 18 additions and 26 deletions

View File

@ -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 \
@ -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 \
@ -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.

View File

@ -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
}

View File

@ -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"
@ -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
}

View File

@ -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 {

View File

@ -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)

View File

@ -1,15 +0,0 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package grub
// Quote exported for testing.
func Quote(s string) string {
return quote(s)
}
// Unquote exported for testing.
func Unquote(s string) string {
return unquote(s)
}

View File

@ -8,10 +8,10 @@ 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))
}
@ -19,8 +19,8 @@ func quote(s string) string {
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))
}