Skip to content

Commit

Permalink
Add ability to create extra disks on hyperkit vms
Browse files Browse the repository at this point in the history
Add the ability to create and attach extra disks to hyperkit vms.

Signed-off-by: Blaine Gardner <blaine.gardner@redhat.com>
  • Loading branch information
BlaineEXE committed May 25, 2021
1 parent 316f5ea commit 410b3da
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
43 changes: 38 additions & 5 deletions cmd/minikube/cmd/start_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const (
defaultSSHUser = "root"
defaultSSHPort = 22
listenAddress = "listen-address"
extraDisks = "extra-disks"
extraDiskSize = "extra-disk-size"
)

var (
Expand Down Expand Up @@ -164,6 +166,8 @@ func initMinikubeFlags() {
startCmd.Flags().StringP(network, "", "", "network to run minikube with. Now it is used by docker/podman and KVM drivers. If left empty, minikube will create a new network.")
startCmd.Flags().StringVarP(&outputFormat, "output", "o", "text", "Format to print stdout in. Options include: [text,json]")
startCmd.Flags().StringP(trace, "", "", "Send trace events. Options include: [gcp]")
startCmd.Flags().Int(extraDisks, 0, "Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit driver)")
startCmd.Flags().String(extraDiskSize, defaultDiskSize, "Disk size allocated for extra disks attached to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g) (currently only implemented for hyperkit driver).")
}

// initKubernetesFlags inits the commandline flags for Kubernetes related options
Expand Down Expand Up @@ -314,10 +318,10 @@ func getMemorySize(cmd *cobra.Command, drvName string) int {
return mem
}

func getDiskSize() int {
diskSize, err := pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize))
func getDiskSize(varName string) int {
diskSize, err := pkgutil.CalculateSizeInMB(viper.GetString(varName))
if err != nil {
exit.Message(reason.Usage, "Generate unable to parse disk size '{{.diskSize}}': {{.error}}", out.V{"diskSize": viper.GetString(humanReadableDiskSize), "error": err})
exit.Message(reason.Usage, "Generate unable to parse disk size '{{.diskSize}}': {{.error}}", out.V{"diskSize": viper.GetString(varName), "error": err})
}

return diskSize
Expand Down Expand Up @@ -376,6 +380,8 @@ func generateNewConfigFromFlags(cmd *cobra.Command, k8sVersion string, drvName s

checkNumaCount(k8sVersion)

checkExtraDiskOptions(cmd, drvName)

cc = config.ClusterConfig{
Name: ClusterFlagValue(),
KeepContext: viper.GetBool(keepContext),
Expand All @@ -385,7 +391,7 @@ func generateNewConfigFromFlags(cmd *cobra.Command, k8sVersion string, drvName s
Network: viper.GetString(network),
Memory: getMemorySize(cmd, drvName),
CPUs: viper.GetInt(cpus),
DiskSize: getDiskSize(),
DiskSize: getDiskSize(humanReadableDiskSize),
Driver: drvName,
ListenAddress: viper.GetString(listenAddress),
HyperkitVpnKitSock: viper.GetString(vpnkitSock),
Expand Down Expand Up @@ -418,6 +424,8 @@ func generateNewConfigFromFlags(cmd *cobra.Command, k8sVersion string, drvName s
SSHUser: viper.GetString(sshSSHUser),
SSHKey: viper.GetString(sshSSHKey),
SSHPort: viper.GetInt(sshSSHPort),
ExtraDisks: viper.GetInt(extraDisks),
ExtraDiskSize: getDiskSize(extraDiskSize),
KubernetesConfig: config.KubernetesConfig{
KubernetesVersion: k8sVersion,
ClusterName: ClusterFlagValue(),
Expand Down Expand Up @@ -525,10 +533,18 @@ func updateExistingConfigFromFlags(cmd *cobra.Command, existing *config.ClusterC
// validate the memory size in case user changed their system memory limits (example change docker desktop or upgraded memory.)
validateRequestedMemorySize(cc.Memory, cc.Driver)

if cmd.Flags().Changed(humanReadableDiskSize) && getDiskSize() != existing.DiskSize {
if cmd.Flags().Changed(humanReadableDiskSize) && getDiskSize(humanReadableDiskSize) != existing.DiskSize {
out.WarningT("You cannot change the disk size for an existing minikube cluster. Please first delete the cluster.")
}

checkExtraDiskOptions(cmd, cc.Driver)
if cmd.Flags().Changed(extraDiskSize) && getDiskSize(extraDiskSize) != existing.ExtraDiskSize {
out.WarningT("You cannot change the disk size of extra disks for an existing minikube cluster. Please first delete the cluster.")
}
if cmd.Flags().Changed(extraDisks) && viper.GetInt(extraDisks) != existing.ExtraDisks {
out.WarningT("You cannot add or remove extra disks for an existing minikube cluster. Please first delete the cluster.")
}

updateStringFromFlag(cmd, &cc.MinikubeISO, isoURL)
updateBoolFromFlag(cmd, &cc.KeepContext, keepContext)
updateBoolFromFlag(cmd, &cc.EmbedCerts, embedCerts)
Expand Down Expand Up @@ -675,3 +691,20 @@ func interpretWaitFlag(cmd cobra.Command) map[string]bool {
klog.Infof("Waiting for components: %+v", waitComponents)
return waitComponents
}

func checkExtraDiskOptions(cmd *cobra.Command, driverName string) {
supportedDrivers := []string{driver.HyperKit}

if cmd.Flags().Changed(extraDisks) || cmd.Flags().Changed(extraDiskSize) {
supported := false
for _, driver := range supportedDrivers {
if driverName == driver {
supported = true
break
}
}
if !supported {
out.WarningT("Specifying extra disks is currently only supported for the following drivers: {{.supported_drivers}}. If you can contribute to add this feature, please create a PR.", out.V{"supported_drivers": supportedDrivers})
}
}
}
16 changes: 11 additions & 5 deletions deploy/iso/minikube-iso/package/automount/minikube-automount
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ MAGIC="boot2docker, please format-me"
# If there is a partition with `boot2docker-data` as its label, use it and be
# very happy. Thus, you can come along if you feel like a room without a roof.
BOOT2DOCKER_DATA=`blkid -o device -l -t LABEL=$LABEL`
UNPARTITIONED_HD="/dev/$(lsblk | grep disk | cut -f1 -d' ')"
DISKS="$(lsblk | grep disk | cut -f1 -d' ')"
echo $BOOT2DOCKER_DATA
if [ ! -n "$BOOT2DOCKER_DATA" ]; then
for DISK in $DISKS; do
if [ -n "$BOOT2DOCKER_DATA" ]; then
# The primary minikube disk has been found or provisioned; stop provisioning other disks
break
fi

UNPARTITIONED_HD="/dev/$DISK"
echo "Is the disk unpartitioned?, test for the 'boot2docker format-me' string"

# Is the disk unpartitioned?, test for the 'boot2docker format-me' string
Expand Down Expand Up @@ -61,18 +67,18 @@ if [ ! -n "$BOOT2DOCKER_DATA" ]; then
BOOT2DOCKER_DATA=`echo "${UNPARTITIONED_HD}1"`
mkfs.ext4 -i 2048 -L $LABEL $BOOT2DOCKER_DATA
else
echo "Disk unpartitioned but something is there... not doing anything"
echo "Disk ($UNPARTITIONED_HD) unpartitioned but something is there... not doing anything"
fi
else
echo "Partition table found on disk, not doing anything"
echo "Partition table found on disk ($UNPARTITIONED_HD), not doing anything"
fi
fi
else
# Pick the first ext4 as a fallback
# TODO: mount all Linux partitions and look for a /var/lib/docker...
BOOT2DOCKER_DATA=`blkid | grep -e 'TYPE="btrfs"' -e 'TYPE="ext4"' | head -n 1 | sed 's/:.*//'`
fi
fi
done

echo $BOOT2DOCKER_DATA

Expand Down
7 changes: 7 additions & 0 deletions pkg/drivers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package drivers

import (
"fmt"
"io"
"io/ioutil"
"os"
Expand All @@ -41,6 +42,12 @@ func GetDiskPath(d *drivers.BaseDriver) string {
return filepath.Join(d.ResolveStorePath("."), d.GetMachineName()+".rawdisk")
}

// ExtraDiskPath returns the path of an additional disk suffixed with an ID.
func ExtraDiskPath(d *drivers.BaseDriver, diskID int) string {
file := fmt.Sprintf("%s-%d.rawdisk", d.GetMachineName(), diskID)
return filepath.Join(d.ResolveStorePath("."), file)
}

// CommonDriver is the common driver base class
type CommonDriver struct{}

Expand Down
9 changes: 9 additions & 0 deletions pkg/drivers/hyperkit/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type Driver struct {
UUID string
VpnKitSock string
VSockPorts []string
ExtraDisks int
ExtraDiskSize int
}

// NewDriver creates a new driver for a host
Expand Down Expand Up @@ -229,6 +231,13 @@ func (d *Driver) createHost() (*hyperkit.HyperKit, error) {
Trim: true,
},
}
for i := 0; i < d.ExtraDisks; i++ {
h.Disks = append(h.Disks, &hyperkit.RawDisk{
Path: pkgdrivers.ExtraDiskPath(d.BaseDriver, i),
Size: d.ExtraDiskSize,
Trim: true,
})
}

return h, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type ClusterConfig struct {
ListenAddress string // Only used by the docker and podman driver
Network string // only used by docker driver
MultiNodeRequested bool
ExtraDisks int // currently only implemented for hyperkit
ExtraDiskSize int // currently only implemented for hyperkit
}

// KubernetesConfig contains the parameters used to configure the VM Kubernetes.
Expand Down
2 changes: 2 additions & 0 deletions pkg/minikube/registry/drvs/hyperkit/hyperkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func configure(cfg config.ClusterConfig, n config.Node) (interface{}, error) {
VpnKitSock: cfg.HyperkitVpnKitSock,
VSockPorts: cfg.HyperkitVSockPorts,
Cmdline: "loglevel=3 console=ttyS0 console=tty0 noembed nomodeset norestore waitusb=10 systemd.legacy_systemd_cgroup_controller=yes random.trust_cpu=on hw_rng_model=virtio base host=" + cfg.Name,
ExtraDisks: cfg.ExtraDisks,
ExtraDiskSize: cfg.ExtraDiskSize,
}, nil
}

Expand Down

0 comments on commit 410b3da

Please sign in to comment.