Setting up a custom disk image
Note
You can only use images set up following this guide.
You can also view out-of-the-box images in Yandex Cloud Marketplace.
You can use your own file with a Linux VM disk image. Once your image is set up, upload it to Compute Cloud.
If you have developed software that might be helpful to others, consider listing it in Yandex Cloud Marketplace.
Warning
Images with an UEFI/EFI
Configure the OS as per the requirements
Make sure the following requirements for boot disk images are met:
- The
virtio-net,virtio-blk, andvirtio-pcidrivers are installed. If you are going to attach file storages to your VM, you also need to install thevirtiofsdriver. For details, see this guide. - The
ttyS0terminal (COM1) is configured as the serial console. For details, see this guide. - The network interface runs correctly when starting the VM and gets the IP address via DHCP.
- The
cloud-initpackage is installed and configured to work with our metadata service. Runsudo yum install cloud-initto install the package for CentOS orsudo apt update && sudo apt install -y cloud-initto install it for Debian or Ubuntu. - For an image based on an AMI, cloud platform verification for the VM instance is disabled in the
cloud-initsettings. For details, see this guide. - The system firewall is configured to only allow the minimum ports required for your applications to run securely and the SSH port (by default, 22 TCP).
- The SSH server starts automatically at VM startup.
- The services running your application are resilient to VM reboots.
- The boot disk uses MBR partitioning.
- The disk is mounted by its UUID rather than by name.
- The file system is not encrypted.
Install virtio drivers
To successfully upload your image, make sure to install the virtio-blk, virtio-net, and virtio-pci drivers. To use Compute Cloud file storages, install virtiofs.
Most modern distributions include the virtio drivers by default. They can be compiled as separate .ko files or built into the kernel itself.
Follow the steps below to check if the drivers are installed in your OS. If not, add them.
-
Check if the drivers are built into the kernel:
How to check it
Run this command:
grep -E -i "VIRTIO_(BLK|NET|PCI|FS)" /boot/config-$(uname -r)If you see no lines starting with
CONFIG_VIRTIO_BLK=,CONFIG_VIRTIO_NET=,CONFIG_VIRTIO_PCI=, andCONFIG_VIRTIO_FS=on your screen, recompile the Linux kernel with the virtio drivers. Otherwise, proceed to the next steps. -
If you get the
CONFIG_VIRTIO_BLK=y,CONFIG_VIRTIO_NET=y,CONFIG_VIRTIO_PCI=y, andCONFIG_VIRTIO_FS=ylines in Step 1, make sure the drivers are included in the kernel:How to check if drivers are included in a kernel
Run this command:
grep -E "virtio(_blk|_net|_pci|fs)" /lib/modules/"$(uname -r)"/modules.builtin- If you get the lines with the
virtio_net.ko,virtio_blk.ko,virtio_pci.koandvirtiofs.kofile names, the drivers are included in the kernel and you do not need to install them. - If you see no such lines on your screen, recompile the Linux kernel with the virtio drivers.
- If you get the lines with the
-
If you get the
CONFIG_VIRTIO_BLK=m,CONFIG_VIRTIO_NET=m,CONFIG_VIRTIO_PCI=m, andCONFIG_VIRTIO_FS=mlines in Step 1, check that the drivers are installed as kernel modules:How to check kernel modules
CentOS/FedoraDebian/UbuntuRun this command:
sudo lsinitrd /boot/initramfs-$(uname -r).img | grep -E "virtio(_blk|_net|_pci|fs)"-
If you get the lines with the
virtio_net.ko.xz,virtio_blk.ko.xz,virtio_pci.ko.xz, andvirtiofs.ko.xzfile names, the drivers are installed as kernel modules. -
If you see no such lines on your screen, create a backup of the
initramfsfile and install the drivers:sudo cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.bak sudo mkinitrd -f --with=virtio_blk --with=virtio_net --with=virtio_pci --with=virtiofs /boot/initramfs-$(uname -r).img $(uname -r)If you get an error saying
Command 'mkinitrd' not found, usedracutto install the drivers:sudo dracut -f --add-drivers "virtio_blk virtio_net virtio_pci virtiofs" /boot/initramfs-$(uname -r).img $(uname -r)Then, reboot your system and check if the drivers are now included in the
initramfsfile and loaded:sudo lsinitrd /boot/initramfs-$(uname -r).img | grep -E "virtio(_blk|_net|_pci|fs)" find /lib/modules/"$(uname -r)"/ -name "virtio*" | grep -E "(blk|net|pci|fs)"Each command should output lines with the
virtio_net.ko.xz,virtio_blk.ko.xz,virtio_pci.ko.xz, andvirtiofs.ko.xzfile names.
Run this command:
lsinitramfs /boot/initrd.img-$(uname -r) | grep -E "virtio(_blk|_net|_pci|fs)"-
If you get the lines with the
virtio_net.ko,virtio_blk.ko,virtio_pci.ko, andvirtiofs.kofile names, the drivers are installed as kernel modules. -
If you see no such lines on your screen, install the drivers:
echo -e "virtio_blk\nvirtio_net\nvirtio_pci\nvirtiofs" | sudo tee -a /etc/initramfs-tools/modules sudo update-initramfs -uThen, reboot your system and check if the drivers are now included in the
initrdfile and loaded:lsinitramfs /boot/initrd.img-$(uname -r) | grep -E "virtio(_blk|_net|_pci|fs)" find /lib/modules/"$(uname -r)"/ -name "virtio*" | grep -E "(blk|net|pci|fs)"Each command should output lines with the
virtio_net.ko,virtio_blk.ko,virtio_pci.ko, andvirtiofs.kofile names.
-
Configure the serial console
A serial console enables accessing a VM regardless of the network or OS state. Use the serial console to troubleshoot your VM or when you have SSH access issues. For more information, see Getting started with a serial console.
To enable serial console access to your VM, configure the image to use ttyS0 (COM1) as the system console:
-
In the
/etc/default/grubfile with GRUB settings, addconsole=ttyS0to theGRUB_CMDLINE_LINUXparameter value. The line with this parameter should look like this:GRUB_CMDLINE_LINUX="foo=bar baz console=ttyS0" -
Make sure the
/etc/securettyfile with a list of terminals therootuser can use to log in to the OS contains thettyS0line. If there is no such file, create one.Note
In some operating systems,
/etc/securettyis not used and therootuser can access all terminals by default. To configure access using this file, add a specification for thepam_securetty.somodule to the/etc/pam.d/loginfile. For more information, seeman pam_securettyandman pam.d. -
Run the following commands:
CentOS/FedoraDebian/Ubuntusudo stty -F /dev/ttyS0 9600 # Sets the recommended baud rate for the `ttyS0` terminal to 9600. sudo grub2-mkconfig -o /boot/grub2/grub.cfg # Generates a GRUB configuration file. sudo systemctl start getty@ttyS0 # Starts `getty` on `ttyS0`. sudo systemctl enable getty@ttyS0 # Tells the system to start `getty` every time it boots up.sudo stty -F /dev/ttyS0 9600 # Sets the recommended baud rate for the `ttyS0` terminal to 9600. sudo update-grub # Generates a GRUB configuration file. sudo systemctl start getty@ttyS0 # Starts `getty` on `ttyS0`. sudo systemctl enable getty@ttyS0 # Tells the system to start `getty` every time it boots up. -
Reboot the OS.
After creating a VM from your image, you need to additionally configure it for using the serial console.
Disable cloud platform verification when creating an image in Amazon EC2
When using an AMI to create an image in Amazon EC2, cloud-init verifies that the VM is running within the Amazon EC2 environment. VMs launched in Yandex Compute Cloud will fail the verification and may run incorrectly.
To avoid this, disable metadata verification on your VMs: in the /etc/cloud/cloud.cfg.d directory, create a configuration file, e.g., 99-ec2-datasource.cfg, and add the following code to it:
#cloud-config
datasource_list: [Ec2]
datasource:
Ec2:
strict_id: false
Alert
The cloud-init package releases starting from version 25.1.4, as well as 24.4.1-0ubuntu0~20.04.3+esm1, and 23.1.2-0ubuntu0~18.04.1+esm1, introduce changes
After the upgrade, such virtual machines may have issues when starting from a snapshot or boot disk VM image.
To fix this issue, run this command on your existing VM:
echo "datasource_list: [Ec2]" | sudo tee /etc/cloud/cloud.cfg.d/99999_ds_list.cfg
Install drivers for GPU compatibility
If you need to use a GPU with your VM, make sure to install NVIDIA drivers while preparing the image.
Create an image file
Supported formats: Qcow2, VMDK, RAW, and VHD.
We recommend using the Qcow2 format with an optimized cluster size for faster import. To convert your image from other formats, use qemu-img:
qemu-img convert -p -O qcow2 -o cluster_size=2M <your_image_file_name> <new_image_file_name>
Note
Do not use compression or archiving utilities when preparing the image file. To make creating VMs faster, optimize the image.