my tutorials‎ > ‎

Linux partition copy

I needed to copy my Linux partition to make a new one for a test-bed or distro customizer. 
So here's what I tried.

1. using dd 
-copies blocks of data -need to unmount partitions, execute from LiveCD
sudo dd if=/dev/sda5 of=/dev/sda9 bs=4096 conv=notrunc,noerror

if means input file, of means output file -so always make sure you get the right partition on 'of' or you could destroy what you wanted to copy!
get the process pid with:
ps -a | grep dd
to watch the process, 
watch -n kill -USR1 <put pid here>

dd of small partition to a larger one: the extra space on the destination will not be usable or visible, but you can shrink the partition in gparted.

2. ddrescue
shows progress, and lots is possible for recovering data from partition with errors -see manpage
sudo ddrescue -v /dev/sda5 /dev/sda9

3. cp 
-copies files and leaves extra space on destination usable, while new partiton will be nicely defragmented too.
mount both source and destination
mount -t ext3 /dev/sda5 /mnt/source
mount -t ext3 /dev/sda9 /mnt/destin
cp -a /mnt/source/* /mnt/destin

4. rsync 
works very nicely, and executes from the running system
sudo rsync -aAXv /* /mnt/destin --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*}

edit the fstab of the new partition, replace the UUID (which is copied from source partition),
create a random UUID for the new partition, in terminal:
tune2fs -U random /dev/sda9
make an entry on the Grub list to boot the new partition -I used Grub Customizer

!At this point the new system on sda9 has a GRUB configuration file that's simply been copied from sda5, so when we try to boot sda9 from the GRUB menu at startup, we will not see the sda9 Debian because its config file calls the root partition of sda5!

before rebooting, edit the /boot/grub/grub.cfg of the new partition and copy/paste the 1st UUID (of sda9) over the 2nd (which is sda5's) -to make the sda9 Debian boot from the GRUB menu
you can also edit the line in the Grub boot menu, but it's time consuming:
reboot and move down to the new entry, press E
the 2nd UUID given is of the copied partition (sda5 for me) so change it so it's the same as the new partition's random UUID (ie same as the 1st UUID shown).
then press F10 to boot

once booted up into Debian on sda9 run 
sudo update-grub