|
| 1 | +# Device Hotplugging [Developer Preview] |
| 2 | + |
| 3 | +> [!WARNING] |
| 4 | +> |
| 5 | +> This feature is currently in |
| 6 | +> [Developer Preview](RELEASE_POLICY.md#developer-preview-features). It may have |
| 7 | +> limitations, and its API or behavior may change in future releases. |
| 8 | +
|
| 9 | +Device hotplugging allows attaching and detaching PCI virtio devices to a |
| 10 | +running microVM without requiring a reboot. Supported device types are: |
| 11 | + |
| 12 | +- `virtio-block` |
| 13 | +- `virtio-pmem` |
| 14 | +- `virtio-net` |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- **PCI transport enabled**: Firecracker must be started with the `--enable-pci` |
| 19 | + flag. Device hotplugging is not supported with MMIO transport. |
| 20 | +- **Guest kernel with PCI support**: The guest kernel must have PCI and the |
| 21 | + relevant virtio drivers enabled. See the |
| 22 | + [kernel policy documentation](kernel-policy.md) for details. |
| 23 | + |
| 24 | +## Limitations |
| 25 | + |
| 26 | +- **No automatic guest notification**: Firecracker does not currently deliver a |
| 27 | + hotplug notification to the guest. After hotplugging a device, the guest must |
| 28 | + manually rescan the PCI bus to discover it. Similarly, before unplugging, the |
| 29 | + guest must manually remove the device. |
| 30 | + |
| 31 | +## Hotplugging a device |
| 32 | + |
| 33 | +Hotplugging uses the same API endpoints used for pre-boot device configuration. |
| 34 | +The only difference is that the request is issued after the VM has started. |
| 35 | + |
| 36 | +```console |
| 37 | +socket_location=/run/firecracker.socket |
| 38 | + |
| 39 | +curl --unix-socket $socket_location -i \ |
| 40 | + -X PUT 'http://localhost/drives/block1' \ |
| 41 | + -H 'Accept: application/json' \ |
| 42 | + -H 'Content-Type: application/json' \ |
| 43 | + -d '{ |
| 44 | + "drive_id": "block1", |
| 45 | + "path_on_host": "/path/to/block.ext4", |
| 46 | + "is_root_device": false, |
| 47 | + "is_read_only": false |
| 48 | + }' |
| 49 | +``` |
| 50 | + |
| 51 | +### Discovering the device in the guest |
| 52 | + |
| 53 | +Since no hotplug notification is delivered to the guest, a PCI bus rescan is |
| 54 | +required to make the guest discover the new device: |
| 55 | + |
| 56 | +```bash |
| 57 | +echo 1 > /sys/bus/pci/rescan |
| 58 | +``` |
| 59 | + |
| 60 | +After the rescan, the device will appear in `lspci` and the corresponding device |
| 61 | +node (e.g. `/dev/vdb`, `/dev/pmem1`) will be created by the guest kernel. |
| 62 | + |
| 63 | +## Hot-unplugging a device |
| 64 | + |
| 65 | +Hot-unplugging is a two-step process: first the guest must release the device, |
| 66 | +then the host issues the unplug request. |
| 67 | + |
| 68 | +### Step 1: Remove the device from the guest |
| 69 | + |
| 70 | +Before issuing the unplug API call, the guest must gracefully release the |
| 71 | +device. For example, unmount any mounted filesystems and remove the PCI device: |
| 72 | + |
| 73 | +```bash |
| 74 | +# Unmount the filesystem (if applicable) |
| 75 | +umount /mnt/block1 |
| 76 | + |
| 77 | +# Remove the device from the guest |
| 78 | +echo 1 > /sys/bus/pci/devices/0000:00:01.0/remove |
| 79 | +``` |
| 80 | + |
| 81 | +Replace `0000:00:01.0` with the actual PCI BDF (Bus:Device.Function) address of |
| 82 | +the device, which can be found via `lspci`. |
| 83 | + |
| 84 | +### Step 2: Unplug from the host |
| 85 | + |
| 86 | +Issue a `DELETE` request to the corresponding device endpoint: |
| 87 | + |
| 88 | +```console |
| 89 | +curl --unix-socket $socket_location -i \ |
| 90 | + -X DELETE 'http://localhost/drives/block1' |
| 91 | +``` |
0 commit comments