How to partclone an NTFS partition to a smaller disk without having space for the full image

0

Every now and then I have got the following problem:

A disk fails and the replacement disk is smaller. The data often easily fits the smaller disk, but partclone simply does not shrink the filesystem to match the smaller partition size.

You can simply image the whole disk onto your temporary drive then shrink. But if there is not enough space?

JPT

Posted 2018-10-13T18:04:05.507

Reputation: 151

1maybe try ntfsclone? – JPT – 2018-10-13T18:04:41.693

"A disk fails" <- there's your problem. Otherwise just shrink the partition on the source disk before copying. Then if you really want you can expand the partition on the source disk again. I would just use the Gparted live disk from https://gparted.sourceforge.io/

– BeowulfNode42 – 2018-10-27T07:21:35.993

Well, I prefer not to mess with the source drive without having a backup. But of course you could do a standard backup with partclone first. – JPT – 2018-10-29T11:12:15.997

Answers

3

Does not work yet from point 16 on. Maybe because there was a stale loop device I did not delete.

partclone raw dump into sparse image

This solution is dangerous to your data. Only try if you understand what you are doing and are familiar with dd and devices. I won't be responsible for any data loss.

Before we start:

  • Do not use this procedure to mirror a disk that is physically damaged because

    • partclone needs a healthy filesystem to work on

    • this procedure is far more complex than doing dd so you probably have to repeat it several times until you succeed - bringing more strain on the broken disk

    • for broken disks you should use safecopy instead because dd aborts on bad sectors

  • You need up to 2 x data-size free space on your temporary drive.

  • I will enter sdXX for any command that might destroy data if you forgot to customize the command. This does not deal with loops, so be sure to NOT use any other loop devices during the following process to avoid accidentally overriding valuable data.

  • If you fail somewhere and delete the image, be sure to first unmount anything pointing to it, then remove the loop devices: kpartx -dv <image>. It may cause major problems if you use wrong loop devices! I believe my problem is caused by a stale loop device...

  • Find fixes for common problems at the end of this answer.

Let's go

  1. get the disk size of your source disk

    sudo fdisk -l /dev/sda
    output: Disk /dev/sda: 465,8 GiB, 500107862016 bytes, 976773168 sectors
    
  2. read the original partition layout into image

    sudo dd if=/dev/sda of=sparse.img bs=1M count=1
    
  3. fill the image with "nothing" up to the above size. - This will not take any space on disk.

    sudo dd if=/dev/zero of=sparse.img bs=1 count=0 seek=500107862016
    
  4. check there are no other loop-devices active - if the output is not empty, see trouble shooting section

    losetup -l -a 
    
  5. check if partition layout is the same as source drive

    fdisk -l sparse.img
    
  6. create loop devices for access of the partitions in the image

    sudo kpartx -av sparse.img 
    output: add map loop0p1 (253:0): 0 976771072 linear 7:2 2048
    
  7. read the data into the image using partclone - this will increase space usage by "data size" plus "overhead".

    sudo partclone.ntfs -s /dev/sda1 -o /dev/mapper/loop0p1 -b -L sparse.log
    
  8. remove source drive to keep it save

  9. shrink using gparted - should be at least 10% smaller than target drive

    • create links because gparted does not understand the devs kpartx created.

      ln -s /dev/mapper/loop0p1 sparse.img1
      
    • use gparted to shrink the partition

      sudo gparted sparse.img
      
  10. alternatively you can shrink using ntfsresize but you have to shrink the partition, too afterwards.

    • get min size

      sudo ntfsresize -i /dev/mapper/loop0p1
      
    • test resize

      sudo ntfsresize -n -s 100G /dev/mapper/loop0p1
      
    • real resize - this will increase space usage by up to "data size"

      sudo ntfsresize -s 100G /dev/mapper/loop0p1
      
    • TODO: shrink partition

  11. (unnecessary because shrinked not enlarged?) delete and recreate the loop devices because partition size changed

    sudo kpartx -dv sparse.img 
    sudo kpartx -av sparse.img 
    
  12. (optional) TODO truncate image

  13. check using ntfsresize - for me this gives an OK answer

    sudo ntfsresize -i /dev/mapper/loop0p1
    
  14. insert target drive - double check you inserted the correct drive

  15. (does not work because ntfsresize marked the fs dirty) partclone the image to the new drive

    sudo partclone.ntfs -o /dev/sdXX -s /dev/mapper/loop0p1 -b -L restore.log
    
  16. instead dd the image to the new drive - either do not give a size or be 10% over the shrinked partition size

    sudo dd if=sparse.img of=/dev/sdXX
    
  17. check using ntfsresize - FAILS for me, this should not happen as the image was OK and we created an identical copy of the image!!!

    sudo ntfsresize -i /dev/sda1
    
  18. delete loop devices

    sudo kpartx -dv sparse.img
    
  19. delete any loop devices that were not cleanly removed - if still some persist, either reboot or search the web on this.

    sudo losetup -D 
    
  20. Finally you should try booting the disk then run Windows chkdsk / f. After that use gparted to expand the partition to it's full size.

Troubleshooting:

  • If ntfsresize or gparted cannot access the image: exec chkdsk /f from windows on the source disk. Do NOT use ntfsfix. Any NTFS checker for Linux?

  • If kpartx cannot create the loop devices: check with losetup and delete unused ones.

  • If you want to get rid of obsolete loop devices delete with sudo losetup -d or search the web.

JPT

Posted 2018-10-13T18:04:05.507

Reputation: 151

With ubuntu, I was able to create the image with losetup -P -v /dev/loop0 sparse.img mkfs.ext4 /dev/loop0p1... and gparted /dev/loop0 worked! – kevinf – 2019-09-30T15:37:29.893