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?
31 Answer
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
partcloneneeds a healthy filesystem to work onthis procedure is far more complex than doing
ddso you probably have to repeat it several times until you succeed - bringing more strain on the broken diskfor broken disks you should use
safecopyinstead becauseddaborts on bad sectors
You need up to 2 x data-size free space on your temporary drive.
I will enter
sdXXfor 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
get the disk size of your source disk
sudo fdisk -l /dev/sda output: Disk /dev/sda: 465,8 GiB, 500107862016 bytes, 976773168 sectorsread the original partition layout into image
sudo dd if=/dev/sda of=sparse.img bs=1M count=1fill 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=500107862016check there are no other loop-devices active - if the output is not empty, see trouble shooting section
losetup -l -acheck if partition layout is the same as source drive
fdisk -l sparse.imgcreate 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 2048read 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.logremove source drive to keep it save
shrink using
gparted- should be at least 10% smaller than target drivecreate links because
gparteddoes not understand the devskpartxcreated.ln -s /dev/mapper/loop0p1 sparse.img1use
gpartedto shrink the partitionsudo gparted sparse.img
alternatively you can shrink using
ntfsresizebut you have to shrink the partition, too afterwards.get min size
sudo ntfsresize -i /dev/mapper/loop0p1test resize
sudo ntfsresize -n -s 100G /dev/mapper/loop0p1real resize - this will increase space usage by up to "data size"
sudo ntfsresize -s 100G /dev/mapper/loop0p1TODO: shrink partition
(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(optional) TODO truncate image
check using
ntfsresize- for me this gives an OK answersudo ntfsresize -i /dev/mapper/loop0p1insert target drive - double check you inserted the correct drive
(does not work because
ntfsresizemarked the fs dirty)partclonethe image to the new drivesudo partclone.ntfs -o /dev/sdXX -s /dev/mapper/loop0p1 -b -L restore.loginstead
ddthe image to the new drive - either do not give a size or be 10% over the shrinked partition sizesudo dd if=sparse.img of=/dev/sdXXcheck 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/sda1delete loop devices
sudo kpartx -dv sparse.imgdelete any loop devices that were not cleanly removed - if still some persist, either reboot or search the web on this.
sudo losetup -DFinally you should try booting the disk then run Windows
chkdsk / f. After that usegpartedto expand the partition to it's full size.
Troubleshooting:
If
ntfsresizeorgpartedcannot access the image: execchkdsk /ffrom windows on the source disk. Do NOT usentfsfix. Any NTFS checker for Linux?If
kpartxcannot create the loop devices: check withlosetupand delete unused ones.If you want to get rid of obsolete loop devices delete with
sudo losetup -dor search the web.