-   Linux - Software (
-   -   Resize NTFS img file (

nandelbosc 02-17-2010 03:42 AM

Resize NTFS img file
Hi gurus!

I was cloned an entire disk (with one NTFS partition of 70GB, only 15GB used) using ddrescue with the following command...


ddrescue --no-split /dev/sda /media/usb250/ntfs.img /media/usb250/ntfs.log
The img file it's around 70GB. Now I want to restore it to another computer with 30GB of space. How can I reduce the size of img file?

If somebody can halp me, I will be a little bit more happy ;)

irmin 02-17-2010 05:10 AM

Maybe the ntfsresize(8) command is the right one for you.

nandelbosc 02-17-2010 06:06 AM

Thank's irmin, I tried ntfsresize before, but don't work with an entire disk image...


$ /media/DISK/$ ntfsresize -i sapbes.img
ntfsresize v2.0.0 (libntfs 10:0:0) Failed to startup volume: Invalid argument.
ERROR(22): Opening 'sapbes.img' as NTFS failed: Invalid argument The device 'sapbes.img' doesn't have a valid NTFS.
Maybe you selected the wrong partition? Or the whole disk instead of a partition (e.g. /dev/hda, not /dev/hda1)? This error might also occur if the disk was incorrectly repartitioned (see the ntfsresize FAQ).

Do you have another idea?

For now, I'm creating a new image, only with one partition...


ddrescue --no-split /dev/sda1 /media/usb250/ntfspart1.img /media/usb250/ntfs.log
After I try with ntfsresive. I will comment on changes.

irmin 02-17-2010 06:14 AM

From ntfsresize(8):

When recreating the partition by a disk partitioning tool, make sure
you create it at the same starting sector and with the same partition
type as before. Otherwise you won't be able to access your filesystem.
Use the 'u' fdisk command to switch to the reliable sector unit from
the default cylinder one.

Also make sure you set the bootable flag for the partition if it
existed before. Failing to do so you might not be able to boot your
computer from the disk.
You cannot apply ntfsresize to a whole harddisk, but only to partitions. So you took the right steps ...

tredegar 02-17-2010 01:13 PM

In post #1 you have copied a whole HDD as a whole disk image, not just the relevant partition as a partition image.

You need to get the partition out of the whole disk image, and then mount that with the -o loop option.

Fortunately, this is remarkably easy to do with linux. See this thread and read it all the way to the end before you start experimenting.

If you have created a new image of just the partition, then mount it (all as root):


modprobe  ntfs-3g
mkdir /mnt/olddisk
mount  -t  ntfs  -o  loop  /path/to/PartitionImage.iso  /mnt/olddisk

Once you have mounted the "partition" (it is really a file, but now you can treat it as a disk), you can then just copy the files to the new disk's formatted partition.

Don't forget to ummount everything before you start unplugging things.

nandelbosc 02-18-2010 03:33 AM

Ok, the command ntfsresize finish ok! ...

$ ntfsresize -s 28G sapbes.img
ntfsresize v2.0.0 (libntfs 10:0:0)
Device name        : sapbes.img
NTFS volume version: 3.1
Cluster size      : 4096 bytes
Current volume size: 73394139648 bytes (73395 MB)
Current device size: 73394141184 bytes (73395 MB)
New volume size    : 27999994368 bytes (28000 MB)
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use      : 17519 MB (23.9%)
Collecting resizing constraints ...
Needed relocations : 1206303 (4942 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 ...
100.00 percent completed
Updating $BadClust file ...
Updating $Bitmap file ...
Updating Boot record ...
Syncing device ...
Successfully resized NTFS on device 'sapbes.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.

But the file size it's the same... (69GB instead of 28GB)

$ ls -lh
-rwxrwxrwx 1 ubuntu ubuntu 512 2010-02-18 08:42 mbr.img
-rwxrwxrwx 1 ubuntu ubuntu 69G 2010-02-18 08:04 sapbes.img

Where is the problem?


nandelbosc 02-18-2010 03:42 AM

Wait a moment... if I mount the new file...


$ mount -o loop sapbes.img /mnt/temp
and look for the free space...


$ df -h
/dev/loop1          27GB    17GB    9.8G  63%  /mnt

the partition size is 27GB... ok, that sounds good!

Now I try to restore the img file to a new (little) disk.

I will comment on changes ;)

nandelbosc 02-18-2010 07:07 AM

No luck,

after a couple of hours, i get this error...


$ dd if=./mbr.img of=/dev/sda
0+1 records in
0+1 records out
512 bytes (512 B) copied, 0,0250117 s, 17,8 kB/s

$ dd if=./sapbes.img of=/dev/sda1
32 GB Copied. No space left on device

And windows 2003 server don't boot...


*** STOP: 0x0000007B (0xF789A63C,0xC0000034,0x00000000,0x00000000)
the 7B stop error is due to an inaccessible boot device. It probably means that the computer cannot locate the system partition.

Now, as tredegar said, I'm trying to mount sapbes.img, and copy files using 'cp' command instead of dd.

Again, I will comment on changes.

tredegar 02-18-2010 11:33 AM


But the file size it's the same... (69GB instead of 28GB)
You have resized the filesystem (to the beginning of the partition) but the partition size is the same.
So there is empty space after the filesystem on that partition.

From man ntfsresize

      If you wish to shrink an NTFS partition, first use ntfsresize to shrink
      the  size  of the filesystem. Then you could use fdisk(8) to shrink the
      size of the partition by deleting the partition and recreating it  with
      the  smaller size.  Do not make the partition smaller than the new size
      of NTFS otherwise you won’t be able to boot. If  you  did  so  notwith‐
      standing then just recreate the partition to be as large as NTFS.

So you need to unmount your loop-mounted partition, and then use fdisk to repartition it to
a size that will hold your resized filesystem.

Easier, I think, is just to copy the files to a new filesystem on a partition of the right size on the disk
of your choice.

nandelbosc 02-18-2010 11:56 AM

Ok, I tried to copy the files to a new system with the same result :(

I think it's a problem with the Windows 2003 and the disk controller drivers.

Now I try to explain why I'm doing this ...

We had an old server with Windows 2003. This server had a hard disk that began to fail and we cannot copy in any way (Veritas BackupExec, ntbackup, ...). To demostrate the versatility of Linux to my work colleagues I wanted to pass the old system of this machine to a host within a VMware ESX Server using Ubuntu LiveCD, but as you saw, does not boot. I think the installation of windows can not support this beasty hardware change (from an old Asus server to a VM ESX virtual host).

Tomorrow I try to restore it in another (phisical) machine with a similar hard as the initial I can.

Thank's for you support!

tredegar 02-18-2010 12:09 PM


Now I try to explain why I'm doing this ...
Thanks for the explanation.

What you are trying to do is "Copy Windows".

Unlike linux, win does not like to be "copied". It likes to be "reinstalled" from the original medium with the "product activation codes" etc.

We are not supposed to help you copy windows, even if it is for an understandable reason.
( Check the LQrules. )

nandelbosc 02-18-2010 12:16 PM


Originally Posted by tredegar (Post 3868317)
We are not supposed to help you copy windows, even if it is for an understandable reason.
( Check the LQrules. )

Ok, I understand and accept the forum rules... not the windows rules! ;)

anyway, thank's again for your support!

tredegar 02-18-2010 12:32 PM


anyway, thank's again for your support!
That's OK.
At least you learnt something, and can now rescue your files, if not win.

nandelbosc 02-18-2010 12:43 PM

Exactly, and as a last option, we can just buy a hard drive and return to the old Asus server with the recovered files.

All times are GMT -5. The time now is 11:36 AM.