generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically detect the disk to resize (#34)
- Loading branch information
Showing
6 changed files
with
208 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package tart | ||
|
||
import ( | ||
"fmt" | ||
"howett.net/plist" | ||
) | ||
|
||
const expectedLastPartitionContent = "Apple_APFS" | ||
|
||
// ParseDiskUtilPlistOutput parses "diskutil list -plist" output, | ||
// makes sure there's only one disk on the system and returns | ||
// its name and the name of the last partition, additionally | ||
// validating that the last partition is not a recovery one | ||
// (which we should've deleted for the disk expansion to work). | ||
func ParseDiskUtilPlistOutput(input []byte) (string, string, error) { | ||
unmarshalledInput := map[string]interface{}{} | ||
|
||
_, err := plist.Unmarshal(input, &unmarshalledInput) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
allDisksAndPartitions, ok := unmarshalledInput["AllDisksAndPartitions"].([]interface{}) | ||
if !ok { | ||
return "", "", fmt.Errorf("\"AllDisksAndPartitions\" value doesn't seem to be a dictionary") | ||
} | ||
|
||
if len(allDisksAndPartitions) != 1 { | ||
return "", "", fmt.Errorf("there are more than one physical disk present on the system") | ||
} | ||
|
||
disk, ok := allDisksAndPartitions[0].(map[string]interface{}) | ||
if !ok { | ||
return "", "", fmt.Errorf("first disk entry doesn't seem to be a dictionary") | ||
} | ||
|
||
diskDeviceIdentifier, ok := disk["DeviceIdentifier"].(string) | ||
if !ok { | ||
return "", "", fmt.Errorf("first disk's \"DeviceIdentifier\" doesn't seem to be a string") | ||
} | ||
|
||
partitions, ok := disk["Partitions"].([]interface{}) | ||
if !ok { | ||
return "", "", fmt.Errorf("first disk's \"Partitions\" doesn't seem to be a list") | ||
} | ||
|
||
lastPartitionRaw := partitions[len(partitions)-1] | ||
lastPartition, ok := lastPartitionRaw.(map[string]interface{}) | ||
if !ok { | ||
return "", "", fmt.Errorf("last partition entry doesn't seem to be a map") | ||
} | ||
|
||
lastPartitionContent, ok := lastPartition["Content"].(string) | ||
if !ok { | ||
return "", "", fmt.Errorf("last partition's \"Content\" doesn't seem to be a string") | ||
} | ||
if lastPartitionContent != expectedLastPartitionContent { | ||
return "", "", fmt.Errorf("last partition's \"Content\" should be %q, got %q", | ||
expectedLastPartitionContent, lastPartitionContent) | ||
} | ||
|
||
lastPartitionDeviceIdentifier, ok := lastPartition["DeviceIdentifier"].(string) | ||
if !ok { | ||
return "", "", fmt.Errorf("last partition's \"DeviceIdentifier\" doesn't seem to be a string") | ||
} | ||
|
||
return diskDeviceIdentifier, lastPartitionDeviceIdentifier, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package tart | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestKek(t *testing.T) { | ||
plistBytes := `<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>AllDisks</key> | ||
<array> | ||
<string>disk0</string> | ||
<string>disk0s1</string> | ||
<string>disk0s2</string> | ||
<string>disk0s3</string> | ||
</array> | ||
<key>AllDisksAndPartitions</key> | ||
<array> | ||
<dict> | ||
<key>Content</key> | ||
<string>GUID_partition_scheme</string> | ||
<key>DeviceIdentifier</key> | ||
<string>disk0</string> | ||
<key>OSInternal</key> | ||
<false/> | ||
<key>Partitions</key> | ||
<array> | ||
<dict> | ||
<key>Content</key> | ||
<string>Apple_APFS_ISC</string> | ||
<key>DeviceIdentifier</key> | ||
<string>disk0s1</string> | ||
<key>DiskUUID</key> | ||
<string>024D2AE5-891F-4244-81EC-182B88D1AA0B</string> | ||
<key>Size</key> | ||
<integer>524288000</integer> | ||
</dict> | ||
<dict> | ||
<key>Content</key> | ||
<string>Apple_APFS</string> | ||
<key>DeviceIdentifier</key> | ||
<string>disk0s2</string> | ||
<key>DiskUUID</key> | ||
<string>430F1409-D91A-47F4-8418-0876B14AA807</string> | ||
<key>Size</key> | ||
<integer>494384795648</integer> | ||
</dict> | ||
</array> | ||
<key>Size</key> | ||
<integer>500277792768</integer> | ||
</dict> | ||
</array> | ||
<key>VolumesFromDisks</key> | ||
<array/> | ||
<key>WholeDisks</key> | ||
<array> | ||
<string>disk0</string> | ||
</array> | ||
</dict> | ||
</plist> | ||
` | ||
|
||
diskName, partitionName, err := ParseDiskUtilPlistOutput([]byte(plistBytes)) | ||
require.NoError(t, err) | ||
require.Equal(t, "disk0", diskName) | ||
require.Equal(t, "disk0s2", partitionName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tart | ||
|
||
import ( | ||
"github.com/hashicorp/packer-plugin-sdk/packer" | ||
"io" | ||
) | ||
|
||
type QuietUi struct { | ||
BaseUi packer.Ui | ||
} | ||
|
||
func (ui QuietUi) Ask(s string) (string, error) { | ||
return ui.BaseUi.Ask(s) | ||
} | ||
|
||
func (ui QuietUi) Say(s string) { | ||
// do nothing | ||
} | ||
|
||
func (ui QuietUi) Message(s string) { | ||
// do nothing | ||
} | ||
|
||
func (ui QuietUi) Error(s string) { | ||
ui.BaseUi.Error(s) | ||
} | ||
|
||
func (ui QuietUi) Machine(s string, s2 ...string) { | ||
ui.BaseUi.Machine(s, s2...) | ||
} | ||
|
||
func (ui QuietUi) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) (body io.ReadCloser) { | ||
return ui.BaseUi.TrackProgress(src, currentSize, totalSize, stream) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.