Convert linux from one system to another

From marc, 5 Years ago, written in Plain Text, viewed 3'093 times.
URL Embed
Download Paste or View Raw
  1. ====================================================
  2. How to transfer a Linux install to another machine.
  3. ====================================================
  5. Either the source or destination can be a virtual machine, physical machine, or possibly even a broken disk you can't boot or mount. This howto doesn't really make an assumption of which, so be intelligent and adapt to your needs.
  7. If you want to end up with 2 machines in the end, make sure you have 2 machines to start. If the destination is a virtual machine, make a new vm with a blank disk to send the install to. Make sure the source disk is attached in a way that you can access it, such as attached to a working virtual machine.
  9. ====================================================
  10. Full version
  11. ====================================================
  13. either:
  14.     1) Start both machines and boot the destination one on a LiveCD. (physical machines, or virtual machines)
  15.     2) mount the destination disk inside the source machine (more difficult to get the bootloader working, but then you don't need a LiveCD)
  17. On the destination machine:
  18.     Format the disk the way you want it,  For example:
  19.         parted /dev/sda mktable gpt
  20.         parted /dev/sda mkpart root 1 -- -1
  21.         mkfs.ext4 /dev/sda1
  23.     Then mount the new disk.
  24.         mkdir /mnt/root
  25.         mount /dev/sda1 /mnt/root
  27.     Transfer using rsync or scp.
  28.         at this point, it is assumed that the source disk is not changing so fast that an rsync copy would be inconsistent. You should shut down all your services on the source machine. You should especially shut down running databases.
  30.         scp push from the source machine (best for speed over fast network):
  31.             source machine :
  32.                 cd /
  33.                 ls
  34.                 scp -r bin boot etc home lib lib64 opt root run sbin selinux srv test tmp usr var destMachine:/mnt/root/
  36.             If the scp is interrupted, you can resume it with scp again, or resume with rsync. But if you use scp again, you also should run rsync afterwards just to fix any half transferred files.
  38.         rsync pull (best for speed over slow network, or to resume an interrupted transfer):
  39.             Then use rsync to pull the whole root system to the new machine (exclude /dev /proc /sys and anything else unneeded: /mnt /media etc).  
  41.                 rsync -avHP --exclude dev --exclude proc --exclude sys --exclude media --exclude mnt sourceMachine:/ /mnt/root/
  43.                     (add -z if you have a very slow network, which compresses things)
  45.     Create some directories we need
  46.         cd /mnt/root
  47.         mkdir dev proc sys media mnt
  49.     Then from the destination machine, you chroot into wherever you put the files, and run "grub-install /dev/sdX" and "update-grub"
  51.         mount -o rbind /dev dev
  52.         mount -o rbind /proc proc
  53.         mount -o rbind /sys sys
  54.         chroot /mnt/root
  56.         Now within the chroot environment (make sure device /dev/sda is correct or you can damage your other install):
  58.             blkid
  59.             vim /etc/fstab
  60.                 replace any UUID=... lines with the correct uuid from blkid
  61.                 if it uses /dev/sdX instead, replace it with UUID=...
  62.                 for lvm, it's fine to use /dev/vg/lv or /dev/mapper/vg-lv instead of UUID
  63.                 also /dev/disk/by-id/... and /dev/disk/by-uuid/... is fine.
  65.             vim /etc/udev/rules.d/70-persistent-net.rules
  66.                 (Optional) remove all lines from this file, or replace the old MAC addresses with the new ones. This will cause the network devices to appear in the default order again (eth0, eth1, etc.) instead of the dest machine having the names all different when you start it up again.
  68.                 If you start up your new vm and eth0 doesn't exist and instead you have eth1, you skipped this step.
  70.                 On RedHat and CentOS, I think this file doesn't exist, and I'm not sure what to do in this step.
  72.             grub-install /dev/$disk
  73.             update-grub
  74.                 NOTE: this will not always work if you have other disks attached. With a LiveCD, this probably always works fine.
  76.             non-debian systems don't have update-grub... instead use something like:
  77.                 grub-mkconfig -o /boot/grub/grub.cfg
  79.             then you edit fstab and any other important files (maybe even grub.cfg) to make sure partitions match what you see in "lsblk" output. Use uuids to be safest.
  81.             vim /boot/grub/grub.cfg
  82.                 normally you don't hand edit this file, instead update-grub or grub-mkconfig does this... but if you have other disk attached, you need to fix this once, reboot and you can relyon update-grub/grub-mkconfig afterwards. With a single disk and booted from a LiveCD, you shouldn't need to do this.
  84.                 replace any UUIDs that are wrong with the correct root one
  86.             Then you might need to rebuild the initrd (probably depends on hardware or something changing rather than simply the files being copied to a new disk on same hardware).
  87.                 In openSUSE, this should work:
  88.                     mkinitrd
  89.                 in Debian, it is:
  90.                     mkinitramfs
  91.                 and in some you need to specify some options
  92.                     mkinitrd -k nameofkernelfile -i nameofinitrdfile ... etc.
  93.                 and in some, you need to install this tool because it's not there by default.
  95.             Then you need to know the 2 machines aren't running at the same time, or at least don't have the same IP. So at this point you can either:
  96.                 1) shut down the source machine
  97.                 2) change the IP on one machine to something else
  98.                 (the mac address should also be different... make sure this is true; check ifconfig output)
  100.                     debian based:
  101.                         vim /etc/network/interfaces
  102.                     openSUSE:
  103.                         vim /etc/sysconfig/network/ifcfg-eth0
  104.                         ( also ifcfg-eth1, ifcfg-br0, etc.)
  106.             you should be done now. hit ctrl+d to exit the chroot
  108.     Then unmount everything (optional since reboot does this)
  110.         umount /mnt/root/{dev,proc,sys}
  111.         umount /mnt/root
  113.     Then try to boot your new system without the LiveCD
  116. ====================================================
  117. The short short version
  118. ====================================================
  119.  (just to show it's really a shorter procedure than it appears above):
  121.     parted /dev/sda mktable gpt
  122.     parted /dev/sda mkpart root 1 -- -1
  124.     mkfs.ext4 /dev/sda1
  126.     mount /dev/sda1 /mnt/root
  128.     rsync -avHP --exclude /dev --exclude /proc --exclude /sys --exclude /media --exclude /mnt sourceMachine:/ /mnt/root/        
  130.     cd /mnt/root
  131.     mkdir dev proc sys media mnt
  133.     mount -o rbind /dev dev
  134.     mount -o rbind /proc proc
  135.     mount -o rbind /sys sys
  136.     chroot /mnt/root
  138.         blkid
  139.         vim /etc/fstab
  140.             make sure UUIDs are right
  141.         vim /etc/udev/rules.d/70-persistent-net.rules
  142.             (Optional) remove all lines from this file
  144.         grub-install /dev/$disk
  145.         grub-mkconfig -o /boot/grub/grub.cfg
  147.         vim /boot/grub/grub.cfg
  148.             make sure UUIDs are right
  150.         mkinitrd
  152.         #debian based:
  153.             vim /etc/network/interfaces
  154.         #openSUSE:
  155.             vim /etc/sysconfig/network/ifcfg-eth0
  157.         exit

Reply to "Convert linux from one system to another"

Here you can reply to the paste above