Advertisement
peetaur

How to transfer a Linux install to another machine.

Jan 17th, 2013
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.92 KB | None | 0 0
  1. ====================================================
  2. How to transfer a Linux install to another machine.
  3. ====================================================
  4.  
  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.
  6.  
  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.
  8.  
  9. ====================================================
  10. Full version
  11. ====================================================
  12.  
  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)
  16.  
  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
  22.  
  23. Then mount the new disk.
  24. mkdir /mnt/root
  25. mount /dev/sda1 /mnt/root
  26.  
  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.
  29.  
  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/
  35.  
  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.
  37.  
  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).
  40.  
  41. rsync -avHP --exclude dev --exclude proc --exclude sys --exclude media --exclude mnt sourceMachine:/ /mnt/root/
  42.  
  43. (add -z if you have a very slow network, which compresses things)
  44.  
  45. Create some directories we need
  46. cd /mnt/root
  47. mkdir dev proc sys media mnt
  48.  
  49. Then from the destination machine, you chroot into wherever you put the files, and run "grub-install /dev/sdX" and "update-grub"
  50.  
  51. # next 4 lines: general, non-arch based (works for arch too, but pacman won't work within chroot)
  52. mount -o rbind /dev dev
  53. mount -o rbind /proc proc
  54. mount -o rbind /sys sys
  55. chroot /mnt/root
  56.  
  57. # next 1 line: archlinux based (requires arch-install-scripts package)
  58. arch-chroot /mnt/root
  59.  
  60. Now within the chroot environment (make sure device /dev/sda is correct or you can damage your other install):
  61.  
  62. blkid
  63. vim /etc/fstab
  64. replace any UUID=... lines with the correct uuid from blkid
  65. if it uses /dev/sdX instead, replace it with UUID=...
  66. for lvm, it's fine to use /dev/vg/lv or /dev/mapper/vg-lv instead of UUID
  67. also /dev/disk/by-id/... and /dev/disk/by-uuid/... is fine.
  68.  
  69. vim /etc/udev/rules.d/70-persistent-net.rules
  70. (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.
  71.  
  72. If you start up your new vm and eth0 doesn't exist and instead you have eth1, you skipped this step.
  73.  
  74. On RedHat and CentOS, I think this file doesn't exist, and I'm not sure what to do in this step.
  75.  
  76. grub-install /dev/$disk
  77. update-grub
  78. NOTE: this will not always work if you have other disks attached. With a LiveCD, this probably always works fine.
  79.  
  80. non-debian systems don't have update-grub... instead use something like:
  81. grub-mkconfig -o /boot/grub/grub.cfg
  82.  
  83. 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.
  84.  
  85. vim /boot/grub/grub.cfg
  86. 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.
  87.  
  88. replace any UUIDs that are wrong with the correct root one
  89.  
  90. 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).
  91. In openSUSE, this should work:
  92. mkinitrd
  93. in Debian, it is:
  94. mkinitramfs
  95. or
  96. #(not sure if it's -c or -u)
  97. update-initramfs -u
  98. or on arch/manjaro; replace "linux" with your kernel name. List first with ls.
  99. mkinitcpio -p /etc/mkinitcpio.d/linux.preset
  100. or on arch/manjaro alternative; (add kernel version in there, based on filenames in /lib/modules/)
  101. mkinitramfs -g /boot/initramfs-...-x86_64.img -k ...
  102.  
  103. and in some you need to specify some options
  104. mkinitrd -k nameofkernelfile -i nameofinitrdfile ... etc.
  105. and in some, you need to install this tool because it's not there by default.
  106.  
  107. 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:
  108. 1) shut down the source machine
  109. 2) change the IP on one machine to something else
  110. (the mac address should also be different... make sure this is true; check ifconfig output)
  111.  
  112. debian based:
  113. vim /etc/network/interfaces
  114. openSUSE:
  115. vim /etc/sysconfig/network/ifcfg-eth0
  116. ( also ifcfg-eth1, ifcfg-br0, etc.)
  117.  
  118. you should be done now. hit ctrl+d to exit the chroot
  119.  
  120. Then unmount everything (optional since reboot does this)
  121.  
  122. umount /mnt/root/{dev,proc,sys}
  123. umount /mnt/root
  124.  
  125. Then try to boot your new system without the LiveCD
  126.  
  127.  
  128. ====================================================
  129. The short short version
  130. ====================================================
  131. (just to show it's really a shorter procedure than it appears above):
  132.  
  133. parted /dev/sda mktable gpt
  134. parted /dev/sda mkpart root 1 -- -1
  135.  
  136. mkfs.ext4 /dev/sda1
  137.  
  138. mount /dev/sda1 /mnt/root
  139.  
  140. rsync -avHP --exclude /dev --exclude /proc --exclude /sys --exclude /media --exclude /mnt sourceMachine:/ /mnt/root/
  141.  
  142. cd /mnt/root
  143. mkdir dev proc sys media mnt
  144.  
  145.  
  146. # next 4 lines: general, non-arch based (works for arch too, but pacman won't work within chroot)
  147. mount -o rbind /dev dev
  148. mount -o rbind /proc proc
  149. mount -o rbind /sys sys
  150. chroot /mnt/root
  151.  
  152. # next 1 line: archlinux based (requires arch-install-scripts package)
  153. arch-chroot /mnt/root
  154. blkid
  155. vim /etc/fstab
  156. make sure UUIDs are right
  157. vim /etc/udev/rules.d/70-persistent-net.rules
  158. (Optional) remove all lines from this file
  159.  
  160. grub-install /dev/$disk
  161. grub-mkconfig -o /boot/grub/grub.cfg
  162.  
  163. vim /boot/grub/grub.cfg
  164. make sure UUIDs are right
  165.  
  166. #debian based: (not sure if it's -c or -u)
  167. update-initramfs -u
  168. #openSUSE:
  169. mkinitrd
  170. #arch/manjaro; replace "linux" with your kernel name. List first with ls.
  171. mkinitcpio -p /etc/mkinitcpio.d/linux.preset
  172. #arch/manjaro alternative; (add kernel version in there, based on filenames in /lib/modules/)
  173. mkinitramfs -g /boot/initramfs-...-x86_64.img -k ...
  174.  
  175. #debian based:
  176. vim /etc/network/interfaces
  177. #openSUSE:
  178. vim /etc/sysconfig/network/ifcfg-eth0
  179.  
  180. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement