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.
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 ๐
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….
Pingback: virt-install ใซใใไปฎๆณใใทใณใฎ่ชๅใคใณในใใผใซ(ใใฎ2) | uvirt.com