readme.fr

Hot opensource news

Update an OpenStack image

Quick presentation about 4 methodes (guestfish, guestmount, virt-* tools, qemu nbd) to update glance image. Add, remove, update files and install package before upload into glance.

Summary

  • guestfish : Image is not mount directly on you file system. It provides you a shell with bases bash commandes (touch, edit, rm, chmod …)
  • guestmount : Allows you to mount the image’s file system directly on your local file system
  • virt-* tools : tools or modification directly in the image
  • qemu nbd : mount filesystem without libguestfs

Examples

Get your image from glance

http://docs.openstack.org/image-guide/content/ch_modifying_images.html

List images into glance

glance image-list
+--------------------------------------+------------------------------------------------------------------+-------------+------------------+------------+--------+
| ID                                   | Name                                                             | Disk Format | Container Format | Size       | Status |
+--------------------------------------+------------------------------------------------------------------+-------------+------------------+------------+--------+
| 00d14bc2-7f43-4b3c-aed5-9cde8a694efb | CentOS 6.3 x86_64 final                                          | qcow2       | bare             | 755957760  | active |
glance image-list

Download your image

glance  image-download 00d14bc2-7f43-4b3c-aed5-9cde8a694efb > CentOS.6.3.final.qcow2

 

guestfish

Run a virtual machin (don’t forget run)

guestfish --rw -a CentOS.6.3.final.qcow2

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

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

><fs>
run
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00

Find and mount the logical volume root

<fs> list-filesystems
/dev/vda1: ext4
<fs> mount /dev/vda1 /

Make your changes

ls /etc/udev/rules.d/
60-raw.rules
70-persistent-cd.rules
70-persistent-net.rules
91-drm-modeset.rules
<fs> rm /etc/udev/rules.d/70-persistent-net.rules

Exit

<fs> exit

 

guestmount

Package

apt-get install libguestfs-toolsbash

Mount/Umount glance image

guestmount -a CentOS.6.3.final.qcow2 -i --rw /mnt/

ls /mnt/
bin  boot  cgroup  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  sbin  selinux  srv  sys  tmp  usr  var

Package install

To install packages like a running instance, you have to mount this

mount -t dev -o bind /dev /mnt/dev
mount --bind /dev/pts /mnt/dev/pts
mount -t proc -o bind /proc /mnt/proc
mount -t sys -o bind /sys /mnt/sys

Run chroot to enter into your image

chroot /mnt /bin/bash

Fix resolv.conf and do your stuff

echo 'nameserver 8.8.8.8' > /etc/resolv.conf

apt-get install

#or

yum install

#...
If you have not enough disk space, please refer to virt-resize section below

Clean your image

Manually

Clean

#Debian
apt-get autoremove
apt-get autoclean
apt-get clean

#Redhat
yum clean all

#tmp files
rm -rf /tmp/*

#user log
rm -f /var/log/wtmp /var/log/btmp
history -c

#... udev-persistent-net, .ssh ...

/sbin/shutdown -h now

Umount

umount /mnt/dev/pts
umount /mnt/dev
umount /mnt/proc
umount /mnt/sys
umount /mnt

Automatically

Or simply use virt-sysprep. This is a list of operation done by virt-sysprep

virt-sysprep --list-operations
bash-history * Remove the bash history in the guest
blkid-tab * Remove blkid tab in the guest
ca-certificates   Remove CA certificates in the guest
cron-spool * Remove user at-jobs and cron-jobs
dhcp-client-state * Remove DHCP client leases
dhcp-server-state * Remove DHCP server leases
dovecot-data * Remove Dovecot (mail server) data
flag-reconfiguration   Flag the system for reconfiguration
hostname * Change the hostname of the guest
kerberos-data   Remove Kerberos data in the guest
logfiles * Remove many log files from the guest
mail-spool * Remove email from the local mail spool directory
net-hwaddr * Remove HWADDR (hard-coded MAC address) configuration
package-manager-cache * Remove package manager cache
pam-data * Remove the PAM data in the guest
random-seed * Generate random seed for guest
rhn-systemid * Remove the RHN system ID
samba-db-log * Remove the database and log files of Samba
script * Run arbitrary scripts against the guest
smolt-uuid * Remove the Smolt hardware UUID
ssh-hostkeys * Remove the SSH host keys in the guest
ssh-userdir * Remove ".ssh" directories in the guest
sssd-db-log * Remove the database and log files of sssd
udev-persistent-net * Remove udev persistent net rules
user-account   Remove the user accounts in the guest
utmp * Remove the utmp file
yum-uuid * Remove the yum UUID

Umount to terminate your job

umount /mnt/dev/pts
umount /mnt/dev
umount /mnt/proc
umount /mnt/sys
umount /mnt

Clean

virt-sysprep -a CentOS.6.3.final.qcow2
Examining the guest ...
Performing "yum-uuid" ...
Performing "utmp" ...
Performing "udev-persistent-net" ...
Performing "sssd-db-log" ...
Performing "ssh-userdir" ...
Performing "ssh-hostkeys" ...
Performing "smolt-uuid" ...
Performing "script" ...
Performing "samba-db-log" ...
Performing "rhn-systemid" ...
Performing "random-seed" ...
Performing "pam-data" ...
Performing "package-manager-cache" ...
Performing "net-hwaddr" ...
Performing "mail-spool" ...
Performing "logfiles" ...
Performing "hostname" ...
Performing "dovecot-data" ...
Performing "dhcp-server-state" ...
Performing "dhcp-client-state" ...
Performing "cron-spool" ...
Performing "blkid-tab" ...
Performing "bash-history" ...

Finalisation

Converted back free space within the disk image to free space on the host

virt-sparsify --compress --tmp /tmp/ CentOS.6.3.final.qcow2 CentOS.6.3.final_2.0.qcow2
Create overlay file to protect source disk ...
Examine source disk ...
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ --:--
Fill free space in /dev/vda1 with zero ...
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
Copy to destination and make sparse ...

Sparsify operation completed with no errors.  Before deleting the old 
disk, carefully check that the target disk boots and works correctly.

Upload your new image version

glance --insecure image-create --name="CentOS 6.3 x86_64 final2.0" --disk-format=qcow2 --container-format=bare --file CentOS.6.3.final.qcow2

3407492

virt-* tools

A lot of tools are available :

  • virt-edit for editing a file inside of an image.
  • virt-df for displaying free space inside of an image.
  • virt-resize for resizing an image.
  • virt-sysprep for preparing an image for distribution (for example, delete SSH host keys, remove MAC address info, or remove user accounts).
  • virt-sparsify for making an image sparse
  • virt-p2v for converting a physical machine to an image that runs on KVM
  • virt-v2v for converting Xen and VMware images to KVM images

Package installation

apt-get install libguestfs-tools

 

Example resize with virt-resize

Identify the root partitions

virt-filesystems --long --parts --blkdevs -h -a CentOS.6.3.final.qcow2 
Name       Type       MBR  Size   Parent
/dev/sda1  partition  83   1022M  /dev/sda
/dev/sda   device     -    1,0G   -

Here we want resize /dev/sda1
Now create a new image with your needed size (Example : 2G)

qemu-img create -f qcow2 CentOS_6.3_x86_64_final-resized.qcow2 2G

Write a resized copy of the original into the new image

virt-resize --expand /dev/sda1 CentOS.6.3.final.qcow2  CentOS_6.3_x86_64_final-resized.qcow2
Examining CentOS.6.3.final.qcow2 ...
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ --:--
**********

Summary of changes:

/dev/sda1: This partition will be resized from 1022,0M to 2,0G.  The 
    filesystem ext4 on /dev/sda1 will be expanded using the 'resize2fs' 
    method.

**********
Setting up initial partition table on CentOS_6.3_x86_64_final-resized.qcow2 ...
Copying /dev/sda1 ...
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ 00:00
 100% ⟦▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓⟧ --:--
Expanding /dev/sda1 using the 'resize2fs' method ...

Resize operation completed with no errors.  Before deleting the old 
disk, carefully check that the resized disk boots and works correctly.

Done, you are able to use your resized image.

qemu nbd

requirement

apt-get install nbd-client qemu-utils
modprobe nbd max_part=63

Mount in RW with ndb

qemu-nbd -c /dev/nbd0 CentOS.6.3.final.qcow2 
mount /dev/nbd0p1 /mnt/

Umount your image

umount /mnt/
nbd-client -d /dev/nbd0

 

 

Helpful links

 

 

talset
talset on Githubtalset on Linkedin

, , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *