Change root password on kvm qcow2 image

How to change root password on kvm qcow2 image

guestfish is an interactive shell that you can use from the command line or from shell scripts to access guest virtual machine file systems. All of the functionality of the libguestfs API is available from the shell.

We will use the guestfish tool to modify the password details and Changing the ‘root’ password on kvm qcow2 (images) for OpenStack environments.

Install lib modules

# yum -y install libguestfs libguestfs-tools*

Generate an encrypted password

# openssl passwd -1 "password"

I used the centos image “CentOS-7-x86_64-GenericCloud-1608.qcow2”

# guestfish -a CentOS-7-x86_64-GenericCloud-1608.qcow2

Welcome to guestfish, the guest filesystem shell for
editing virtual machine filesystems and disk images.

Type: ‘help’ for help on commands
‘man’ to read the manual
‘quit’ to quit the shell

<fs> run
><fs> list-filesystems
/dev/sda1: xfs
><fs> mount /dev/sda1 /
><fs> vi /etc/shadow (update the encrupted password)
><fs> quit

 

Create new VM from this qcow2 image and verify the modified password.

 

Errors:

# guestfish -a CentOS-7-x86_64-GenericCloud-1608.qcow2

Welcome to guestfish, the guest filesystem shell for
editing virtual machine filesystems and disk images.

Type: ‘help’ for help on commands
‘man’ to read the manual
‘quit’ to quit the shell

><fs> run
libguestfs: error: could not create appliance through libvirt.

Try running qemu directly without libvirt using this environment variable:
export LIBGUESTFS_BACKEND=direct

Original error from libvirt: Cannot access storage file ‘/root/CentOS-7-x86_64-GenericCloud-1608.qcow2’ (as uid:107, gid:107): Permission denied [code=38 int1=13]
><fs> quit
I am running this commands from the openstack qemu environment and it should need to move image file to libvirt image folder.

# mv /root/CentOS-7-x86_64-GenericCloud-1608.qcow2 /var/lib/libvirt/images/

That’s it. Try again with run command.