How to import Cloud Image to Virtual Machines on Debian 10

Download the latest Debian 10 cloud image in qcow2 format

$ curl -L -O https://cloud.debian.org/images/cloud/buster/latest/debian-10-genericcloud-amd64.qcow2
$ du -sh *.qcow2
223M    debian-10-genericcloud-amd64.qcow2

Reset root password on cloud image

Your system might not have install virt-customize

To install virt-customize:

$ sudo apt update
$ sudo apt install libguestfs-tools
$ which virt-customize
/usr/bin/virt-customize

To reset root password, set hostname and apply new host SSH-key with virt-customize:

$ virt-customize --add "PathToTagetDisk" \
--root-password password:MyRootPW \
--hostname "VMName" \ 
--firstboot-command 'ssh-keygen -A && systemctl restart sshd'

and if you want to allow SSH login with password for root (WARNING, only for test-servers!)

$ virt-customize --add "PathToTagetDisk" \
--root-password password:MyRootPW \
--append-line "/etc/ssh/sshd_config.d/rootlogin.conf":"PermitRootLogin yes" \
--append-line "/etc/ssh/sshd_config.d/rootlogin.conf":"PasswordAuthentication yes" \
--hostname "VMName" \ 
--firstboot-command 'ssh-keygen -A && systemctl restart sshd'

Copy cloud image to images directory for libvirt

$ sudo cp debian-10-genericcloud-amd64.qcow2 /var/lib/libvirt/images/d10c.qcow2

Create VM with import option

$ virt-install --import \
--connect qemu:///system \
--autostart \
--name d10c \
--os-variant debian10 \
--memory 2048 \
--vcpus 2 \
--disk /var/lib/libvirt/images/d10c.qcow2,bus=virtio \
--network default

Login to VM

Make sure the VM is up-n-running

$ virsh dominfo d10c
Id:             76
Name:           d10c
UUID:           5dbcd917-5984-42c2-86c5-75882a1d1216
OS Type:        hvm
State:          running
CPU(s):         2
CPU time:       12.3s
Max memory:     2097152 KiB
Used memory:    2097152 KiB
Persistent:     yes
Autostart:      enable
Managed save:   no
Security model: apparmor
Security DOI:   0
Security label: libvirt-5dbcd917-5984-42c2-86c5-75882a1d1216 (enforcing)

Get the IP from the VM

$ virsh domifaddr d10c
Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet11     52:54:00:36:28:f6    ipv4         192.168.122.161/24

Edit /etc/hosts to add d10c

$ sudo vi /etc/hosts
...
192.168.122.161  d10c   # Debian 10 Cloud Image
...

Now, you can login with the password you've reset earlier

$ ssh d10c
Connection closed by 192.168.122.161 port 22

Open the VM and reinstall openssh-server

Open the VM with virt-viewer or vnc-console

$ virt-viewer d10c

Screen for virt-viewer

Screen for vnc-console

Login as root to reinstall openssh-server

# apt update
# apt install --reinstall openssh-server

Now you'll need to add user account to login with

Make sure to add the user to sudo group

# adduser tchung
...
# adduser tchung sudo

Make sure to enable PasswordAuthentication in sshd_config

# vi /etc/ssh/sshd_config
...
PasswordAuthentication yes
...
# systemctl reload sshd

Success

Now, you should be able to login the VM which was created from Cloud Image

$ ssh d10c
tchung@d10c's password: 
Linux debian 4.19.0-17-cloud-amd64 #1 SMP Debian 4.19.194-1 (2021-06-10) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
tchung@debian:~$


CategorySystemAdministration | CategoryVirtualization | CategorySoftware