Skip to content

Commit

Permalink
refactor: move primary disk creation into its own function
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Sollier <[email protected]>
  • Loading branch information
Kuruyia committed Apr 14, 2024
1 parent a9b8f88 commit e13dc5e
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions cluster-provision/centos9/vmcli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ func addIptablesRules(iptables *iptables.IPTables, rootless bool, protocol strin
return nil
}

func createPrimaryDisk(forcedNextDiskPath string) (string, error) {
// For backward compatibility, so that we can just copy over the newer files
_, err := os.Stat(provisionedDiskPath)

if err == nil {
os.Remove(firstDiskPath)
os.Symlink(provisionedDiskPath, firstDiskPath)
}

diskFile, diskBackingFile, err := utils.CalcNextDisk(".", forcedNextDiskPath)
if err != nil {
return "", fmt.Errorf("Failed to calculate the next disk image path: %v", err)
}

diskSize, err := qemu.GetDiskSize(diskBackingFile)
if err != nil {
return "", fmt.Errorf("Failed to get the disk image size of \"%s\": %v", diskBackingFile, err)
}

if diskSize < defaultDiskSize {
diskSize = defaultDiskSize
}

fmt.Printf("Creating disk \"%s backed by %s with size %d\"\n", diskFile, diskBackingFile, diskSize)
return diskFile, qemu.CreateDiskWithBackingFile(diskFile, "qcow2", diskSize, diskBackingFile, "qcow2")
}

// Creates raw disks to be used by secondary block devices
func createSecondaryRawDisks(diskSizes []uint, deviceKind string) error {
for i, size := range diskSizes {
Expand Down Expand Up @@ -236,30 +263,7 @@ func run(cmd *cobra.Command, args []string) error {
}
}

// For backward compatibility, so that we can just copy over the newer files
_, err = os.Stat(provisionedDiskPath)

if err == nil {
os.Remove(firstDiskPath)
os.Symlink(provisionedDiskPath, firstDiskPath)
}

diskFile, diskBackingFile, err := utils.CalcNextDisk(".", nextDiskFlag)
if err != nil {
return fmt.Errorf("Failed to calculate the next disk image path: %v", err)
}

diskSize, err := qemu.GetDiskSize(diskBackingFile)
if err != nil {
return fmt.Errorf("Failed to get the disk image size of \"%s\": %v", diskBackingFile, err)
}

if diskSize < defaultDiskSize {
diskSize = defaultDiskSize
}

fmt.Printf("Creating disk \"%s backed by %s with size %d\"\n", diskFile, diskBackingFile, diskSize)
err = qemu.CreateDiskWithBackingFile(diskFile, "qcow2", diskSize, diskBackingFile, "qcow2")
diskFile, err := createPrimaryDisk(nextDiskFlag)
if err != nil {
return err
}
Expand Down

0 comments on commit e13dc5e

Please sign in to comment.