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 25, 2023
1 parent 7d2c71b commit f2e4be4
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 37 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
23 changes: 23 additions & 0 deletions pkg/drivers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ func ExtraDiskPath(d *drivers.BaseDriver, diskID int) string {
return filepath.Join(d.ResolveStorePath("."), file)
}

// CreateRawDisk creates a new raw disk image.
//
// Example usage:
//
// path := ExtraDiskPath(baseDriver, diskID)
// err := CreateRawDisk(path, baseDriver.DiskSize)
func CreateRawDisk(diskPath string, sizeMB int) error {
log.Infof("Creating raw disk image: %s of size %vMB", diskPath, sizeMB)

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(sizeMB)); err != nil {
return errors.Wrap(err, "truncate")
}
}
return nil
}

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

Expand Down
26 changes: 0 additions & 26 deletions pkg/drivers/kvm/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ package kvm
import (
"bytes"
"fmt"
"os"
"text/template"

"github.com/docker/machine/libmachine/log"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/drivers"
"k8s.io/minikube/pkg/util"
)

// extraDisksTmpl ExtraDisks XML Template
Expand Down Expand Up @@ -58,23 +52,3 @@ func getExtraDiskXML(diskpath string, logicalName string) (string, error) {
}
return extraDisksXML.String(), nil
}

// createExtraDisks creates the extra disk files
func createExtraDisk(d *Driver, index int) (string, error) {
diskPath := drivers.ExtraDiskPath(d.BaseDriver, index)
log.Infof("Creating raw disk image: %s of size %v", diskPath, 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 diskPath, nil

}
4 changes: 2 additions & 2 deletions pkg/drivers/kvm/kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ func (d *Driver) Create() (err error) {
return errors.Wrap(err, "cannot create more than 20 extra disks")
}
for i := 0; i < d.ExtraDisks; i++ {
diskpath, err := createExtraDisk(d, i)
if err != nil {
diskpath := pkgdrivers.ExtraDiskPath(d.BaseDriver, i)
if err := pkgdrivers.CreateRawDisk(diskpath, d.DiskSize); err != nil {
return errors.Wrap(err, "creating extra disks")
}
// Starting the logical names for the extra disks from hdd as the cdrom device is set to hdc.
Expand Down
20 changes: 20 additions & 0 deletions pkg/drivers/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Driver struct {
MACAddress string
SocketVMNetPath string
SocketVMNetClientPath string
ExtraDisks int
}

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

if d.ExtraDisks > 0 {
log.Info("Creating extra disk images...")
for i := 0; i < d.ExtraDisks; i++ {
path := pkgdrivers.ExtraDiskPath(d.BaseDriver, i)
if err := pkgdrivers.CreateRawDisk(path, d.DiskSize); 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", pkgdrivers.ExtraDiskPath(d.BaseDriver, i), index),
)
}

if d.VirtioDrives {
startCmd = append(startCmd,
"-drive", fmt.Sprintf("file=%s,index=0,media=disk,if=virtio", d.diskPath()))
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
2 changes: 1 addition & 1 deletion 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
1 change: 1 addition & 0 deletions translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "Aktivives podman-env am Treiber {{.driver_name}} in diesem Terminal erkannt:",
"Number of CPUs allocated to the minikube VM": "Anzahl der CPUs, die der minikube-VM zugeordnet sind",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "Anzahl der Extra-Disks, die erstellt und an die Minikube VM geängt werden (derzeit nur im hyperkit und kvm2 Treiber implementiert)",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "Anzahl der Zeilen, die im Log zurückgegangen werden soll",
"OS release is {{.pretty_name}}": "Die Betriebssystem-Version ist {{.pretty_name}}",
"One of 'text', 'yaml' or 'json'.": "Entweder 'text', 'yaml' oder 'json'.",
Expand Down
2 changes: 1 addition & 1 deletion translations/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "",
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "",
"Number of CPUs allocated to the minikube VM": "Número de CPU asignadas a la VM de minikube",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "",
"OS release is {{.pretty_name}}": "",
"One of 'text', 'yaml' or 'json'.": "",
Expand Down
1 change: 1 addition & 0 deletions translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "Vous avez remarqué que vous avez un docker-env activé sur le pilote {{.driver_name}} dans ce terminal :",
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "Vous avez remarqué que vous avez un pilote podman-env activé sur {{.driver_name}} dans ce terminal :",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "Nombre de disques supplémentaires créés et attachés à la machine virtuelle minikube (actuellement implémenté uniquement pour les pilotes hyperkit et kvm2)",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "Nombre de lignes à remonter dans le journal",
"OS release is {{.pretty_name}}": "La version du système d'exploitation est {{.pretty_name}}",
"One of 'text', 'yaml' or 'json'.": "Un parmi 'text', 'yaml' ou 'json'.",
Expand Down
1 change: 1 addition & 0 deletions translations/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "通知: このターミナルでは、{{.driver_name}} ドライバーの docker-env が有効になっています:",
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "通知: このターミナルでは、{{.driver_name}} ドライバーの podman-env が有効になっています:",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "作成して minikube VM に接続する追加ディスク数 (現在、hyperkit と kvm2 ドライバーでのみ実装されています)",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "ログ中で遡る行数",
"OS release is {{.pretty_name}}": "OS リリースは {{.pretty_name}} です",
"One of 'text', 'yaml' or 'json'.": "'text'、'yaml'、'json' のいずれか。",
Expand Down
2 changes: 1 addition & 1 deletion translations/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@
"None of the known repositories in your location are accessible. Using {{.image_repository_name}} as fallback.": "",
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "",
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "",
"OS release is {{.pretty_name}}": "",
"One of 'text', 'yaml' or 'json'.": "",
Expand Down
2 changes: 1 addition & 1 deletion translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
"Number of CPUs allocated to Kubernetes.": "Liczba procesorów przypisana do Kubernetesa",
"Number of CPUs allocated to the minikube VM": "Liczba procesorów przypisana do maszyny wirtualnej minikube",
"Number of CPUs allocated to the minikube VM.": "Liczba procesorów przypisana do maszyny wirtualnej minikube",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "",
"OS release is {{.pretty_name}}": "Wersja systemu operacyjnego to {{.pretty_name}}",
"One of 'text', 'yaml' or 'json'.": "",
Expand Down
2 changes: 1 addition & 1 deletion translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@
"None of the known repositories in your location are accessible. Using {{.image_repository_name}} as fallback.": "",
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "",
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "",
"OS release is {{.pretty_name}}": "",
"One of 'text', 'yaml' or 'json'.": "",
Expand Down
2 changes: 1 addition & 1 deletion translations/strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@
"None of the known repositories in your location are accessible. Using {{.image_repository_name}} as fallback.": "",
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "",
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "",
"OS release is {{.pretty_name}}": "",
"One of 'text', 'yaml' or 'json'.": "",
Expand Down
2 changes: 1 addition & 1 deletion translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@
"Noticed you have an activated docker-env on {{.driver_name}} driver in this terminal:": "",
"Noticed you have an activated podman-env on {{.driver_name}} driver in this terminal:": "",
"Number of CPUs allocated to the minikube VM": "分配给 minikube 虚拟机的 CPU 的数量",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit and kvm2 drivers)": "",
"Number of extra disks created and attached to the minikube VM (currently only implemented for hyperkit, kvm2, and qemu2 drivers)": "",
"Number of lines back to go within the log": "",
"OS release is {{.pretty_name}}": "",
"One of 'text', 'yaml' or 'json'.": "",
Expand Down

0 comments on commit f2e4be4

Please sign in to comment.