linux:system:disk:clone-system
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| linux:system:disk:clone-system [2023/09/08 11:50] – oscar | linux:system:disk:clone-system [2023/09/08 12:33] (current) – [Write Target System] oscar | ||
|---|---|---|---|
| Line 5: | Line 5: | ||
| This page assumes that system is installed in EFI mode having: a 200-500MB vfat/fat32 "EFI system" | This page assumes that system is installed in EFI mode having: a 200-500MB vfat/fat32 "EFI system" | ||
| - | ===== Get Source | + | ===== Get Source |
| Create a live USB and boot system from USB. Once booted into the live-cd, mount the source filesystem. First check the correct device with lsblk | Create a live USB and boot system from USB. Once booted into the live-cd, mount the source filesystem. First check the correct device with lsblk | ||
| < | < | ||
| Line 21: | Line 21: | ||
| </ | </ | ||
| We need to copy the **"/ | We need to copy the **"/ | ||
| + | ==== Get EFI Partition ==== | ||
| + | We will use dd to get an exact image copy and the size of source and target partitions are usually similar. | ||
| + | Create Backup Image: | ||
| + | |||
| + | # dd bs=4M if=/ | ||
| + | # sync | ||
| + | ==== Get Root Partition ==== | ||
| + | For the root partition we only want to copy the files. This is faster since the root partitions is usually partially filled. | ||
| + | < | ||
| + | # mkdir / | ||
| + | # mount -t ext4 /dev/sdXY / | ||
| + | # tar -zcvf image-sdXY.tgz / | ||
| + | </ | ||
| + | ===== Create Target Disk ===== | ||
| + | Again, first check with lsblk the disk structure, before proceeding: | ||
| + | < | ||
| + | # lsblk | ||
| + | NAME | ||
| + | sda 8:0 0 931, | ||
| + | ├─sda1 | ||
| + | ├─sda2 | ||
| + | └─sdb3 | ||
| + | sdb 8:0 0 931, | ||
| + | ├─sdb1 | ||
| + | ├─sdb2 | ||
| + | ├─sdb3 | ||
| + | └─sdb4 | ||
| + | </ | ||
| + | ==== Write EFI Partition ==== | ||
| + | < | ||
| + | # gzip -dc efi-image.gz | dd bs=4M of=/ | ||
| + | # sync | ||
| + | </ | ||
| + | ==== Write Root Partition ==== | ||
| + | Empty and create a fresh filesystem on the root partition of the target disk: | ||
| + | # mkfs.ext4 -l root /dev/sdbX | ||
| + | Mount the root partition. Use the same / | ||
| + | # mount -t ext4 /dev/sdX / | ||
| + | Now restore the files to the partitions. | ||
| + | # cd / | ||
| + | # tar -zxvf image-sdX1.tgz | ||
| + | |||
| + | ===== install a bootloader ===== | ||
| + | At this point we have all the files we need on the new system, but we need to make the new system bootable. We will chroot into the newly extracted filesystem to install a bootloader. | ||
| + | |||
| + | First, change directories to where the new installation is mounted (e.g. / | ||
| + | |||
| + | # cd / | ||
| + | |||
| + | Then we’ll use mount ---bind to give the chroot access to the linux special directories. The bind option of the mount command allows you to remount part of a file hierarchy at a different location while it is still available at the original location. The format of this command is as follows. | ||
| + | < | ||
| + | # for i in /dev /dev/pts /proc /sys /run; do mount --bind $i .$i; done | ||
| + | Which is similar to: | ||
| + | # mount --bind /dev / | ||
| + | # mount --bind /dev/pts / | ||
| + | # mount --bind /proc / | ||
| + | # mount --bind /sys / | ||
| + | </ | ||
| + | |||
| + | Because we installing in EFI mode we also need to give our chroot access to the EFI partition we mounted earlier. mount --bind comes to the rescue again here, we simply bind mount the livecd mount point into the /boot/efi directory inside the chroot (/boot/efi is where grub expects to find the EFI partition). | ||
| + | First mount the new EFI partition: | ||
| + | # mkdir / | ||
| + | # mount /dev/sdX1 / | ||
| + | Now bind it in the new croot partition: | ||
| + | # cd / | ||
| + | # mkdir -p boot/ | ||
| + | # mount --bind / | ||
| + | |||
| + | Now that we have access to the Linux special folders (and the EFI partition), we can use the chroot command to actually use our source installation: | ||
| + | |||
| + | # chroot / | ||
| + | |||
| + | At this point you should have a shell inside the same Linux environment you originally copied. | ||
| + | We can run grub-install from inside the chroot to update the boot partition. | ||
| + | |||
| + | ===== Install bootloader ===== | ||
| + | Run grub-install against the drive you installed to. In my case that’s /dev/sdb, but this may be different on your machine. Next we install grub to our drive, thereby making it bootable. Be careful to install grub to a drive and not to a partition. | ||
| + | |||
| + | # grub-install /dev/sdX | ||
| + | # grub-install --recheck /dev/sdX | ||
| + | | ||
| + | # update-grub | ||
| + | |||
| + | If all went well you will see messages saying that grub was successfully installed. When you see this feel free to reboot and check out your freshly cloned installation. | ||
| + | |||
| + | ===== Troubleshooting ===== | ||
| + | If you get a a warning saying that “EFI variables cannot be set on this system,” you may need to mount the EFI vars into the chroot (h/t Jesse Dhillon): | ||
| + | |||
| + | # mount -t efivarfs none / | ||
| + | |||
| + | If you get error messages when installing in EFI mode it’s possible grub’s autodetect got confused and tried to use MBR mode when it should’ve used EFI. You may be able to succesfully perform an EFI install by forcing EFI mode like so: | ||
| + | |||
| + | @ grub-install --target=x86_64-efi | ||
| + | |||
| ===== Write Target System ===== | ===== Write Target System ===== | ||
| Line 56: | Line 150: | ||
| </ | </ | ||
| - | Alternatively, | ||
| - | < | ||
| - | apt-get remove --purge grub-pc grub-common | ||
| - | apt-get install grub-pc | ||
| - | grub-mkconfig | ||
| - | update-grub | ||
| - | grub-install /dev/sda | ||
| - | </ | ||
| - | |||
| - | Now grub is back, all that is left is to exit the chrooted system and unmount everything: | ||
| - | < | ||
| - | # exit | ||
| - | # umount /mnt/sys | ||
| - | # umount /mnt/proc | ||
| - | # umount /mnt/dev/pt | ||
| - | # umount /mnt/dev | ||
| - | # umount /mnt | ||
| - | </ | ||
| Shut down and turn your computer back on, and you will be met with the default Grub2 screen. | Shut down and turn your computer back on, and you will be met with the default Grub2 screen. | ||
linux/system/disk/clone-system.1694173819.txt.gz · Last modified: by oscar
