forked from tinkerbell/actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (69 loc) · 1.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"io"
"os"
"os/exec"
log "github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)
func main() {
fmt.Printf("SYSLINUX - Boot Loader Installation\n------------------------\n")
disk := os.Getenv("DEST_DISK")
partition := os.Getenv("DEST_PARTITION")
ver := os.Getenv("SYSLINUX_VERSION")
switch ver {
case "386", "3.86":
syslinux386(disk, partition)
default:
log.Fatalf("Unknown syslinux version [%s]", ver)
}
}
func syslinux386(disk, partition string) {
log.Infof("Writing mbr to [%s] and installing boot loader to [%s]", disk, partition)
// Open the block device and write the Master boot record
blockOut, err := os.OpenFile(disk, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
log.Fatalln(err)
}
_ = ReReadPartitionTable(blockOut)
defer blockOut.Close()
mbrIn, err := os.OpenFile("/mbr.bin.386", os.O_RDONLY, 0o644)
if err != nil {
log.Fatalln(err) //nolint:gocritic // this is fine
}
defer func() { _ = mbrIn.Close() }()
_, err = io.Copy(blockOut, mbrIn)
if err != nil {
log.Fatalln(err)
}
_, err = os.Stat(partition)
if err != nil {
log.Fatalln(err)
}
cmd := exec.Command("/syslinux.386", partition)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err = cmd.Start()
if err != nil {
log.Fatalf("Error starting [%v]", err)
}
err = cmd.Wait()
if err != nil {
log.Fatalf("Error running [%v]", err)
}
}
const (
BLKRRPART = 0x125f
)
// ReReadPartitionTable forces the kernel to re-read the partition table
// on the disk.
//
// It is done via an ioctl call with request as BLKRRPART.
func ReReadPartitionTable(d *os.File) error {
fd := d.Fd()
_, err := unix.IoctlGetInt(int(fd), BLKRRPART)
if err != nil {
return fmt.Errorf("unable to re-read partition table: %w", err)
}
return nil
}