Modify QCOW2 images using guestfish

If we want to modify the QCOW2 image that we have just created or the one that is downloaded from the Internet, without booting, we could do so by using the guestfish utility. This is provided by the libguestfs library.

Installing guestfish

The installation of guestfish is fairly simple.

apt-get install libguestfs-tools

On some versions (like 12.04), the command is apt-get install guestfish. It will also ask for the creation of the virtual appliance; please do so. Once the appliance is installed, you can then execute the command sudo guestfish (or as root).

Loading the images

Once in the guestfish console, you can then load the qcow2 image by typing the command add <full path to qcow2 imange>.

add /home/alokas/mysql.qcow2

We will then type the command run, which will load the image.

Loading the images

Once the slider reaches 100%, we are ready to proceed further.

Modify the files on the image

We can mount the volumes that are available in the image; we can execute the command list-filesystems.

><fs> list-filesystems
/dev/sda1: ext4

We can now mount the volume using the command format mount <volume name> <mount point>.

mount /dev/sda1 /

Once the filesystem is mounted, we can modify the files at our will by using vi; for example, to modify the SSH configuration (vi /etc/ssh/sshd_config).

We could also mount the filesystem as read only if we don't want to accidentally make changes.

Send commands

We can also send commands to guestfish. However, care should be taken that the command doesn't have user interactions or the guestfish shell will freeze.

The command format is command "bash -c '<command to be passed>'".

Example: Adding a user to the Ubuntu QCOW2 image

For example, we need to add a user (testuser) to an Ubuntu system with the password test123 and add it to the sudoers group, so we can log in using the console (or SSH if the SSH configuration is modified to allow it). We will first generate the password hash using perl (outside of guestfish).

pass=$(perl -e 'print crypt($ARGV[0], "password")' "test123")

This will store the hash in a variable called pass; we will check the output by echo $pass, and we have to note down this hash (in our case, this is paOElDH7voHBo).

Example: Adding a user to the Ubuntu QCOW2 image

We will then use the command format of guestfish and execute.

Example: Adding a user to the Ubuntu QCOW2 image

This will add the user testuser with the password test123. Using the quit command, we can get out of the shell and use the modified image. For more information, read the manual using the man guestfish command.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.10.85