Skip to content

Instantly share code, notes, and snippets.

@stylesen
Created December 22, 2017 07:24
Show Gist options
  • Save stylesen/9328887688b95de6d8c65f78c7734cf4 to your computer and use it in GitHub Desktop.
Save stylesen/9328887688b95de6d8c65f78c7734cf4 to your computer and use it in GitHub Desktop.
Convert a sparse image to ext4 image and use python-guestfs to play with it
#!/usr/bin/env python
import os
import guestfs
import subprocess
image = '/tmp/rpb-console-image-hikey-20171218134627-9.rootfs.img'
ext4_img = 'rpb-console-image-hikey-20171218134627-9.rootfs.img.ext4'
guest = guestfs.GuestFS(python_return_dict=True)
output = subprocess.check_output(['dpkg', '-l', 'python-guestfs'],
stderr=subprocess.STDOUT)
print output
subprocess.check_output(['/usr/bin/simg2img', image, ext4_img],
stderr=subprocess.STDOUT)
guest.add_drive(ext4_img)
guest.launch()
devices = guest.list_devices()
print devices
guest.mount(devices[0], '/')
# Check if we have space left on the mounted image.
output = guest.df()
device, size, used, available, percent, mountpoint = output.split(
"\n")[1].split()
print output
guest.umount(devices[0])
subprocess.check_output(['/usr/bin/img2simg', ext4_img, image],
stderr=subprocess.STDOUT)
os.remove(ext4_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment