diff --git a/QUICKSTART.md b/QUICKSTART.md index b82a1f1..636e946 100644 --- a/QUICKSTART.md +++ b/QUICKSTART.md @@ -46,6 +46,9 @@ Enter choice (1 or 2): qemu-vm> create my-first-vm # Follow the interactive prompts +# Import a VM from configuration file +qemu-vm> import examples/sample-vm-config.json my-imported-vm + # Start the VM qemu-vm> start my-first-vm @@ -198,6 +201,7 @@ qemu-vm> upnp mappings |---------|-------------|---------| | `help` | Show all commands | `help` | | `create` | Create new VM | `create my-vm` | +| `import` | Import VM from config | `import config.json my-vm` | | `start` | Start VM | `start my-vm` | | `stop` | Stop VM | `stop my-vm` | | `status` | Show VM status | `status my-vm` | diff --git a/QemuVmManager.Console/Program.cs b/QemuVmManager.Console/Program.cs index 46c09b2..5205d2d 100644 --- a/QemuVmManager.Console/Program.cs +++ b/QemuVmManager.Console/Program.cs @@ -102,6 +102,9 @@ class Program case "delete": await DeleteVmAsync(vmService, arguments); break; + case "import": + await ImportVmAsync(vmService, arguments); + break; case "status": await ShowVmStatusAsync(vmService, arguments); break; @@ -144,6 +147,7 @@ class Program System.Console.WriteLine("Available commands:"); System.Console.WriteLine(" list - List all VMs"); System.Console.WriteLine(" create - Create a new VM (interactive)"); + System.Console.WriteLine(" import [name] - Import VM from configuration file"); System.Console.WriteLine(" start - Start a VM"); System.Console.WriteLine(" stop [--force] - Stop a VM"); System.Console.WriteLine(" pause - Pause a VM"); @@ -760,6 +764,49 @@ class Program } } + static async Task ImportVmAsync(VmManagementService vmService, string[] arguments) + { + if (arguments.Length == 0) + { + System.Console.WriteLine("Usage: import [new-vm-name]"); + System.Console.WriteLine("Example: import examples/sample-vm-config.json my-imported-vm"); + return; + } + + var configPath = arguments[0]; + var newName = arguments.Length > 1 ? arguments[1] : null; + + try + { + System.Console.WriteLine($"Importing VM configuration from: {configPath}"); + + var importedConfig = await vmService.ImportVmConfigurationAsync(configPath, newName); + + System.Console.WriteLine($"Successfully imported VM '{importedConfig.Name}'"); + System.Console.WriteLine($"Description: {importedConfig.Description}"); + System.Console.WriteLine($"CPU: {importedConfig.Cpu.Cores} cores"); + System.Console.WriteLine($"Memory: {importedConfig.Memory.Size}{importedConfig.Memory.Unit}"); + + if (importedConfig.Storage.Disks.Any()) + { + System.Console.WriteLine("Disks:"); + foreach (var disk in importedConfig.Storage.Disks) + { + System.Console.WriteLine($" - {disk.Path} ({disk.Size}GB, {disk.Format})"); + } + } + } + catch (FileNotFoundException) + { + System.Console.WriteLine($"Error: Configuration file '{configPath}' not found"); + System.Console.WriteLine("Make sure the file path is correct and the file exists."); + } + catch (Exception ex) + { + System.Console.WriteLine($"Error importing VM: {ex.Message}"); + } + } + static string GetUserInput(string prompt, string defaultValue = "") { System.Console.Write(prompt); diff --git a/examples/sample-vm-config.json b/examples/sample-vm-config.json index 94ea07c..06f949d 100644 --- a/examples/sample-vm-config.json +++ b/examples/sample-vm-config.json @@ -3,79 +3,38 @@ "description": "Ubuntu Desktop VM for development", "cpu": { "cores": 4, - "sockets": 1, - "threads": 1, - "model": "qemu64", - "enableKvm": true + "model": "host", + "sockets": 1 }, "memory": { - "size": 8192, - "unit": "M" + "sizeGB": 8 }, "storage": { "disks": [ { - "path": "C:\\Users\\mahes\\Disks\\ubuntu-desktop.qcow2", - "size": 50, + "path": "ubuntu-desktop.qcow2", "format": "qcow2", - "interface": "virtio", - "cache": "writeback", + "sizeGB": 50, "isBoot": true }, { - "path": "C:\\Users\\mahes\\Disks\\ubuntu-data.qcow2", - "size": 100, + "path": "ubuntu-data.qcow2", "format": "qcow2", - "interface": "virtio", - "cache": "writeback", + "sizeGB": 100, "isBoot": false } ], - "cdrom": "C:\\Users\\mahes\\Downloads\\ubuntu-24.04.3-desktop-amd64.iso" + "cdrom": { + "path": "ubuntu-24.04.3-desktop-amd64.iso", + "isBoot": true + } }, "network": { - "interfaces": [ - { - "type": "bridge", - "model": "virtio-net-pci", - "mac": "52:54:00:12:34:56", - "bridge": "virbr0" - } - ], - "bridge": "virbr0" + "backend": "user", + "device": "e1000" }, "display": { "type": "gtk", - "vga": "virtio", - "resolution": "1920x1080", - "enableSpice": true, - "spicePort": 5930 - }, - "boot": { - "order": ["c", "d", "n"], - "kernel": null, - "initrd": null, - "cmdline": null - }, - "advanced": { - "enableAudio": true, - "enableUsb": true, - "enableBalloon": true, - "enableVirtioRng": true, - "enableVirtioFs": false, - "sharedFolders": [ - { - "hostPath": "C:\\Users\\mahes\\Shared", - "guestPath": "/mnt/shared", - "readOnly": false - } - ], - "extraArgs": [ - "-enable-kvm", - "-cpu", - "host" - ] - }, - "created": "2024-01-01T10:00:00Z", - "lastModified": "2024-01-01T10:00:00Z" + "vga": "virtio" + } }