On Debian 13 `virsh` how to add *Virtual Disk* *Source path*?
Aug 12, 10:31pm

To add a virtual disk with a source path using virsh on Debian 13 (or any system using libvirt), you typically need to:

  1. Edit the VM's XML configuration to define the new disk.
  2. Attach the disk to the VM using the updated XML.

Here’s how you can do it:


Step 1: Shut Down the VM (if running)

If the VM is running, shut it down first:

bash
virsh shutdown <vm-name>
virsh destroy <vm-name>  # Force stop if needed

Step 2: Edit the VM's XML Configuration

Use virsh edit to modify the VM's configuration:

bash
virsh edit <vm-name>

In the XML editor, locate the <devices> section and add a new <disk> entry. For example, to add a raw disk image (e.g., /path/to/disk.img), use:

xml
<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/path/to/disk.img'/>
  <target dev='vdb' bus='virtio'/>
</disk>

Key Attributes:

  • type='file': Specifies the disk is a file (use type='block' for physical devices like /dev/sdX).
  • device='disk': Treats the disk as a storage device (other options: cdrom, floppy).
  • <source file='...'>: Path to the disk image or block device.
  • <target dev='vdb' bus='virtio'>:
    • dev='vdb': The device name (e.g., vda, vdb, sda, etc.).
    • bus='virtio': The bus type (recommended for performance; alternatives: ide, scsi, sata).

Step 3: Save and Exit

Save the changes in the editor. The VM's configuration will update automatically.


Step 4: Start the VM

bash
virsh start <vm-name>

Alternative: Attach a Disk Temporarily (Hotplug)

If the VM is running and supports hotplugging, you can attach a disk without editing the XML:

bash
virsh attach-disk <vm-name> /path/to/disk.img vdb --type disk --mode shareable --persistent
  • --persistent: Ensures the disk remains attached after a reboot.
  • --mode shareable: Allows the disk to be shared (optional).

Verify the Disk

Check if the disk is attached:

bash
virsh domblklist <vm-name>

Notes:

  • Replace <vm-name> with your VM's name.
  • For QCOW2 or other formats, change type='raw' to type='qcow2'.
  • For physical disks (e.g., /dev/sdX), use:
    xml
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw'/>
      <source dev='/dev/sdX'/>
      <target dev='vdb' bus='virtio'/>
    </disk>
Aug 12, 10:31pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more