/
parttable.go
210 lines (180 loc) · 4.97 KB
/
parttable.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"encoding/binary"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"strconv"
"syscall"
"golang.org/x/sys/unix"
)
var (
active = byte(0x80)
inactive = byte(0x00)
// invalidCHS results in using the sector values instead
invalidCHS = [3]byte{0xFE, 0xFF, 0xFF}
FAT = byte(0xc)
Linux = byte(0x83)
SquashFS = Linux // SquashFS does not have a dedicated type
signature = uint16(0xAA55)
)
func writePartitionTable(w io.Writer, devsize uint64) error {
for _, v := range []interface{}{
[446]byte{}, // boot code
// partition 1
active,
invalidCHS,
FAT,
invalidCHS,
uint32(8192), // start at 8192 sectors
uint32(100 * MB / 512), // 100MB in size
// partition 2
inactive,
invalidCHS,
SquashFS,
invalidCHS,
uint32(8192 + (100 * MB / 512)), // start after partition 1
uint32(500 * MB / 512), // 500MB in size
// partition 3
inactive,
invalidCHS,
SquashFS,
invalidCHS,
uint32(8192 + (600 * MB / 512)), // start after partition 2
uint32(500 * MB / 512), // 500MB in size
// partition 4
inactive,
invalidCHS,
Linux,
invalidCHS,
uint32(8192 + (1100 * MB / 512)), // start after partition 3
uint32((devsize / 512) - 8192 - (1100 * MB / 512)), // remainder
signature,
} {
if err := binary.Write(w, binary.LittleEndian, v); err != nil {
return err
}
}
return nil
}
func partitionDevice(o *os.File, path string) error {
devsize, err := deviceSize(uintptr(o.Fd()))
if err != nil {
return err
}
log.Printf("device holds %d bytes", devsize)
if devsize == 0 {
return fmt.Errorf("path %s does not seem to be a device", path)
}
if err := writePartitionTable(o, devsize); err != nil {
return err
}
// Make Linux re-read the partition table. Sequence of system calls like in fdisk(8).
unix.Sync()
if err := rereadPartitions(uintptr(o.Fd())); err != nil {
log.Printf("Re-reading partition table failed: %v. Remember to unplug and re-plug the SD card before creating a file system for persistent data, if desired.", err)
}
if err := o.Sync(); err != nil {
return err
}
unix.Sync()
return nil
}
func mustUnixConn(fd uintptr) *net.UnixConn {
fc, err := net.FileConn(os.NewFile(fd, ""))
if err != nil {
panic(err)
}
return fc.(*net.UnixConn)
}
func sudoPartition(path string) (*os.File, error) {
if fd, err := strconv.Atoi(os.Getenv("GOKR_PACKER_FD")); err == nil {
// child process
conn := mustUnixConn(uintptr(fd))
f, err := os.Create(path)
if err != nil {
return nil, err
}
if err := partitionDevice(f, path); err != nil {
return nil, err
}
_, _, err = conn.WriteMsgUnix(nil, syscall.UnixRights(int(f.Fd())), nil)
return nil, err
}
pair, err := syscall.Socketpair(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
if err != nil {
return nil, err
}
syscall.CloseOnExec(pair[0]) // used in the parent process
// Use absolute path because $PATH might not be the same when using sudo:
if exe, err := os.Executable(); err == nil {
os.Args[0] = exe
}
cmd := exec.Command("sudo", append([]string{"--preserve-env"}, os.Args...)...)
// We cannot use cmd.ExtraFiles with sudo, as sudo closes all file
// descriptors but stdin, stdout and stderr.
cmd.Env = []string{"GOKR_PACKER_FD=1"}
cmd.Stdout = os.NewFile(uintptr(pair[1]), "")
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return nil, err
}
go func() {
err := cmd.Wait()
if err != nil {
log.Fatal(err)
}
}()
// receive file descriptor
conn := mustUnixConn(uintptr(pair[0]))
// 32 bytes as per
// https://github.com/golang/go/blob/21d2e15ee1bed44a7a1b8f775aff4a57cae9533a/src/syscall/syscall_unix_test.go#L177
oob := make([]byte, 32)
_, oobn, flags, _, err := conn.ReadMsgUnix(nil, oob)
if err != nil {
return nil, err
}
if flags != 0 || oobn <= 0 {
return nil, fmt.Errorf("ReadMsgUnix: flags != 0 || oobn <= 0")
}
// file descriptors are now open in this process
scm, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return nil, err
}
if got, want := len(scm), 1; got != want {
return nil, fmt.Errorf("SCM message: got %d, want %d", got, want)
}
fds, err := syscall.ParseUnixRights(&scm[0])
if err != nil {
return nil, err
}
if got, want := len(fds), 1; got != want {
return nil, fmt.Errorf("ParseUnixRights: got %d fds, want %d fds", got, want)
}
return os.NewFile(uintptr(fds[0]), ""), nil
}
func partition(path string) (*os.File, error) {
if *sudo == "always" {
return sudoPartition(path)
}
o, err := os.Create(path)
if err != nil {
pe, ok := err.(*os.PathError)
if ok && pe.Err == syscall.EACCES && *sudo == "auto" {
// permission denied
log.Printf("Using sudo to gain permission to format %s", path)
log.Printf("If you prefer, cancel and use: sudo setfacl -m u:${USER}:rw %s", path)
return sudoPartition(path)
}
if ok && pe.Err == syscall.EROFS {
log.Printf("%s read-only; check if you have a physical write-protect switch on your SD card?", path)
return nil, err
}
return nil, err
}
return o, partitionDevice(o, path)
}