2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2026-06-22 04:16:05 +00:00

feat: add mac address support to qemu run command

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
2026-06-18 15:37:31 +02:00
parent 58739c9f1b
commit 8d40bc255e
4 changed files with 21 additions and 3 deletions
+3
View File
@@ -32,6 +32,7 @@ var (
qemuCmd string
qemuDetached bool
networking string
mac string
publishFlags MultipleFlag
QemuCmd = &cobra.Command{
@@ -80,6 +81,7 @@ func init() {
// Networking
flags.StringVar(&networking, "networking", qemuNetworkingDefault, "Networking mode. Valid options are 'default', 'user', 'bridge[,name]', tap[,name] and 'none'. 'user' uses QEMUs userspace networking. 'bridge' connects to a preexisting bridge. 'tap' uses a prexisting tap device. 'none' disables networking.`")
flags.StringVar(&mac, "mac", "", "MAC address for the virtual machine")
flags.Var(&publishFlags, "publish", "Publish a vm's port(s) to the host (default [])")
}
@@ -105,6 +107,7 @@ func Qemu(cmd *cobra.Command, args []string) {
qemu.WithCPUs(cpus),
qemu.WithMemory(mem),
qemu.WithNetworking(networking),
qemu.WithMAC(mac),
qemu.WithStdin(os.Stdin),
qemu.WithStdout(os.Stdout),
qemu.WithStderr(os.Stderr),
+1
View File
@@ -17,6 +17,7 @@ d2vm run qemu [options] [image-path] [flags]
--disk disk Disk config, may be repeated. [file=]path[,size=1G][,format=qcow2] (default [])
--gui Set qemu to use video output instead of stdio
-h, --help help for qemu
--mac string MAC address for the virtual machine
--mem uint Amount of memory in MB (default 1024)
--networking string Networking mode. Valid options are 'default', 'user', 'bridge[,name]', tap[,name] and 'none'. 'user' uses QEMUs userspace networking. 'bridge' connects to a preexisting bridge. 'tap' uses a prexisting tap device. 'none' disables networking.` (default "user")
--publish multiple-flag Publish a vm's port(s) to the host (default []) (default A multiple flag is a type of flag that can be repeated any number of times)
+7
View File
@@ -51,6 +51,7 @@ type config struct {
qemuImgPath string
publishedPorts []PublishedPort
netdevConfig string
mac string
stdin io.Reader
stdout io.Writer
@@ -129,6 +130,12 @@ func WithPublishedPorts(ports ...PublishedPort) Option {
}
}
func WithMAC(mac string) Option {
return func(c *config) {
c.mac = mac
}
}
func WithStdin(r io.Reader) Option {
return func(c *config) {
c.stdin = r
+10 -3
View File
@@ -135,6 +135,14 @@ func Run(ctx context.Context, path string, opts ...Option) error {
return fmt.Errorf("Invalid networking mode: %s", netMode[0])
}
if config.mac != "" {
if _, err := net.ParseMAC(config.mac); err != nil {
return fmt.Errorf("Invalid MAC address: %s", config.mac)
}
} else {
config.mac = generateMAC().String()
}
if err := config.discoverBinaries(); err != nil {
log.Fatal(err)
}
@@ -286,11 +294,10 @@ func (c *config) buildQemuCmdline() ([]string, error) {
}
if c.netdevConfig != "" {
mac := generateMAC()
if c.arch == "s390x" {
qemuArgs = append(qemuArgs, "-device", "virtio-net-ccw,netdev=t0,mac="+mac.String())
qemuArgs = append(qemuArgs, "-device", "virtio-net-ccw,netdev=t0,mac="+c.mac)
} else {
qemuArgs = append(qemuArgs, "-device", "virtio-net-pci,netdev=t0,mac="+mac.String())
qemuArgs = append(qemuArgs, "-device", "virtio-net-pci,netdev=t0,mac="+c.mac)
}
forwardings, err := buildQemuForwardings(c.publishedPorts)
if err != nil {