LinuxCommandLibrary

virt-customize

TLDR

Set root password

$ virt-customize -a [image.qcow2] --root-password password:[newpass]
copy
Install packages
$ virt-customize -a [image.qcow2] --install [nginx,vim]
copy
Upload file to image
$ virt-customize -a [image.qcow2] --upload [local.conf]:[/etc/app.conf]
copy
Run shell script
$ virt-customize -a [image.qcow2] --run [setup.sh]
copy
Run command
$ virt-customize -a [image.qcow2] --run-command "[systemctl enable nginx]"
copy
Inject SSH key
$ virt-customize -a [image.qcow2] --ssh-inject [root]:file:[~/.ssh/id_rsa.pub]
copy
Set timezone
$ virt-customize -a [image.qcow2] --timezone [America/New_York]
copy
Set hostname
$ virt-customize -a [image.qcow2] --hostname [myserver]
copy

SYNOPSIS

virt-customize -a image [options]

DESCRIPTION

virt-customize modifies virtual machine disk images by installing packages, editing configuration, uploading files, and running scripts. It is part of the libguestfs tools suite.
The tool works by mounting the disk image using libguestfs and performing operations in a secure, isolated environment. The VM must be shut down before customization.
Customization scripts run inside a small appliance chrooted into the guest filesystem. Limited network access is available for package downloads.
This tool is useful for preparing cloud images, creating golden images, and automating VM provisioning.

PARAMETERS

-a, --add file

Disk image file to customize.
--root-password method:arg
Set root password.
--password user:method:arg
Set user password.
--install packages
Install packages (comma-separated).
--uninstall packages
Remove packages.
--update
Update all packages.
--upload local:remote
Upload file to image.
--copy-in local:remotedir
Copy files/dirs into image.
--run script
Run shell script in image.
--run-command cmd
Run single command.
--ssh-inject user:file:keyfile
Inject SSH public key.
--timezone tz
Set timezone.
--hostname name
Set hostname.
--edit file:expr
Edit file with Perl expression.
--delete path
Delete file or directory.
--commands-from-file file
Read commands from file.
-v, --verbose
Enable verbose output.

CAVEATS

VM must be shut down. Don't run as root unless necessary. Snapshot disk before modifying production images. Some operations require specific guest OS support. SELinux relabeling may be needed.

HISTORY

virt-customize is part of libguestfs, created by Red Hat for virtual machine image manipulation. The toolset provides safe, scriptable access to VM disk images without requiring the guest to be running.

SEE ALSO

Copied to clipboard