Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a raw QEMU image (vda.raw), and I would like to resize it and add an existing partition, using data I have in a file that contains a raw ext3 file system populated with data (vdb.raw). The two files look like this:

$ file vda.raw
vda.raw: x86 boot sector; partition 1: ID=0x83, active, starthead 0, startsector 16065, 20948760 sectors, code offset 0x63
$ file vdb.raw
vdb.raw: Linux rev 1.0 ext3 filesystem data, UUID=14555b9c-4837-4b43-a8e8-fe4e19194e88, volume name "ephemeral0" (large files)

Is there a simple way I can create a new image by combining these two: where the contents of vdb.raw appear as a new disk partition in vda.raw? I'd like to avoid having to:

  1. Resize vda.raw
  2. Create a new partition in vda.raw
  3. Mount vda.raw
  4. Mount vdb.raw
  5. Copy the contents of vdb.raw into vda.raw

Is there a way I could, say, concatenate them with dd, and then fix the partition table to make it aware that there's a new partition in the image?

share|improve this question
    
So, did this help out at all? –  larsks Feb 29 '12 at 1:39
add comment

1 Answer

Have you tried the obvious -- simply concatenating vdb to vda and creating a new partition? I tried that and it seems to work. Here's what I did...

I started with two files:

# ls -l vd*
-rw-r--r-- 1 root root 104857600 Feb 27 20:31 vda.raw
-rw-r--r-- 1 root root 104857600 Feb 27 20:31 vdb.raw

# file vd[ab].raw
vda.raw: x86 boot sector; partition 1: ID=0x83, starthead 32, startsector 2048, 202752 sectors, extended partition table (last)\011, code offset 0x0
vdb.raw: Linux rev 1.0 ext3 filesystem data, UUID=734fa0ee-0bc8-4428-a13c-3147c9c0866f

The file vda.raw contains a standard partition map with a single partition (containing an ext3 filesystme):

# fdisk -l vda.raw 

Disk vda.raw: 104 MB, 104857600 bytes
191 heads, 50 sectors/track, 21 cylinders, total 204800 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: 0xad0a5dfe

  Device Boot      Start         End      Blocks   Id  System
vda.raw1            2048      204799      101376   83  Linux

vdb.raw contains an ext3 filesystem.

First, we concatenate these two files together:

# cat vda.raw vdb.raw > combined.raw

Next, we create a new partition that covers the new data:

# fdisk combined.raw

Here's the initial partition table:

Command (m for help): p

Disk combined.raw: 209 MB, 209715200 bytes
191 heads, 50 sectors/track, 42 cylinders, total 409600 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: 0xad0a5dfe

       Device Boot      Start         End      Blocks   Id  System
combined.raw1            2048      204799      101376   83  Linux

Now we create a new one, accepting the defaults for start sector and size:

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (204800-409599, default 204800): 
Using default value 204800
Last sector, +sectors or +size{K,M,G} (204800-409599, default 409599): 
Using default value 409599

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

Syncing disks.

Now let's see what we've got:

# kpartx -a combined.raw 
# mkdir /mnt/{1,2}
# mount /dev/mapper/loop1p1 /mnt/1
# mount /dev/mapper/loop1p2 /mnt/2
# ls /mnt/1
lost+found  README.vda
# ls /mnt/2
lost+found  README.vdb
# 

Ta-da!

share|improve this answer
    
I tried this, but fdisk hung when I tried to save the partition table (although I think that may have been unrelated, see 32921), but I wasn't able to mount the resulting combined image. So I just resized the first image, mounted both, and copied the files instead. –  Lorin Hochstein Feb 29 '12 at 3:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.