virt-rescue — it works on blank files too

virt-rescue fires up a temporary virtual machine. You can attach any file as a virtual disk. That might be an existing virtual machine that you need to rescue. It also works on empty files, so you can partition those files as disk images:

$ truncate -s 10G test.img
$ virt-rescue -a test.img
[boot messages omitted]
Welcome to virt-rescue, the libguestfs rescue shell.

Note: The contents of / are the rescue appliance.
You have to mount the guest's partitions under /sysroot
before you can examine them.

bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
><rescue> fdisk /dev/vda

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
Partition number (1-4, default 1): 1
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
Using default value 20971519

Command (m for help): p

Disk /dev/vda: 10.7 GB, 10737418240 bytes
16 heads, 63 sectors/track, 20805 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xfc153d75

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1            2048    20971519    10484736   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
[   79.089501]  vda: vda1
Syncing disks.

><rescue> pvcreate /dev/vda1
  Writing physical volume data to disk "/dev/vda1"
  Physical volume "/dev/vda1" successfully created

><rescue> exit

$ file test.img
test.img: x86 boot sector; partition 1: ID=0x83, 
starthead 0, startsector 2048, 20969472 sectors,
extended partition table (last)11, code offset 0x0

If you want to automate all of this, it’s much better to use guestfish.

Leave a comment

Filed under Uncategorized

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.