mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-08-05 16:53:48 +00:00
feat(format): allow custom partition layout specification in linuxkit/format
Signed-off-by: Yoshi Jaeger <github@jaeger.berlin>
This commit is contained in:
parent
7ac34a6aec
commit
8e29b48757
@ -20,13 +20,14 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
labelVar string
|
labelVar string
|
||||||
fsTypeVar string
|
fsTypeVar string
|
||||||
partTypeVar string
|
partTypeVar string
|
||||||
forceVar bool
|
forceVar bool
|
||||||
verboseVar bool
|
verboseVar bool
|
||||||
drives map[string]bool
|
drives map[string]bool
|
||||||
driveKeys []string
|
partitionLayoutVar string
|
||||||
|
driveKeys []string
|
||||||
)
|
)
|
||||||
|
|
||||||
func hasPartitions(d string) bool {
|
func hasPartitions(d string) bool {
|
||||||
@ -73,7 +74,7 @@ func isEmptyDevice(d string) (bool, error) {
|
|||||||
return isEmpty, nil
|
return isEmpty, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func autoformat(label, fsType string, partType string) error {
|
func autoformat(label, fsType string, partType string, partitionLayoutVar string) error {
|
||||||
var first string
|
var first string
|
||||||
for _, d := range driveKeys {
|
for _, d := range driveKeys {
|
||||||
if verboseVar {
|
if verboseVar {
|
||||||
@ -94,7 +95,7 @@ func autoformat(label, fsType string, partType string) error {
|
|||||||
return fmt.Errorf("No eligible disks found")
|
return fmt.Errorf("No eligible disks found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return format(first, label, fsType, partType, false)
|
return format(first, label, fsType, partType, partitionLayoutVar, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func refreshDevicesAndWaitFor(awaitedDevice string) error {
|
func refreshDevicesAndWaitFor(awaitedDevice string) error {
|
||||||
@ -129,7 +130,7 @@ func refreshDevicesAndWaitFor(awaitedDevice string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func format(d, label, fsType string, partType string, forced bool) error {
|
func format(d, label, fsType string, partType string, partitionLayoutVar string, forced bool) error {
|
||||||
if forced {
|
if forced {
|
||||||
// clear partitions on device if forced format and they exist
|
// clear partitions on device if forced format and they exist
|
||||||
if hasPartitions(d) {
|
if hasPartitions(d) {
|
||||||
@ -137,7 +138,7 @@ func format(d, label, fsType string, partType string, forced bool) error {
|
|||||||
log.Printf("Clearing partitions on %s because forced format was requested", d)
|
log.Printf("Clearing partitions on %s because forced format was requested", d)
|
||||||
}
|
}
|
||||||
partCmd := exec.Command("sfdisk", "--quiet", "--delete", d)
|
partCmd := exec.Command("sfdisk", "--quiet", "--delete", d)
|
||||||
partCmd.Stdin = strings.NewReader(";")
|
partCmd.Stdin = strings.NewReader(partitionLayoutVar)
|
||||||
if out, err := partCmd.CombinedOutput(); err != nil {
|
if out, err := partCmd.CombinedOutput(); err != nil {
|
||||||
return fmt.Errorf("Error deleting partitions with sfdisk: %v\n%s", err, out)
|
return fmt.Errorf("Error deleting partitions with sfdisk: %v\n%s", err, out)
|
||||||
}
|
}
|
||||||
@ -169,7 +170,7 @@ func format(d, label, fsType string, partType string, forced bool) error {
|
|||||||
|
|
||||||
// format one large partition
|
// format one large partition
|
||||||
partCmd := exec.Command("sfdisk", "--quiet", d)
|
partCmd := exec.Command("sfdisk", "--quiet", d)
|
||||||
partCmd.Stdin = strings.NewReader(";")
|
partCmd.Stdin = strings.NewReader(partitionLayoutVar)
|
||||||
if out, err := partCmd.CombinedOutput(); err != nil {
|
if out, err := partCmd.CombinedOutput(); err != nil {
|
||||||
return fmt.Errorf("Error running sfdisk: %v\n%s", err, out)
|
return fmt.Errorf("Error running sfdisk: %v\n%s", err, out)
|
||||||
}
|
}
|
||||||
@ -267,6 +268,7 @@ func init() {
|
|||||||
flag.StringVar(&labelVar, "label", "", "Disk label to apply")
|
flag.StringVar(&labelVar, "label", "", "Disk label to apply")
|
||||||
flag.StringVar(&fsTypeVar, "type", "ext4", "Type of filesystem to create")
|
flag.StringVar(&fsTypeVar, "type", "ext4", "Type of filesystem to create")
|
||||||
flag.StringVar(&partTypeVar, "partition", "dos", "Type of partition table to create")
|
flag.StringVar(&partTypeVar, "partition", "dos", "Type of partition table to create")
|
||||||
|
flag.StringVar(&partitionLayoutVar, "layout", ";", "Partition layout for sfdisk invocation")
|
||||||
flag.BoolVar(&verboseVar, "verbose", false, "Enable verbose output (default false)")
|
flag.BoolVar(&verboseVar, "verbose", false, "Enable verbose output (default false)")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -292,7 +294,7 @@ func main() {
|
|||||||
if flag.NArg() == 0 {
|
if flag.NArg() == 0 {
|
||||||
// auto-detect drives if a device to format is not explicitly specified
|
// auto-detect drives if a device to format is not explicitly specified
|
||||||
findDrives()
|
findDrives()
|
||||||
if err := autoformat(labelVar, fsTypeVar, partTypeVar); err != nil {
|
if err := autoformat(labelVar, fsTypeVar, partTypeVar, partitionLayoutVar); err != nil {
|
||||||
log.Fatalf("%v", err)
|
log.Fatalf("%v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -303,13 +305,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if forceVar == true {
|
if forceVar == true {
|
||||||
if err := format(candidateDevice, labelVar, fsTypeVar, partTypeVar, forceVar); err != nil {
|
if err := format(candidateDevice, labelVar, fsTypeVar, partTypeVar, partitionLayoutVar, forceVar); err != nil {
|
||||||
log.Fatalf("%v", err)
|
log.Fatalf("%v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// add the deviceVar to the array of devices to consider autoformatting
|
// add the deviceVar to the array of devices to consider autoformatting
|
||||||
driveKeys = []string{candidateDevice}
|
driveKeys = []string{candidateDevice}
|
||||||
if err := autoformat(labelVar, fsTypeVar, partTypeVar); err != nil {
|
if err := autoformat(labelVar, fsTypeVar, partTypeVar, partitionLayoutVar); err != nil {
|
||||||
log.Fatalf("%v", err)
|
log.Fatalf("%v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user