Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cadvisor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ onboot:
- name: sysfs
image: linuxkit/sysfs:23a13bbf83bf666ef6a3ba7b3ebba35d2daead98
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/lib/docker"]
Expand Down
2 changes: 1 addition & 1 deletion examples/dm-crypt-loop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ onboot:
image: linuxkit/dhcpcd:f46134c05f9665d8865a9fbebd5be0995057af28
command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "/dev/sda"]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
2 changes: 1 addition & 1 deletion examples/dm-crypt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ onboot:
image: linuxkit/dhcpcd:f46134c05f9665d8865a9fbebd5be0995057af28
command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "/dev/sda"]
- name: dm-crypt
image: linuxkit/dm-crypt:ad2a05dcffa28ef809a61aa27ba230c82f02f603
Expand Down
2 changes: 1 addition & 1 deletion examples/docker-for-mac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ onboot:
image: linuxkit/binfmt:564e110729c8d8ecc365979a7d149e2851942883
# Format and mount the disk image in /var/lib/docker
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/lib"]
Expand Down
2 changes: 1 addition & 1 deletion examples/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ onboot:
- name: sysfs
image: linuxkit/sysfs:23a13bbf83bf666ef6a3ba7b3ebba35d2daead98
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/lib/docker"]
Expand Down
2 changes: 1 addition & 1 deletion examples/swap.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ onboot:
image: linuxkit/dhcpcd:f46134c05f9665d8865a9fbebd5be0995057af28
command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/external"]
Expand Down
32 changes: 17 additions & 15 deletions pkg/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ const (
)

var (
labelVar string
fsTypeVar string
partTypeVar string
forceVar bool
verboseVar bool
drives map[string]bool
driveKeys []string
labelVar string
fsTypeVar string
partTypeVar string
forceVar bool
verboseVar bool
drives map[string]bool
partitionLayoutVar string
driveKeys []string
)

func hasPartitions(d string) bool {
Expand Down Expand Up @@ -73,7 +74,7 @@ func isEmptyDevice(d string) (bool, error) {
return isEmpty, nil
}

func autoformat(label, fsType string, partType string) error {
func autoformat(label, fsType string, partType string, partitionLayoutVar string) error {
var first string
for _, d := range driveKeys {
if verboseVar {
Expand All @@ -94,7 +95,7 @@ func autoformat(label, fsType string, partType string) error {
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 {
Expand Down Expand Up @@ -129,15 +130,15 @@ func refreshDevicesAndWaitFor(awaitedDevice string) error {
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 {
// clear partitions on device if forced format and they exist
if hasPartitions(d) {
if verboseVar {
log.Printf("Clearing partitions on %s because forced format was requested", d)
}
partCmd := exec.Command("sfdisk", "--quiet", "--delete", d)
partCmd.Stdin = strings.NewReader(";")
partCmd.Stdin = strings.NewReader(partitionLayoutVar)
if out, err := partCmd.CombinedOutput(); err != nil {
return fmt.Errorf("Error deleting partitions with sfdisk: %v\n%s", err, out)
}
Expand Down Expand Up @@ -169,7 +170,7 @@ func format(d, label, fsType string, partType string, forced bool) error {

// format one large partition
partCmd := exec.Command("sfdisk", "--quiet", d)
partCmd.Stdin = strings.NewReader(";")
partCmd.Stdin = strings.NewReader(partitionLayoutVar)
if out, err := partCmd.CombinedOutput(); err != nil {
return fmt.Errorf("Error running sfdisk: %v\n%s", err, out)
}
Expand Down Expand Up @@ -267,6 +268,7 @@ func init() {
flag.StringVar(&labelVar, "label", "", "Disk label to apply")
flag.StringVar(&fsTypeVar, "type", "ext4", "Type of filesystem 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)")
}

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

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)
}
} else {
// add the deviceVar to the array of devices to consider autoformatting
driveKeys = []string{candidateDevice}
if err := autoformat(labelVar, fsTypeVar, partTypeVar); err != nil {
if err := autoformat(labelVar, fsTypeVar, partTypeVar, partitionLayoutVar); err != nil {
log.Fatalf("%v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion projects/compose/compose-dynamic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ onboot:
image: linuxkit/dhcpcd:f46134c05f9665d8865a9fbebd5be0995057af28
command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/lib/docker"]
Expand Down
2 changes: 1 addition & 1 deletion projects/compose/compose-static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ onboot:
image: linuxkit/dhcpcd:f46134c05f9665d8865a9fbebd5be0995057af28
command: ["/sbin/dhcpcd", "--nobackground", "-f", "/dhcpcd.conf", "-1"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/lib/docker"]
Expand Down
2 changes: 1 addition & 1 deletion test/cases/030_security/000_docker-bench/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ onboot:
- name: sysfs
image: linuxkit/sysfs:23a13bbf83bf666ef6a3ba7b3ebba35d2daead98
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/lib/docker"]
Expand Down
2 changes: 1 addition & 1 deletion test/cases/040_packages/003_containerd/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ onboot:
- name: sysctl
image: linuxkit/sysctl:112fe3d480ccb6cd115d9d6c446f9d833f6b8e68
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/lib"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
command: ["/usr/bin/mountie", "/var/lib/docker"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ onboot:
image: linuxkit/modprobe:fad39ef443853ef02520052cdbf6acbeb4ec7799
command: ["modprobe", "btrfs"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-type", "btrfs" ]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
2 changes: 1 addition & 1 deletion test/cases/040_packages/005_extend/002_xfs/test-create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-type", "xfs"]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
2 changes: 1 addition & 1 deletion test/cases/040_packages/005_extend/003_gpt/test-create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-partition", "gpt"]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
2 changes: 1 addition & 1 deletion test/cases/040_packages/006_format_mount/000_auto/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format"]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-label", "docker"]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "@DEVICE@"]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ onboot:
image: linuxkit/modprobe:fad39ef443853ef02520052cdbf6acbeb4ec7799
command: ["modprobe", "btrfs"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-type", "btrfs" ]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
2 changes: 1 addition & 1 deletion test/cases/040_packages/006_format_mount/004_xfs/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-type", "xfs" ]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-verbose", "-type", "ext4", "/dev/sda"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-verbose", "-type", "ext4", "/dev/sdb"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-verbose", "-type", "xfs", "/dev/sda"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-verbose", "-force", "-type", "xfs", "/dev/sdb"]
- name: test
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
binds:
- /check.sh:/check.sh
command: ["sh", "./check.sh"]
Expand Down
2 changes: 1 addition & 1 deletion test/cases/040_packages/006_format_mount/006_gpt/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-partition", "gpt"]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ init:
- linuxkit/runc:8b5af3365fc7d015db4e44113d93c7b1f8e2d2ab
onboot:
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-label", "docker"]
- name: format
image: linuxkit/format:8f487d728959192289e0783784fc2b185eadbc82
image: linuxkit/format:ced02d849206371b6983eff1eb28f9515406fef0
command: ["/usr/bin/format", "-label", "foo"]
- name: mount
image: linuxkit/mount:2a507ef30302693682f9f612289028df00c58ac5
Expand Down