Today, I am using qemu-kvm to install a virtual machine. Because there is already an image file of the virtual machine (in qcow2 format), it is very simple to create a virtual machine. Just start it from the image with the following command.
- qemu-kvm -cpu host -smp 2 -name cenos6 -m 2048 -drive file=/var/tmp/CentOS---6.6-64bit---2015-01-29-a.qcow2,if=ide,media=disk,format=qcow2 -boot order=c -usbdevice tablet -nographic
1) Mount the image file in qcow2 format, which requires the help of the qemu-nbd command, as follows:
- // load nbd module first
- $ modprobe nbd max_part=8
- //Create a connection associated with the nbd device for the image file
- $ qemu-nbd -c /dev/nbd0 CentOS---6.6-64bit---2015-01-29-a.qcow2
- //Check whether the connection is successfully created, if there is an nbd0p1 device, it is successful
- $ ls -l /dev/nbd0*
- //mount image file
- $ mount /dev/nbd0p1 /mnt/img
2) Use chroot to change root's password.
Now that we have mounted the OS image, we can do whatever we want.
- //Log in to the shell of the virtual machine
- $ /mnt/img/bin/sh
- sh-4.1#
- </pre><pre name="code" class="html">//chroot first, then change root's password
- sh-4.1# chroot /mnt/img/
- [root@server-185 /]# passwd root
- Changing password for user root.
- New password:
- Retype new password:
- passwd: all authentication tokens updated successfully.
By now, the password has been changed and it is time to log back into the virtual machine. But, don't forget to umount image, delete nbd connection and unload nbd module.
3) Aftermath work
$ umount /mnt/img
$ qemu-nbd -d /dev/nbd0
- $ rmmod nbd
Be sure to develop a good habit of using up and releasing.