how to convert raw images to qcow2 image and register to openstack glance

  • convert raw image to qcow2 image
qemu-img convert -f raw -O qcow2 source.img target.qcow2

https://docs.openstack.org/image-guide/convert-images.html


  • register image to glance in order to provision VM with the image, for example
glance --insecure image-create --name vPBX-raw --disk-format raw \
        --container-format bare --visibility public --progress \
        --file vPBX_Templete.img

glance --insecure image-create --name vPBX-qcow2 --disk-format qcow2 \
        --container-format bare --visibility public --progress \
        --file vPBX_Templete.qcow2

glance --insecure image-create --name utm-kvm --disk-format qcow2 \
        --container-format bare --property hw_disk_bus=ide \
        --property hw_vif_model=virtio --visibility public --progress \
        --file utm-kvm.qcow2

  • how to append authentication key to downloaded cloudinit rootfs for LXC
# download a tar file of a root file system
wget https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-root.tar.gz

# create a backing file and attach it to a loopback device 
truncate --size 2GB xenial.img
sudo losetup -f xenial.img

# find what /dev/loopX was used - assume it was /dev/loop1 at this time
losetup -a

# write out a filesystem and mount it
sudo mkfs /dev/loop1
mkdir mnt
sudo mount /dev/loop1 mnt

# extract the root fs tar into the image file
cd mnt
tar -xvf ../xenial-server-cloudimg-amd64-root.tar.gz

# enable automatic login with certificates
mkdir root/.ssh
cat ~/.ssh/id_rsa.pub >> root/.ssh/authorized_keys

# clean up
cd ../
sudo umount /dev/loop1

# register the built-up image to glance
glance image-create --name xenial-lxc --visibility public \
    --disk-format raw --container-format bare \
    --progress --file xenial.img

  • how to append authentication key to downloaded cloudinit image for KVM
# download a tar file of a root file system
wget https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img

# prepare nbd module and add nbd into /etc/modules for boot-time module loading
modprobe nbd

# mount and add authentication key into the image
qemu-nbd --connect=/dev/nbd0 ./xenial-server-cloudimg-amd64-disk1.img
mkdir mnt
mount /dev/nbd0p1 mnt
cd mnt
mkdir root/.ssh
cat ~/.ssh/id_rsa.pub >> root/.ssh/authorized_keys
cd ..
umount mnt
qemu-nbd --disconnect /dev/nbd0

# register the built-up image to glance
glance --insecure image-create --name xenial-kvm --disk-format raw \
        --container-format bare --visibility public --progress \
        --file xenial-server-cloudimg-amd64-disk1.img

  • onetime password change to KVM instance from cloudinit image
sudo apt-get install libguestfs-tools
sudo virt-sysprep -a xenial-server-cloudimg-amd64-disk1.qcow2 \
                    --root-password password:xxxx

Leave a Reply

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