You are not logged in.

#1 2011-09-01 07:53:38

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

[Solved] How to repartition a disk image that is not a physical disk.

gparted is a very good GUI to repartition a disk. But it does not work (to my knowledge) on a disk image only on a physical disk. My question is is there a equivalent that works on a disk image? Or is there any possibility to simulate a disk device from a disk image (I tried losetup, but the kernel do not see the partition table of this device, which is needed for gparted). Asa last ressort is there a command line utility to resize a FAT file system (for every other filesystem that I know: ext/{2,3,4}, ntfs, I know that such utilities exists and I can do what I want with these and fdisk).

Basically, what I am trying to do is to repartition a vmware hard disk (vmware has a tool to show the disk as a flat image). As a last ressort I can run gparted from the virtual machine that I boot from a (virtual) CD. But it is more efficient if I could do it directly.

Last edited by olive (2011-09-01 14:16:16)

Offline

#2 2011-09-01 09:04:30

skodabenz
Banned
From: Tamilnadu, India
Registered: 2010-04-11
Posts: 382

Re: [Solved] How to repartition a disk image that is not a physical disk.

Try

sudo gparted <path_to_disk_image>

But for this the disk image should be a raw dump (dd copy) of the disk, not any of the virtual disk formats.


My new forum user/nick name is "the.ridikulus.rat" .

Offline

#3 2011-09-01 09:22:39

jakobm
Member
Registered: 2008-03-24
Posts: 132

Re: [Solved] How to repartition a disk image that is not a physical disk.

olive wrote:

I tried losetup, but the kernel do not see the partition table of this device, which is needed for gparted

Try kpartx from community/multipath-tools. It creates device maps from files/loop devices.
To setup the maps:

kpartx -a disk-image

To delete them:

kpartx -d disk-image

The device nodes are created as /dev/loopN and /dev/mapper/loopNpM.

Offline

#4 2011-09-01 10:01:49

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Re: [Solved] How to repartition a disk image that is not a physical disk.

jakobm wrote:
olive wrote:

I tried losetup, but the kernel do not see the partition table of this device, which is needed for gparted

Try kpartx from community/multipath-tools. It creates device maps from files/loop devices.
To setup the maps:

kpartx -a disk-image

To delete them:

kpartx -d disk-image

The device nodes are created as /dev/loopN and /dev/mapper/loopNpM.

This is apparently not sufficient for gparted (it gives errors when repartitioning). Apparently gparted want a real device and want to be able to call rereadpt on it. It also expect to see the partition of the forms (for the devices /dev/sda) /dev/sda1 /dev/sda2, ... . I have tried to make hard links of the forms /dev/sdf /dev/sdf1, etc...  of the device created. But if you repartition and gparted want to create a new partition it expects the kernel to create /dev/sdf2 after forcing the kernel to reread partition table. This make gparted unreliable in this situation. To use gparted the kernel should really see a disk device with the ability to read itself the partition table on it with rereadapt and it should create the partitions on the format /dev/sda1, /dev/sda2, etc...

Now the only tool I want to have is a tool to resize a fat filesystem. Then I can simply leave gparted and do everything by hand with fdisk.

@skodabenz As I said gparted do not work. I know that the disk should be a raw dump but vmware has the ability to create a (virtual) file representing the raw dump of the disk (vmware-mount -f ). Obviously I first have tried with a real file to test the situation without risk.

Last edited by olive (2011-09-01 10:08:59)

Offline

#5 2011-09-01 13:35:14

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 723
Website

Re: [Solved] How to repartition a disk image that is not a physical disk.

If you want partitions on loopback devices just load loop module with max_part=N

Offline

#6 2011-09-01 13:41:46

George.Harmony
Member
From: Off The Wire
Registered: 2010-04-30
Posts: 97

Re: [Solved] How to repartition a disk image that is not a physical disk.

You can do this using a raw img of a disk. I don't know if it will work with a vhd or if you can convert a vhd to a .img

Using  gdisk (part of the gptdisk package) and dd to add space and fix partition tables

First open your image with gdisk. I'm using a ChromiumOS image. and print the partition table

gdisk ChromeOS-Vanilla-0.14.805.2011_07_31_1705-r54807a91.img 
GPT fdisk (gdisk) version 0.7.2

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk ChromeOS-Vanilla-0.14.805.2011_07_31_1705-r54807a91.img: 4005923 sectors, 1.9 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F07CBA0D-1738-3C42-9B1B-92D7A2E76277
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 4005889
Partitions will be aligned on 1-sector boundaries
Total free space is 4059 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1         1810432         3907583   1024.0 MiB  0700  STATE
   2           20480           53247   16.0 MiB    7F00  KERN-A
   3           53248         1810431   858.0 MiB   7F01  ROOT-A
   4         3973120         4005887   16.0 MiB    7F00  KERN-B
   5         4005888         4005888   512 bytes   7F01  ROOT-B
   6           16448           16448   512 bytes   7F00  KERN-C
   7           16449           16449   512 bytes   7F01  ROOT-C
   8         3907584         3940351   16.0 MiB    0700  OEM
   9           16450           16450   512 bytes   7F02  reserved
  10           16451           16451   512 bytes   7F02  reserved
  11              64           16447   8.0 MiB     FFFF  RWFW
  12         3940352         3973119   16.0 MiB    EF00  EFI-SYSTEM

We need to know the block size and  the number of sectors of the image

Logical sector size: 512 bytes

and

4005923 sectors

next we will dd some zeros at the end of the drive.

dd if=/dev/zero of=ChromeOS-Vanilla-0.14.805.2011_07_31_1705-r54807a91.img seek=4005889 count=4005889 bs=512
4005889+0 records in
4005889+0 records out
2051015168 bytes (2.1 GB) copied, 39.9097 s, 51.4 MB/s

Here i am effectively doubling the size of my image as you can see from my ls output

  ls -la
total 4005904
drwxr-xr-x 2 owner users       4096 Sep  1 10:17 .
drwxr-xr-x 3 owner users       4096 Sep  1 10:05 ..
-rw-r--r-- 1 owner users 4102030336 Sep  1 10:22 ChromeOS-Vanilla-0.14.805.2011_07_31_1705-r54807a91.img

Now i need to fix the partition tables with gdisk

 gdisk ChromeOS-Vanilla-0.14.805.2011_07_31_1705-r54807a91.img 
GPT fdisk (gdisk) version 0.7.2

Caution: invalid backup GPT header, but valid main header; regenerating
backup header from main header.

Warning! One or more CRCs don't match. You should repair the disk!

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: damaged

****************************************************************************
Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk
verification and recovery are STRONGLY recommended.
****************************************************************************

As you can see there are some problems. Let's verify the disk to see what they are.

Command (? for help): v

Caution: The CRC for the backup partition table is invalid. This table may
be corrupt. This program will automatically create a new backup partition
table when you save your partitions.

Problem: The secondary header's self-pointer indicates that it doesn't reside
at the end of the disk. If you've added a disk to a RAID array, use the 'e'
option on the experts' menu to adjust the secondary header's and partition
table's locations.

Identified 2 problems!

and it tell us how to fix them

Command (? for help): x

Expert command (? for help): e
Relocating backup data structures to the end of the disk

Expert command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT).
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.

The writing fixes the first error about crc and the second error is fixed by relocating the secondary header to the end of the drive.

Now you can open the img back up and partition the space as required.

gdisk ChromeOS-Vanilla-0.14.805.2011_07_31_1705-r54807a91.img 
GPT fdisk (gdisk) version 0.7.2

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk ChromeOS-Vanilla-0.14.805.2011_07_31_1705-r54807a91.img: 8011778 sectors, 3.8 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F07CBA0D-1738-3C42-9B1B-92D7A2E76277
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 8011744
Partitions will be aligned on 1-sector boundaries
Total free space is 4009914 sectors (1.9 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1         1810432         3907583   1024.0 MiB  0700  STATE
   2           20480           53247   16.0 MiB    7F00  KERN-A
   3           53248         1810431   858.0 MiB   7F01  ROOT-A
   4         3973120         4005887   16.0 MiB    7F00  KERN-B
   5         4005888         4005888   512 bytes   7F01  ROOT-B
   6           16448           16448   512 bytes   7F00  KERN-C
   7           16449           16449   512 bytes   7F01  ROOT-C
   8         3907584         3940351   16.0 MiB    0700  OEM
   9           16450           16450   512 bytes   7F02  reserved
  10           16451           16451   512 bytes   7F02  reserved
  11              64           16447   8.0 MiB     FFFF  RWFW
  12         3940352         3973119   16.0 MiB    EF00  EFI-SYSTEM

Command (? for help): n
Partition number (13-128, default 13): 
First sector (34-8011744, default = 4005889) or {+-}size{KMGTP}: 
Last sector (4005889-8011744, default = 8011744) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk ChromeOS-Vanilla-0.14.805.2011_07_31_1705-r54807a91.img: 8011778 sectors, 3.8 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): F07CBA0D-1738-3C42-9B1B-92D7A2E76277
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 8011744
Partitions will be aligned on 1-sector boundaries
Total free space is 4058 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1         1810432         3907583   1024.0 MiB  0700  STATE
   2           20480           53247   16.0 MiB    7F00  KERN-A
   3           53248         1810431   858.0 MiB   7F01  ROOT-A
   4         3973120         4005887   16.0 MiB    7F00  KERN-B
   5         4005888         4005888   512 bytes   7F01  ROOT-B
   6           16448           16448   512 bytes   7F00  KERN-C
   7           16449           16449   512 bytes   7F01  ROOT-C
   8         3907584         3940351   16.0 MiB    0700  OEM
   9           16450           16450   512 bytes   7F02  reserved
  10           16451           16451   512 bytes   7F02  reserved
  11              64           16447   8.0 MiB     FFFF  RWFW
  12         3940352         3973119   16.0 MiB    EF00  EFI-SYSTEM
  13         4005889         8011744   1.9 GiB     8300  Linux filesystem

Hope this helps


Desktop:  Compiz Stand Alone w/ Cairo Dock.
Laptop:    Pekwm w/ Tint2
Jukebox:   MPD w/ cli
Gateway: Vuurmuur w/dialog

Offline

#7 2011-09-01 14:15:25

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Re: [Solved] How to repartition a disk image that is not a physical disk.

djgera wrote:

If you want partitions on loopback devices just load loop module with max_part=N

That's interesting thank you. However this is even not sufficient to make gparted happy (it fails 1 times on two), but I give up with gparted, it won't get happy unless I give it a real physical disk -:( ; moreover even on a real physical disk gparted is not able to resize fat filesystem. But I think your answer go as far as it is possible with my initial question. I mark this thread as solved and I open a new one just to know if it is possible to resize a FAT filesystem on linux.

Last edited by olive (2011-09-01 14:17:00)

Offline

#8 2011-09-01 14:50:48

skodabenz
Banned
From: Tamilnadu, India
Registered: 2010-04-11
Posts: 382

Re: [Solved] How to repartition a disk image that is not a physical disk.

olive wrote:
djgera wrote:

If you want partitions on loopback devices just load loop module with max_part=N

That's interesting thank you. However this is even not sufficient to make gparted happy (it fails 1 times on two), but I give up with gparted, it won't get happy unless I give it a real physical disk -:( ; moreover even on a real physical disk gparted is not able to resize fat filesystem. But I think your answer go as far as it is possible with my initial question. I mark this thread as solved and I open a new one just to know if it is possible to resize a FAT filesystem on linux.

Thats because GNU libparted 3.0 removed all FS support. Due to this gparted does not support resizing FAT and HFS partitions since there are no standalone tools (like ntfsresize) for FAT and HFS filesystems that gparted can use.


My new forum user/nick name is "the.ridikulus.rat" .

Offline

#9 2011-09-01 15:23:57

George.Harmony
Member
From: Off The Wire
Registered: 2010-04-30
Posts: 97

Re: [Solved] How to repartition a disk image that is not a physical disk.

As usual I missed the part about resizing the fat partition. I believe you could use dd to do this too.

Simply extract the partition using dd with the skip, count and bs options.
Add the zeros as in my previous post to the partition and to the image.
Delete the fat partition in the image and create a new one of desired size, move everything that isn't a fat partition around to make room for it.
Then dd the saved partition back to the empty one you created.

Something like that should work. I'm not a hundred percent sure if you even need to write the zero's to the end of the partition you extracted.


Desktop:  Compiz Stand Alone w/ Cairo Dock.
Laptop:    Pekwm w/ Tint2
Jukebox:   MPD w/ cli
Gateway: Vuurmuur w/dialog

Offline

#10 2011-09-01 16:59:44

olive
Member
From: Belgium
Registered: 2008-06-22
Posts: 1,490

Re: [Solved] How to repartition a disk image that is not a physical disk.

@George.Harmony That will not work because you need to resize the filesystem and that need specific tools. Apparently parted can do it up to version 2.4. I just have "upgraded" parted from 3.0.3 to version 2.4-1 and I was able to do it.

By the way parted 3.0-3 is the most ridiculous new version of a softawre I have found. The "new features" apparently are the removal of the file system code including the only code that can resize a FAT partition on Linux!. Fantastic new version indeed...

Offline

Board footer

Powered by FluxBB