libguestfs python example

I wrote this Python libguestfs example as a suggestion for the Fedora AutoQA project. This Python script can be used to preload a kickstart file on to a virt filesystem, the idea being you can script a Fedora DVD install without requiring any network access at all (using ks=hd:vda1:/ks.cfg).

Edit:

Since the link above is now broken, the test script is attached below.


#!/usr/bin/python

# To see the Python docs, do: help (guestfs)
# To see the general docs http://libguestfs.org/guestfs.3.html
# For FAQ see http://libguestfs.org/FAQ.html

import os
import guestfs

g = guestfs.GuestFS ()

# You would do this add_drive command, but instead to illustrate
# I will create a blank test image.
#g.add_drive ("/path/to/virt/disk")
f = open ("/tmp/test.img", "w")
f.truncate (500 * 1024 * 1024)
f.close ()
g.add_drive ("/tmp/test.img")

g.launch ()

# This command makes a single MBR-style large partition covering the
# whole disk, you may instead want to make several partitions, LVM LVs
# etc.  See the documentation.
g.part_disk ("/dev/sda", "mbr")

g.mkfs ("ext2", "/dev/sda1")

g.mount ("/dev/sda1", "/")

# Don't have a ks.cfg to upload, so create a dummy one:
f = open ("/tmp/ks.cfg", "w")
f.write ("# this would be the kickstart file\n")
f.write ("#\n")
f.write ("# end of file\n")
f.close ()

# Write it to the disk.
g.upload ("/tmp/ks.cfg", "/ks.cfg")

g.sync ()
g.umount_all ()

# Now go look at /tmp/test.img which is the virtual disk image.
#
# $ guestfish --ro -a test.img 
# 
# Welcome to guestfish, the libguestfs filesystem interactive shell for
# editing virtual machine filesystems.
# 
# Type: 'help' for help with commands
#       'quit' to quit the shell
# 
# ><fs> run
# ><fs> mount /dev/sda1 /
# ><fs> ll /
# total 14
# drwxr-xr-x  3 root root  1024 Feb  1 19:36 .
# dr-xr-xr-x 21 root root     0 Jan 29 14:39 ..
# -rw-r--r--  1 root root    51 Feb  1 19:36 ks.cfg
# drwx------  2 root root 12288 Feb  1 19:36 lost+found
# 
# ><fs> cat /ks.cfg 
# # this would be the kickstart file
# #
# # end of file
# 
# ><fs> exit
About these ads

2 Comments

Filed under Uncategorized

2 responses to “libguestfs python example

  1. bomko

    is it possible to get python example on copy_file_to_device function, cause i cant get it to work.

    • rich

      Can you give me more information about what you’re trying to do?

      One thing to note about g.copy_file_to_device: It copies from a file within the guest filesystem.

      If what you want to do is to upload from the host into the disk image, then you need to call g.upload instead.

      For more information on this see http://libguestfs.org/guestfs.3.html#copying

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s