guestfish — not just for disk images

You know you can run libguestfs / guestfish on anything, right? This question came up at one of the Summit parties, so maybe some people don’t know. But it works on USB keys, CDs, hard disks, partitions, and files (and more).

There is a practical benefit to using libguestfs on USB keys, because it adds a layer of isolation between the unknown contents of the USB key and your host kernel. Mounting random disks on your host kernel is deeply insecure.

In the example below, I’ve stuck in a random USB key (empty and safe as it happens, but how was I to know?) and I ran guestfish on the first partition directly:

$ guestfish -a /dev/sdb1

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
><fs> mount-options "" /dev/vda /
><fs> ll /
total 36
drwxr-xr-x  2 root root 32768 Jan  1  1970 .
drwxr-xr-x 24  500  500  4096 Jul  9 17:11 ..
><fs> df-h
Filesystem            Size  Used Avail Use% Mounted on
/dev/vda              3.7G   32K  3.7G   1% /sysroot

(I don’t need to be root, because I’ve added myself to the “disk” group, which means I can access any block device.)

Above, I ran guestfish on the USB key’s first partition (/dev/sdb1, so that inside libguestfs the partition appears as a whole device (/dev/vda). Equally I could have run guestfish on the whole USB key (/dev/sdb), in which case libguestfs would see and be able to manipulate the partition table too.


Filed under Uncategorized

6 responses to “guestfish — not just for disk images

  1. I have been used guestfish for recovering data from nandroid backup image file.

  2. Raith

    I just want to write two or more lines to a file using guestfish, but I can’t believe that it’s so complicated. Using “write” overwrites the old file. Isn’t there anything to append to a file, like echo “one line” > /tmp/file ; echo “second line” >> /tmp/file ? I can’t even use “write “/tmp/file” “one line\nsecond line”.

    • rich

      Use this syntax:

      upload -<<END /foo
      input line 1
      input line 2
      input line 3

      Appending to an existing file is harder in guestfish. You can do it using filesize + pwrite. However once you want to make complex changes to guests it’s usually better to move on to one of the language bindings and write your script in a real language, not shell script.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.