Skip to content

Commit bffdefe

Browse files
committed
feat(format): allow custom partition layout specification in linuxkit/format
1 parent 812a9f1 commit bffdefe

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

pkg/format/format.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ const (
2020
)
2121

2222
var (
23-
labelVar string
24-
fsTypeVar string
25-
partTypeVar string
26-
forceVar bool
27-
verboseVar bool
28-
drives map[string]bool
29-
driveKeys []string
23+
labelVar string
24+
fsTypeVar string
25+
partTypeVar string
26+
forceVar bool
27+
verboseVar bool
28+
drives map[string]bool
29+
partitionLayoutVar string
30+
driveKeys []string
3031
)
3132

3233
func hasPartitions(d string) bool {
@@ -73,7 +74,7 @@ func isEmptyDevice(d string) (bool, error) {
7374
return isEmpty, nil
7475
}
7576

76-
func autoformat(label, fsType string, partType string) error {
77+
func autoformat(label, fsType string, partType string, partitionLayoutVar string) error {
7778
var first string
7879
for _, d := range driveKeys {
7980
if verboseVar {
@@ -94,7 +95,7 @@ func autoformat(label, fsType string, partType string) error {
9495
return fmt.Errorf("No eligible disks found")
9596
}
9697

97-
return format(first, label, fsType, partType, false)
98+
return format(first, label, fsType, partType, partitionLayoutVar, false)
9899
}
99100

100101
func refreshDevicesAndWaitFor(awaitedDevice string) error {
@@ -129,15 +130,15 @@ func refreshDevicesAndWaitFor(awaitedDevice string) error {
129130
return nil
130131
}
131132

132-
func format(d, label, fsType string, partType string, forced bool) error {
133+
func format(d, label, fsType string, partType string, partitionLayoutVar string, forced bool) error {
133134
if forced {
134135
// clear partitions on device if forced format and they exist
135136
if hasPartitions(d) {
136137
if verboseVar {
137138
log.Printf("Clearing partitions on %s because forced format was requested", d)
138139
}
139140
partCmd := exec.Command("sfdisk", "--quiet", "--delete", d)
140-
partCmd.Stdin = strings.NewReader(";")
141+
partCmd.Stdin = strings.NewReader(partitionLayoutVar)
141142
if out, err := partCmd.CombinedOutput(); err != nil {
142143
return fmt.Errorf("Error deleting partitions with sfdisk: %v\n%s", err, out)
143144
}
@@ -169,7 +170,7 @@ func format(d, label, fsType string, partType string, forced bool) error {
169170

170171
// format one large partition
171172
partCmd := exec.Command("sfdisk", "--quiet", d)
172-
partCmd.Stdin = strings.NewReader(";")
173+
partCmd.Stdin = strings.NewReader(partitionLayoutVar)
173174
if out, err := partCmd.CombinedOutput(); err != nil {
174175
return fmt.Errorf("Error running sfdisk: %v\n%s", err, out)
175176
}
@@ -267,6 +268,7 @@ func init() {
267268
flag.StringVar(&labelVar, "label", "", "Disk label to apply")
268269
flag.StringVar(&fsTypeVar, "type", "ext4", "Type of filesystem to create")
269270
flag.StringVar(&partTypeVar, "partition", "dos", "Type of partition table to create")
271+
flag.StringVar(&partitionLayoutVar, "layout", ";", "Partition layout for sfdisk invocation")
270272
flag.BoolVar(&verboseVar, "verbose", false, "Enable verbose output (default false)")
271273
}
272274

@@ -292,7 +294,7 @@ func main() {
292294
if flag.NArg() == 0 {
293295
// auto-detect drives if a device to format is not explicitly specified
294296
findDrives()
295-
if err := autoformat(labelVar, fsTypeVar, partTypeVar); err != nil {
297+
if err := autoformat(labelVar, fsTypeVar, partTypeVar, partitionLayoutVar); err != nil {
296298
log.Fatalf("%v", err)
297299
}
298300
} else {
@@ -303,13 +305,13 @@ func main() {
303305
}
304306

305307
if forceVar == true {
306-
if err := format(candidateDevice, labelVar, fsTypeVar, partTypeVar, forceVar); err != nil {
308+
if err := format(candidateDevice, labelVar, fsTypeVar, partTypeVar, partitionLayoutVar, forceVar); err != nil {
307309
log.Fatalf("%v", err)
308310
}
309311
} else {
310312
// add the deviceVar to the array of devices to consider autoformatting
311313
driveKeys = []string{candidateDevice}
312-
if err := autoformat(labelVar, fsTypeVar, partTypeVar); err != nil {
314+
if err := autoformat(labelVar, fsTypeVar, partTypeVar, partitionLayoutVar); err != nil {
313315
log.Fatalf("%v", err)
314316
}
315317
}

0 commit comments

Comments
 (0)