Skip to content

Commit

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

Signed-off-by: Blaine Gardner <[email protected]>
  • Loading branch information
BlaineEXE committed Feb 18, 2023
1 parent 7d2c71b commit e10357e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/start_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ 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 and kvm2 drivers)")
startCmd.Flags().Int(extraDisks, 0, "Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)")
startCmd.Flags().Duration(certExpiration, constants.DefaultCertExpiration, "Duration until minikube certificate expiration, defaults to three years (26280h).")
startCmd.Flags().String(binaryMirror, "", "Location to fetch kubectl, kubelet, & kubeadm binaries from.")
startCmd.Flags().Bool(disableOptimizations, false, "If set, disables optimizations that are set for local Kubernetes. Including decreasing CoreDNS replicas from 2 to 1. Defaults to false.")
Expand Down Expand Up @@ -940,7 +940,7 @@ func interpretWaitFlag(cmd cobra.Command) map[string]bool {
}

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

if cmd.Flags().Changed(extraDisks) {
supported := false
Expand Down
46 changes: 46 additions & 0 deletions pkg/drivers/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

pkgdrivers "k8s.io/minikube/pkg/drivers"
"k8s.io/minikube/pkg/network"
"k8s.io/minikube/pkg/util"
)

const (
Expand Down Expand Up @@ -83,6 +84,7 @@ type Driver struct {
MACAddress string
SocketVMNetPath string
SocketVMNetClientPath string
ExtraDisks int
}

func (d *Driver) GetMachineName() string {
Expand Down Expand Up @@ -267,6 +269,15 @@ func (d *Driver) Create() error {
}
}

if d.ExtraDisks > 0 {
log.Info("Creating extra disk images...")
for i := 0; i < d.ExtraDisks; i++ {
if err := d.generateExtraDisk(i); err != nil {
return err
}
}
}

log.Info("Starting QEMU VM...")
return d.Start()
}
Expand Down Expand Up @@ -442,6 +453,15 @@ func (d *Driver) Start() error {
"virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=config-2")
}

for i := 0; i < d.ExtraDisks; i++ {
// use a higher index for extra disks to reduce ID collision with current or future
// low-indexed devices (e.g., firmware, ISO CDROM, cloud config, and network device)
index := i + 10
startCmd = append(startCmd,
"-drive", fmt.Sprintf("file=%s,index=%d,media=disk,format=raw,if=virtio", d.extraDiskPath(i), index),
)
}

if d.VirtioDrives {
startCmd = append(startCmd,
"-drive", fmt.Sprintf("file=%s,index=0,media=disk,if=virtio", d.diskPath()))
Expand Down Expand Up @@ -623,6 +643,12 @@ func (d *Driver) pidfilePath() string {
return filepath.Join(machineDir, "qemu.pid")
}

func (d *Driver) extraDiskPath(i int) string {
machineDir := filepath.Join(d.StorePath, "machines", d.GetMachineName())
diskFile := fmt.Sprintf("extra-disk-%d.raw", i)
return filepath.Join(machineDir, diskFile)
}

// Make a boot2docker VM disk image.
func (d *Driver) generateDiskImage(size int) error {
log.Debugf("Creating %d MB hard disk image...", size)
Expand Down Expand Up @@ -712,6 +738,26 @@ func (d *Driver) generateUserdataDisk(userdataFile string) (string, error) {
return ccRoot, nil
}

// generateExtraDisk creates the extra disk files
func (d *Driver) generateExtraDisk(i int) error {
diskPath := d.extraDiskPath(i)
log.Infof("Creating an extra %d MB raw hard disk image...", d.DiskSize)

if _, err := os.Stat(diskPath); os.IsNotExist(err) {
file, err := os.OpenFile(diskPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return errors.Wrap(err, "open")
}
defer file.Close()

if err := file.Truncate(util.ConvertMBToBytes(d.DiskSize)); err != nil {
return errors.Wrap(err, "truncate")
}
}
return nil

}

func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
// connect to monitor
conn, err := net.Dial("unix", d.monitorPath())
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/registry/drvs/qemu2/qemu2.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
MACAddress: mac,
SocketVMNetPath: cc.SocketVMnetPath,
SocketVMNetClientPath: cc.SocketVMnetClientPath,
ExtraDisks: cc.ExtraDisks,
}, nil
}

Expand Down
5 changes: 2 additions & 3 deletions site/content/en/docs/commands/start.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ minikube start [flags]
The key should be '.' separated, and the first part before the dot is the component to apply the configuration to.
Valid components are: kubelet, kubeadm, apiserver, controller-manager, etcd, proxy, scheduler
Valid kubeadm parameters: ignore-preflight-errors, dry-run, kubeconfig, kubeconfig-dir, node-name, cri-socket, experimental-upload-certs, certificate-key, rootfs, skip-phases, pod-network-cidr
--extra-disks int Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)
--extra-disks int Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)
--feature-gates string A set of key=value pairs that describe feature gates for alpha/experimental features.
--force Force minikube to perform possibly dangerous operations
--force-systemd If set, force the container runtime to use systemd as cgroup manager. Defaults to false.
Expand Down Expand Up @@ -127,7 +127,7 @@ minikube start [flags]
--add_dir_header If true, adds the file directory to the header of the log messages
--alsologtostderr log to standard error as well as files (no effect when -logtostderr=true)
-b, --bootstrapper string The name of the cluster bootstrapper that will set up the Kubernetes cluster. (default "kubeadm")
-h, --help
-h, --help
--log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0)
--log_dir string If non-empty, write log files in this directory (no effect when -logtostderr=true)
--log_file string If non-empty, use this log file (no effect when -logtostderr=true)
Expand All @@ -144,4 +144,3 @@ minikube start [flags]
-v, --v Level number for the log level verbosity
--vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging
```

0 comments on commit e10357e

Please sign in to comment.