Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've resized partitions in GParted before. Now I'd like to move a large but mostly empty partition to a smaller drive. Instead of resizing it in place, I'd prefer to make a copy on a 3rd drive (drive image using dd), resize the copy, and then dd the copy to the new drive. But I don't think Gparted operates on drive images? How would I do this? Can I use the tools GParted uses directly?

Ooh! Or can I use a loopback thing to make the image file look like a physical drive that gparted can operate on?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You can't do this with GParted, but you can use the ntfsresize command directly on a disk image.

First, clone the disk image. You can find the disk device name using blkid or gParted, and then run:

sudo dd if=<disk device> of=<image file>

For example:

$ sudo dd if=/dev/sda2 of=disk.img
1000000+0 records in
1000000+0 records out
512000000 bytes (512 MB) copied, 9.33361 s, 54.9 MB/s

Then, verify if you can resize it to the size you need by running:

ntfsresize -i <image file>

For instance:

$ ntfsresize -i disk.img
ntfsresize v2011.4.12 (libntfs-3g)
Device name        : disk.img
NTFS volume version: 3.1
Cluster size       : 4096 bytes
Current volume size: 511996416 bytes (512 MB)
Current device size: 512000000 bytes (512 MB)
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use       : 4 MB (0.6%)
Collecting resizing constraints ...
You might resize at 3035136 bytes or 4 MB (freeing 508 MB).
Please make a test run using both the -n and -s options before real resizing!

In this example, I can resize all the way down to 4MB because I just made a blank disk image for illustration purposes!

Now you can do the test run if you'd like, although it's not strictly necessary since you're operating on an image. You can specify the size in kilobytes, megabytes, or gigabytes by appending a k, M, or G, respectively:

ntfsresize -cnvs <size> <image file>

For instance:

$ ntfsresize -cnvs 256M disk.img 
ntfsresize v2011.4.12 (libntfs-3g)

No output is good output! Now do it for real:

ntfsresize -vs <size> <image file>

Last example:

$ ntfsresize -vs 256M disk.img 
ntfsresize v2011.4.12 (libntfs-3g)
Device name        : disk.img
NTFS volume version: 3.1
Cluster size       : 4096 bytes
Current volume size: 511996416 bytes (512 MB)
Current device size: 512000000 bytes (512 MB)
New volume size    : 255996416 bytes (256 MB)
Checking for bad sectors ...
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use       : 4 MB (0.6%)
Collecting resizing constraints ...
Needed relocations : 625 (3 MB)
WARNING: Every sanity check passed and only the dangerous operations left.
Make sure that important data has been backed up! Power outage or computer
crash may result major data loss!
Are you sure you want to proceed (y/[n])? y
Schedule chkdsk for NTFS consistency check at Windows boot time ...
Resetting $LogFile ... (this might take a while)
Relocating needed data ...
Relocate record       1:0x80:00000001:0x00000000:0x0000f423 --> 0x00007a11
Relocate record       2:0x80:00000624:0x00000000:0x0000f424 --> 0x00007a12
100.00 percent completed
Updating $BadClust file ...
Updating $Bitmap file ...
Updating Boot record ...
Syncing device ...
Successfully resized NTFS on device 'disk.img'.
You can go on to shrink the device for example with Linux fdisk.
IMPORTANT: When recreating the partition, make sure that you
  1)  create it at the same disk sector (use sector as the unit!)
  2)  create it with the same partition type (usually 7, HPFS/NTFS)
  3)  do not make it smaller than the new NTFS filesystem size
  4)  set the bootable flag for the partition if it existed before
Otherwise you won't be able to access NTFS or can't boot from the disk!
If you make a mistake and don't have a partition table backup then you
can recover the partition table by TestDisk or Parted's rescue mode.

That's it! Now you can copy that disk image to the new device.

share|improve this answer
    
Hmmm... the logical partition is inside an extended partition. Should I dd the logical partition itself when making the copy? –  endolith Jun 15 '11 at 18:59
    
I used resize2fs on an ext3 image file last night, and the file itself was reduced in size afterward! :) I thought that would be a separate step because the filesystem is not the same as the partition. –  endolith Jun 15 '11 at 19:04
    
@endolith: Yes, dd the logical partition. If you did the extended partition you'd get all the logical partitions inside it! –  Patches Jun 15 '11 at 22:38
    
@Patches: Well there's only one logical partition inside it, of the same size. :) –  endolith Jun 15 '11 at 22:51
    
@endolith: Still, I think dding the extended partition will bring in extra info that will confuse e2fsprogs and ntfsprogs. They don't know anything about partitions, as you discovered earlier. –  Patches Jun 15 '11 at 22:53

Could you resize it in place with GParted then use Clonezilla to move the partition to the new disk?

share|improve this answer
1  
I don't want to resize it in place. Don't want to screw up the original if I don't have to. –  endolith Jun 15 '11 at 3:08

I have been using GParted for many, many years, and never lost a bit. I also like Easus free home edition for Windows.

How large/small is your partition? Can you copy the data to USB/DVD/a free online storage system, so as not to lose it - and then resize in place? The two aforementioned tools will not lose your data if you let them run to completion.

You say I've resized partitions in GParted before - so why the worry now? And DD will make a bit for bit copy... so you will have to resize anyway (defrag first).

share|improve this answer
    
I'm converting a partition from Drive A (which I want read-only) into an image file on Drive C, resizing it, and then moving it to a partition on Drive B, which is smaller than A (so I have to resize first). –  endolith Jun 15 '11 at 19:01

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.