How to Use the Command 'virt-install' (with Examples)

How to Use the Command 'virt-install' (with Examples)

The virt-install command is a utility provided by libvirt, a toolkit used to interact with virtualization technologies. This tool facilitates the creation and management of virtual machines (VMs) by allowing users to specify various parameters for the VM’s hardware and software setup. By using virt-install, users can effortlessly set up and begin the installation of operating systems on virtualized hardware. It is especially valuable for efficiently deploying VMs with customized configurations, making it a popular choice among system administrators and developers working with virtualization.

Create a Virtual Machine with 1 GB RAM and 12 GB Storage and Start a Debian Installation

Code:

virt-install --name vm_name --memory 1024 --disk path=path/to/image.qcow2,size=12 --cdrom path/to/debian.iso

Motivation:

This use case showcases a typical scenario for setting up a simple virtual machine, especially useful for testing or development purposes with minimal resource allocation. The use of Debian as an operating system is popular due to its stability and vast array of available software packages.

Explanation:

  • --name vm_name: Assigns a unique name to the virtual machine for easy identification and management.
  • --memory 1024: Reserves 1 GB (1024 MB) of RAM for the virtual machine, ensuring it has enough memory to operate smoothly without overcommitting host resources.
  • --disk path=path/to/image.qcow2,size=12: Creates a disk image at the specified path with a storage size of 12 GB, which is sufficient for installing the Debian OS and some basic applications.
  • --cdrom path/to/debian.iso: Initiates the installation process from the specified Debian ISO image, simulating booting from a CD-ROM.

Example Output:

When the command executes successfully, you may see output messages indicating the allocation of resources and the beginning of the OS installation process. A virtual machine will be created with the settings specified, ready for initial setup through the console.

Create a x86-64, KVM-Accelerated, UEFI-Based Virtual Machine with the Q35 Chipset, 4 GiB RAM, 16 GiB RAW Storage, and Start a Fedora Installation

Code:

virt-install --name vm_name --arch x86_64 --virt-type kvm --machine q35 --boot uefi --memory 4096 --disk path=path/to/image.raw,size=16 --cdrom path/to/fedora.iso

Motivation:

This configuration is ideal for users requiring a virtual machine that closely mimics modern physical machines, complete with UEFI firmware. It is perfect for scenarios demanding high performance and specific hardware emulation, such as software development, testing, and continuous integration environments.

Explanation:

  • --arch x86_64: Specifies the architecture of the VM, ensuring compatibility with 64-bit software and operating systems.
  • --virt-type kvm: Utilizes the Kernel-based Virtual Machine (KVM) for hardware acceleration, enhancing performance by leveraging host CPU resources directly.
  • --machine q35: Emulates the Q35 chipset, widely used in newer Intel-based systems, thus mimicking modern hardware environments.
  • --boot uefi: Uses UEFI firmware rather than traditional BIOS, aligning with current standards for booting and system management.
  • --memory 4096: Allocates 4 GiB of RAM, offering ample memory for running the Fedora OS and applications efficiently.
  • --disk path=path/to/image.raw,size=16: Creates a RAW disk image with 16 GiB, suitable for the Fedora OS installation and user data.
  • --cdrom path/to/fedora.iso: Boots and installs Fedora from the specified ISO image, one of the most popular Linux distributions known for its cutting-edge features.

Example Output:

The output will confirm the creation of the VM with UEFI, the Q35 chipset, and specified resources. Following creation, the VM will begin the OS installation, displaying the Fedora installer interface for further configuration.

Create a Diskless Live Virtual Machine without an Emulated Sound Device or a USB Controller

Code:

virt-install --name vm_name --memory 512 --disk none --controller type=usb,model=none --sound none --autoconsole none --install no_install=yes --cdrom path/to/tails.iso

Motivation:

This setup is suitable for cases where persistent storage is unnecessary, such as using a live CD distribution like Tails for secure and private activities. A stripped-down hardware configuration benefits environments where minimal hardware interaction is needed, maximizing resource efficiency.

Explanation:

  • --name vm_name: Specifies the virtual machine name for identification.
  • --memory 512: Allocates 512 MB of RAM, sufficient for running lightweight live distributions.
  • --disk none: Configures the VM as diskless, meaning no hard disk is attached, ideal for running OSes directly from live media.
  • --controller type=usb,model=none: Omits a USB controller, which may enhance security by reducing hardware attack vectors.
  • --sound none: Excludes a sound card, conserving resources and simplifying hardware configuration.
  • --autoconsole none: Disables automatic connection to the console upon VM startup.
  • --install no_install=yes: Prevents initiation of an OS installation process, appropriate for live media booting.
  • --cdrom path/to/tails.iso: Attaches the Tails live CD ISO as the boot medium.

Example Output:

The output messages will reflect the creation of a minimal-resource VM configured to boot from the specified Tails ISO image. The VM will remain diskless, ready to execute the live session without installation steps.

Create a Virtual Machine with 16 GiB RAM, 250 GiB Storage, 8 Cores with Hyperthreading, a Specific CPU Topology, and a CPU Model Sharing Most Features with the Host CPU

Code:

virt-install --name vm_name --cpu host-model,topology.sockets=1,topology.cores=4,topology.threads=2 --memory 16384 --disk path=path/to/image.qcow2,size=250 --cdrom path/to/debian.iso

Motivation:

Highly suitable for virtual environments requiring substantial computational power, such as enterprise applications, data processing tasks, or resource-intensive development environments. The configuration takes advantage of hyperthreading and a CPU topology that closely matches the host CPU, optimizing processing efficiency and compatibility.

Explanation:

  • --name vm_name: Sets a name for the virtual machine for organizational purposes.
  • --cpu host-model,topology.sockets=1,topology.cores=4,topology.threads=2: Configures the CPU to use the host model with a topology that includes 1 physical socket, 4 cores per socket, and 2 threads per core, taking advantage of hyperthreading technologies.
  • --memory 16384: Dedicates 16 GiB of RAM, ensuring ample memory availability for demanding applications.
  • --disk path=path/to/image.qcow2,size=250: Generates a disk image with 250 GiB, providing considerable storage for data-heavy applications.
  • --cdrom path/to/debian.iso: Begins OS installation using a specified Debian ISO, well-suited for various environments due to its robustness and package ecosystem.

Example Output:

Upon execution, you’ll see confirmations of the virtual machine’s deployment with high resource allocations. The installation of Debian will commence automatically, providing an extensive base for deploying heavy-duty applications.

Create a Virtual Machine and Kickstart an Automated Deployment Based on Fedora 35 Using Only Remote Resources

Code:

virt-install --name vm_name --memory 2048 --disk path=path/to/image.qcow2,size=20 --location=https://download.fedoraproject.org/pub/fedora/linux/releases/35/Everything/x86_64/os/ --extra-args="inst.ks=https://path/to/valid/kickstart.org"

Motivation:

Automating VM installations streamlines the deployment process, especially beneficial in large-scale or frequently updated environments. By utilizing remote installation resources and a kickstart file, users can configure Fedora installations without manual intervention or local ISO management, optimizing efficiency and consistency.

Explanation:

  • --name vm_name: Assigns a name to the VM for management purposes.
  • --memory 2048: Provides 2 GiB of RAM, sufficient for initiating automated deployments and running typical applications.
  • --disk path=path/to/image.qcow2,size=20: Specifies a 20 GiB disk image for the VM, ample for OS installation and basic software.
  • --location=https://download.fedoraproject.org/pub/fedora/linux/releases/35/Everything/x86_64/os/: Points to the remote repository hosting Fedora 35 installation files, eliminating the need for local ISO files.
  • --extra-args="inst.ks=https://path/to/valid/kickstart.org": Supplies extra parameters, including a kickstart file URL, to automate the setup process, defining system configurations like user accounts and package installations.

Example Output:

The system will output messages verifying contact with the Fedora installation repository, initializing the automated installation process through the specified kickstart file. Upon completion, the VM will be pre-configured based on the parameters set within the kickstart file.

Conclusion:

Using virt-install offers users flexible and powerful means to deploy virtual machines with precise specifications, catering to diverse use cases from lightweight testing environments to robust production platforms. Through the detailed explanations and examples provided, users can now leverage this command to efficiently manage their virtualization needs.

Related Posts

How to Use the Command 'kops' (with examples)

How to Use the Command 'kops' (with examples)

Kops, a popular choice for Kubernetes enthusiasts, enables users to create, destroy, upgrade, and maintain Kubernetes clusters effortlessly.

Read More
Understanding the 'errno' Command (with examples)

Understanding the 'errno' Command (with examples)

The errno command is a useful utility for programmers and system administrators alike.

Read More
How to Use the Command 'phpenmod' (with examples)

How to Use the Command 'phpenmod' (with examples)

The phpenmod command is a useful tool on Debian-based operating systems for enabling PHP extensions.

Read More