d2vm Documentation

repository·main·Indexed 18 days ago

https://github.com/linka-cloud/d2vm

d2vm is a tool for converting Docker images and Dockerfiles into bootable Linux Virtual Machine images, supporting distributions such as Ubuntu, Debian, Alpine, CentOS, Rocky Linux, and AlmaLinux. It enables the creation of VM images in formats like .qcow2, .vdi, and .vmdk, and can generate KubeVirt Container Disk Images. The tool includes commands for building, converting, and running VM images via drivers like QEMU.

Tokens
22.6K
Snippets
84
Records
107
Agent score
62%

What's inside d2vm

  1. Overview of d2vm

    main

    d2vm (Docker to Virtual Machine) is a tool designed to build bootable Linux Virtual Machine (VM) images from Docker images. It is currently in alpha status.

    Key Capabilities:

    • Converts existing Docker images into VM images.
    • Builds VM images directly from a Dockerfile.
    • Generates KubeVirt Container Disk Images.

    Important Constraints:

    • Linux Only: Only building Linux VM images is supported.
    • Distribution Discovery: The tool relies on the /etc/os-release file within the image to identify the distribution and install the appropriate Kernel. If this file is missing, the build will fail.
    • Distroless Support: Distroless images are not supported because they lack the necessary metadata and tools for kernel installation.
  2. How d2vm handles non-Linux hosts and non-root users

    main

    Starting from v0.1.0, d2vm provides a fallback mechanism for environments where direct hardware/disk manipulation is restricted:

    • Non-Linux Hosts (e.g., macOS): d2vm automatically runs build and convert commands inside a Docker container.
    • Non-Root Users on Linux: If running without root privileges, d2vm automatically executes build and convert commands inside Docker to ensure necessary operations can proceed.
  3. Build a VM image from a Dockerfile

    main

    Use the d2vm build command to create a VM image directly from a Dockerfile and a build context. This command accepts most flags used by docker build.

    Example: Build Ubuntu VM from Dockerfile

    sudo d2vm build -p MyP4Ssw0rd -f ubuntu.Dockerfile -o ubuntu.qcow2 .

    Example: Build VirtualBox image

    sudo d2vm build -p MyP4Ssw0rd -f ubuntu.Dockerfile -o ubuntu.vdi .
    sudo d2vm build -p MyP4Ssw0rd -f ubuntu.Dockerfile -o ubuntu.qcow2 .
  4. Manage a running VM via serial console

    main

    When running a VM via the serial console, you can interact with the shell directly. To exit the terminal and stop the VM properly, use the poweroff command inside the VM:

    sudo poweroff

    To find the IP address of the VM for SSH connection, use:

    hostname -I
    # or
    ip a show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1
  5. Run a virtual machine image with d2vm run

    main

    Use the d2vm run command to execute a virtual machine image. This command is the entry point for launching the VM in various environments (such as QEMU, VirtualBox, or Hetzner Cloud).

    Available Options

    Local Options:

    • -h, --help: Display help information for the run command.

    Global Options (Inherited):

    • --time <string>: Enable formatted timed output. Valid formats are:
      • relative (or rel / r)
      • full (or f)
      • Default is "none".
    • -v, --verbose: Enable verbose output for detailed execution logs.
    d2vm run [image] [options]
  6. Run a VM image using d2vm run qemu

    main

    Once a VM image has been built, you can run it using the qemu driver. This command starts the virtual machine with specified resource allocations.

    Use the following flags:

    • qemu: The driver to use.
    • --mem <SIZE>: Amount of memory to allocate (e.g., 4096).
    • --cpus <COUNT>: Number of CPUs to allocate.

    Example run command:

    d2vm run qemu --mem 4096 --cpus 4 workstation.qcow2
    d2vm run qemu --mem 4096 --cpus 4 $IMAGE
  7. Prerequisites for d2vm

    main

    Before using d2vm, ensure your host environment meets the following requirements based on your operating system.

    macOS (osx)

    Linux

    • Required: Docker
    • Required System Packages:
      • util-linux
      • udev
      • parted
      • e2fsprogs
      • dosfstools (required when using FAT32)
      • mount
      • tar
      • extlinux (required when using syslinux)
      • qemu-utils
      • cryptsetup (required when using LUKS)
    • Optional: QEMU, VirtualBox

    Privileges

    sudo or root privileges are required for operations such as mounting disk images, managing loopback devices, and invoking Docker commands.

  8. Generate bash autocompletion for d2vm

    main

    You can generate a bash autocompletion script for the d2vm CLI to improve your shell experience. This script requires the bash-completion package to be installed on your system via your OS package manager.

    Load completions for the current session

    To immediately enable autocompletion in your active terminal session, run:

    source <(d2vm completion bash)

    Load completions permanently

    To ensure autocompletion is available in every new shell session, write the output to your system's completion directory. Note that you must start a new shell for these changes to take effect.

    On Linux:

    d2vm completion bash > /etc/bash_completion.d/d2vm

    On macOS:

    d2vm completion bash > $(brew --prefix)/etc/bash_completion.d/d2vm
  9. Enable shell completion for d2vm

    main

    The d2vm program supports shell completion for bash, zsh, and fish. To enable it for your current session, run:

    source <(d2vm completion $(basename $SHELL))

    To view help for installing completion files in your shell's completion directory, use:

    d2vm completion $(basename $SHELL) --help
    source <(d2vm completion $(basename $SHELL))
  10. Generate shell autocompletion for d2vm

    main

    You can generate autocompletion scripts for the d2vm CLI to enable tab-completion in your terminal. The completion command produces the script required for your specific shell (bash, fish, powershell, or zsh).

    To use the generated script, refer to the specific documentation for your shell (e.g., d2vm_completion_bash.md) for instructions on how to source or install the output into your shell configuration.

    d2vm completion [options]