Tip: replace text strings in a file using guestfish

virt-edit has a handy -e option that lets you do replacements on files. For example this wipes out your root password:

virt-edit domname /etc/passwd -e 's/^root:.*?:/root::/'

How can you do the same thing from guestfish or the libguestfs API?

There’s no support for this operation directly in the API, but you can download the file, use sed/perl/whatever on it locally, and upload it, and that is essentially the same thing that virt-edit is doing.

Here’s how to do that easily in guestfish:

$ guestfish --rw -i -d domname
><fs> download /etc/passwd /tmp/passwd
><fs> ! sed 's/^root:[^:]\+:/root::/' /tmp/passwd > /tmp/passwd.new
><fs> upload /tmp/passwd.new /etc/passwd

In guestfish, ! before a command runs a local command.



Filed under Uncategorized

3 responses to “Tip: replace text strings in a file using guestfish

  1. Nice thanks for this info, I’d been either mounting the disk in loop back and editing /etc/passwd && /etc/shadow _or_ booting into single user mode when having to recover a dev’s vm after a forgotten root pass reset ๐Ÿ™‚

  2. madhavan

    this tutorial is very helpful.. i tried to view the temp file using vi after using sed command and before uploading into the domain. but it said, the temp file doesn’t exist… i came out, used guestfish -a img_name to confirm, if all edits are good to go….

  3. Pingback: virt-install ใซใ‚ˆใ‚‹ไปฎๆƒณใƒžใ‚ทใƒณใฎ่‡ชๅ‹•ใ‚คใƒณใ‚นใƒˆใƒผใƒซ(ใใฎ2) | uvirt.com

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 )

Connecting to %s

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