-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: install bootloader to block device (#455)
Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
- Loading branch information
1 parent
6ae6118
commit 31a00ef
Showing
13 changed files
with
190 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* 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 bootloader | ||
|
||
// Bootloader describes a bootloader. | ||
type Bootloader interface { | ||
Prepare(string) error | ||
Install(string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* 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 syslinux | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"text/template" | ||
|
||
"github.com/autonomy/talos/internal/pkg/constants" | ||
"github.com/autonomy/talos/internal/pkg/version" | ||
) | ||
|
||
const extlinuxConfig = `DEFAULT Talos | ||
SAY Talos ({{ .Version }}) by Autonomy | ||
LABEL Talos | ||
KERNEL /vmlinuz | ||
INITRD /initramfs.xz | ||
APPEND {{ .Append }}` | ||
|
||
const gptmbrbin = "/usr/share/gptmbr.bin" | ||
|
||
// Syslinux represents the syslinux bootloader. | ||
type Syslinux struct{} | ||
|
||
// Prepare implements the Bootloader interface. It works by invoking writing | ||
// gptmbr.bin to a block device. | ||
func Prepare(dev string) (err error) { | ||
b, err := ioutil.ReadFile(gptmbrbin) | ||
if err != nil { | ||
return err | ||
} | ||
f, err := os.OpenFile(dev, os.O_WRONLY, os.ModeDevice) | ||
if err != nil { | ||
return err | ||
} | ||
// nolint: errcheck | ||
defer f.Close() | ||
if _, err := f.Write(b); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Install implements the Bootloader interface. It sets up extlinux with the | ||
// specified kernel parameters. | ||
func Install(args string) (err error) { | ||
aux := struct { | ||
Version string | ||
Append string | ||
}{ | ||
Version: version.Tag, | ||
Append: args, | ||
} | ||
|
||
b := []byte{} | ||
wr := bytes.NewBuffer(b) | ||
t := template.Must(template.New("extlinux").Parse(extlinuxConfig)) | ||
if err = t.Execute(wr, aux); err != nil { | ||
return err | ||
} | ||
|
||
if err = os.MkdirAll(constants.NewRoot+"/boot/extlinux", os.ModeDir); err != nil { | ||
return err | ||
} | ||
|
||
log.Println("writing extlinux.conf to disk") | ||
if err = ioutil.WriteFile(constants.NewRoot+"/boot/extlinux/extlinux.conf", wr.Bytes(), 0600); err != nil { | ||
return err | ||
} | ||
|
||
if err = cmd("extlinux", "--install", constants.NewRoot+"/boot/extlinux"); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func cmd(name string, args ...string) error { | ||
cmd := exec.Command(name, args...) | ||
err := cmd.Start() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cmd.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters