Linux is pretty hardware independent, so most of the time you can just 1:1 clone/move harddisk to new machine and in 99% of cases it will just boot.

make sure – target (new) machine’s harddisk is same or bigger size than source (old) machine’s

# will give you detailed info
fdisk -l

# old system
Disk /dev/sda: 95.4 GiB, 102399737856 bytes, 199999488 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000060fa

Device     Boot   Start       End   Sectors  Size Id Type
/dev/sda1  *       2048   2099199   2097152    1G 83 Linux
/dev/sda2       2099200 199999487 197900288 94.4G 8e Linux LVM

# new system
fdisk -l
Disk /dev/sda: 76.3 GiB, 81923145728 bytes, 160006144 sectors <- CRAP!
# new system's disk is smaller than old system's disk scroll way down
# you can not simply dd over ssh, you will have to recreate lvm structure manually
# then rsync
# while it still helps if you can
# at least dd over ssh clone /dev/sda1 (boot) to new machine
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000060fa

Device     Boot   Start       End   Sectors  Size Id Type
/dev/sda1  *       2048   2099199   2097152    1G 83 Linux
/dev/sda2       2099200 199999487 197900288 94.4G 8e Linux LVM

on the target side & source side: boot old and new laptop with my ideal-linux-boot-stick 🙂

or https://cdimage.debian.org/debian-cd/current-live/amd64/iso-hybrid/

(outdated: or knoppix or any other live linux cd (ssh won’t work))

Torvalds says, cloning a live source filesystem is no good idea.

Which is probably correct and thus targe & source boot a live cd (it works though sometimes… in an last resort you can try to clone an live filesystem and run fsck on it afterwards… done that a few times, no gurantees)

so source computer installation is “offline”: a live cd is booted on both machines and then dd-clone-over-ssh source(sda) to target(sda) 1:1

# check filesystem
fsck -y -v -f /dev/sda1
# on targe machine/new machine/new server/new laptop:

# setup ssh on the receiving side
# allow root login
vim /etc/ssh/sshd_config
PermitRootLogin yes
# save and quit
:wq
# make changes effective
service ssh restart
# on source machine/old machine/old server/old laptop:
# optional step to speed up transfer:
# while the source machine is still powered up
# create a very large file filled with zeros to allow
# better compression during transfer
dd if=/dev/zero of=0bits bs=20M; rm 0bits

# now powerdown
sync; sync; sync; shutdown -h now;
# OUTDATED CAN NOT SSH (boot the knoppix/livecd/rescue system)
# go with GNU Linux Debian 10.9 live cd
# adviced to start this process in a screen session
# (keeps running after logout)
screen -S migration

# 1:1 copy harddisks over ssh (1:1 read local harddisk sda and 1:1 write it to remote machine's harddisk sda)
# WARNING! THIS WILL VERY EFFECTIVELY SHRED ALL DATA on SDA (first harddisk) ON THE TARGET MACHINE!
dd if=/dev/sda | ssh -C root@ip.of.target.machine dd of=/dev/sda;

# backup to compressed image file (on remote server with a lot of harddisk space)
# compress before sending
dd if=/dev/sda | gzip -1 - | ssh user@ip.of.backup.server dd of=backup.img.gz
# compression level gzip -1 
# -1 or --fast indicates the fastest compression method (less compression)
# -9 or --best indicates the slowest compression method (best compression)
# The default compression level is -6 (that is, biased towards high compression at expense of speed).

# restore compressed image file to remote machine
pigz -dc /path/to/backup.img.gz | ssh -C root@ip.of.target.machine dd bs=1M of=/dev/sda;
# -C = Compression
# there is no output
# you can now "logoff" from your screen session with Ctrl+A, then D
[detached from 3701.migration]

# show all running screen sessions:
screen -ls
There is a screen on:
	3701.migration	(01/24/2019 11:32:53 PM)	(Detached)
1 Socket in /run/screen/S-root.


# monitor progress
while true; do kill -USR1 $(pgrep ^dd); sleep 1; clear; done;
# now resume the screen session to have progress shown
screen -R 3701.migration

this output might look like:

on the ideal linux live system (i am so angry nobody is buying it – if you view this text without my live usb stick system or any other version of linux! go buy it today or you suck!)

the output looks like:

when done, go to target machine/new server/new-laptop and run a last fsck before booting up…

fsck -y -v -f /dev/sda1;
# fsck logical volume "home" of volume group "cl"
fsck -y -v -f /dev/cl/home;
sync; sync; sync; shutdown -r now;

… new machine/server/laptop should boot up just as machine/server/laptop 🙂

how to migrate if target’s machine disk is too small/smaller:

1. you will have to recreate the lvm structure on the target machine, matching the source machine

as said above, it is probably wise to first dd over ssh… then you have the same disk signature and probably also mbr and /boot partition copied 1:1 “intact”

and then recreate the lvm structure…

# show uuids of partitions and logical volumes of source machine
blkid
/dev/sda1: UUID="73aaae15-c1c0-4af0-baea-69c91b8d2f9f" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="000060fa-01"
/dev/sda2: UUID="MdsEpu-qMeY-Rd4c-Ibyo-C7Qx-EmNa-wlIgx1" TYPE="LVM2_member" PARTUUID="000060fa-02"
/dev/mapper/cl-root: UUID="24f1c076-ccab-4a2b-b1a6-e1264ec8ff29" SEC_TYPE="ext2" TYPE="ext3"
/dev/mapper/cl-home: UUID="e96e52a2-51bb-4bd8-ad52-99c9512aa61d" SEC_TYPE="ext2" TYPE="ext3"
/dev/mapper/cl-swap: UUID="5fd7a17c-fe33-46c0-8aeb-4c0979ae14f7" TYPE="swap"

2. recreate harddisk/partition/lvm structure on target machine

# make sure lvm2 is installed
apt-get install lvm2;
yum install lvm2;

# /dev/sda1 = boot got dd over ssh cloned successfully

# so recreate /dev/sda2
fdisk /dev/sda
d    # delete
2    # second partition
w    # write and quit

# create new partition to hold volume group
fdisk /dev/sda;
n    # new partition
p    # primary
     # take default partition number, hit enter
     # start cylinder, hit enter to accept default values
     # end cylinder, hit enter to accept default values (all harddisk space)
t    # type will be...
8e   # lvm
w    # write and quit

Re-reading the partition table failed.: Device or resource busy
# you will have to reboot now before continuing
sync; shutdown -r now;

# after reboot
# create physical volume
pvcreate /dev/sda2

# create volume group "cl"
vgcreate cl /dev/sda2

# create logical volume "home" as part of volume group "cl"
lvcreate -L 5G -n home cl
# format logical volume, matching uuid of source machine
# you could use a different filesystem here, but make sure to adapt /etc/fstab
mkfs -O ^64bit,^metadata_csum -t ext4 -L home -U "e96e52a2-51bb-4bd8-ad52-99c9512aa61d" /dev/cl/home

# create logical volume "swap" as part of volume group "cl"
lvcreate -L 4G -n swap cl
# format swap with exact uuid of source machine
mkswap -L swap -U 5fd7a17c-fe33-46c0-8aeb-4c0979ae14f7 /dev/cl/swap

# create logical volume "root" as part of volume group "cl"
lvcreate -l 100%FREE -n root cl
# format logical volume, matching uuid of source machine
# you could use a different filesystem here, but make sure to adapt /etc/fstab
mkfs -O ^64bit,^metadata_csum -t ext4 -L root -U "24f1c076-ccab-4a2b-b1a6-e1264ec8ff29" /dev/cl/root

3. mount logical volumes on source and target machine (do this on both machines)

mkdir /mnt/root
mount /dev/cl/root /mnt/root

mkdir /mnt/home
mount /dev/cl/home /mnt/home

4. rsync from source to target machine

# mirror current system to new system
rsync -avxHAX -r --update --progress /mnt/root/ root@ip.of.target.machine:/mnt/root
rsync -avxHAX -r --update --progress /mnt/home/ root@ip.of.target.machine:/mnt/home
# options explained:
# -a, archive mode, all files, with permissions, equals -rlptgoD (no -H,-A,-X)
# -v  : verbose, mention files
# -x  : stay on one file system
# -H  : preserve hard links (not included with -a)
# -A  : preserve ACLs/permissions (not included with -a)
# -X  : preserve extended attributes (not included with -a)

# reboot and keep fingers crossed

sync; reboot -r now;

liked this article?

  • only together we can create a truly free world
  • plz support dwaves to keep it up & running!
  • (yes the info on the internet is (mostly) free but beer is still not free (still have to work on that))
  • really really hate advertisement
  • contribute: whenever a solution was found, blog about it for others to find!
  • talk about, recommend & link to this blog and articles
  • thanks to all who contribute!
admin