(d, fsType string)
| 70 | } |
| 71 | |
| 72 | func extend(d, fsType string) error { |
| 73 | mountpoint := "/mnt/tmp" |
| 74 | |
| 75 | data, err := exec.Command("sfdisk", "-J", d).Output() |
| 76 | if err != nil { |
| 77 | return fmt.Errorf("Unable to get drive data for %s from sfdisk: %v", d, err) |
| 78 | } |
| 79 | |
| 80 | f := Fdisk{} |
| 81 | if err := json.Unmarshal(data, &f); err != nil { |
| 82 | return fmt.Errorf("Unable to unmarshal partition table from sfdisk: %v", err) |
| 83 | } |
| 84 | |
| 85 | if len(f.PartitionTable.Partitions) == 0 { |
| 86 | log.Printf("Disk %s has no partitions. Skipping", d) |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | if len(f.PartitionTable.Partitions) > 1 { |
| 91 | log.Printf("Disk %s has more than 1 partition. Skipping", d) |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | partition := f.PartitionTable.Partitions[0] |
| 96 | // fail on anything that isn't a Linux partition |
| 97 | // 83 -> MBR/DOS Linux Partition ID |
| 98 | // 0FC63DAF-8483-4772-8E79-3D69D8477DE4 -> GPT Linux Partition GUID |
| 99 | if partition.Type != "83" && partition.Type != "0FC63DAF-8483-4772-8E79-3D69D8477DE4" { |
| 100 | return fmt.Errorf("Partition 1 on disk %s is not a Linux Partition", d) |
| 101 | } |
| 102 | |
| 103 | if f.PartitionTable.Label == "gpt" && partition.Start+partition.Size == f.PartitionTable.LastLBA { |
| 104 | log.Printf("No free space on device to extend partition") |
| 105 | return nil |
| 106 | } |
| 107 | if f.PartitionTable.Label == "dos" { |
| 108 | totalSize, err := deviceSize(d) |
| 109 | if err != nil { |
| 110 | return fmt.Errorf("Unable to convert total size from string to int: %v", err) |
| 111 | } |
| 112 | if partition.Start+partition.Size == totalSize { |
| 113 | log.Printf("No free space on device to extend partition") |
| 114 | return nil |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | switch fsType { |
| 119 | case "ext4": |
| 120 | if err := e2fsck(partition.Node, false); err != nil { |
| 121 | return fmt.Errorf("Initial e2fsck failed: %v", err) |
| 122 | } |
| 123 | // resize2fs fails unless we set force=true here |
| 124 | if err := e2fsck(partition.Node, true); err != nil { |
| 125 | return fmt.Errorf("e2fsck before resize failed: %v", err) |
| 126 | } |
| 127 | |
| 128 | if err := createPartition(d, partition); err != nil { |
| 129 | return err |
no test coverage detected