User Tools

Site Tools


linux:system:disk:clone-system

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
linux:system:disk:clone-system [2023/09/08 11:32] oscarlinux:system:disk:clone-system [2023/09/08 12:33] (current) – [Write Target System] oscar
Line 2: Line 2:
  
 ---- ----
-===== Grub Configuration ===== +===== Overview ===== 
-GRUB settings are stored in the following locations: +This page assumes that system is installed in EFI mode havinga 200-500MB vfat/fat32 "EFI system" partition.
-  * **/etc/default/grub** file Edit this file to change GRUB2’s settings.  +
-  * **/etc/grub.d/** directory contains additional scripts that are loaded by the /etc/default/grub file. For example, on Ubuntu, there are scripts here that configure the default theme. There’s also an os-prober script that checks the system’s internal hard drives for other installed operating systems — Windows, other Linux distributions, Mac OS X, and so on — and automatically adds them to GRUB2’s menu. +
-  * **/boot/grub/grub.cfg** file that’s created by update-grub and read at boot+
  
-When you run the update-grub commandGRUB automatically combines the settings from the /etc/default/grub filethe scripts from the /etc/grub.ddirectoryand everything elsecreating a /boot/grub/grub.cfg file that’s read at boot. In other words, to customize your GRUB2 settings, you’ll have to edit the **/etc/default/grub** file and then run the **update-grub** command. Subsequently install/update the Grub loader in the disk+===== Get Source Images ===== 
-    # /etc/default/grub +Create a live USB and boot system from USB. Once booted into the live-cdmount the source filesystem. First check the correct device with lsblk 
-    # update-grub +<code> 
-    # grub-install /dev/sdX +# lsblk 
-    # grub-install --recheck /dev/sdX+NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT 
 +sda      8:0    0 931,5G  0 disk  
 +├─sda1   8:   0   512M  0 part /boot/efi 
 +├─sda2   8:   0  18,6G  0 part / 
 +└─sdb3   8:   0   9,8G  0 part [SWAP] 
 +sdb      8:0    0 931,5G  0 disk  
 +├─sdb1   8:   0   512M  0 part /boot/efi 
 +├─sdb2   8:   0  18,6G  0 part / 
 +├─sdb3   8:   0   9,8G  0 part [SWAP] 
 +└─sdb4   8:   0 902,6G  0 part /media/storage 
 +</code> 
 +We need to copy the **"/boot/efi"** and **"/"** partitions to the new system.  
 +==== 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:
  
-===== Probe installed OS ===== +  # dd bs=4M if=/dev/sdb1 | gzip > efi-image.gz 
-Open a terminal and run the os-prober command with root privilegesThis will search for other operating system installations besides the current distro you’re booted into.+  # 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. 
 +<code> 
 +# mkdir /tmp/source_sdXY 
 +# mount -t ext4 /dev/sdXY /tmp/source_sdXY 
 +# tar -zcvf image-sdXY.tgz /tmp/source_sdXY 
 +</code> 
 +===== Create Target Disk ===== 
 +Again, first check with lsblk the disk structure, before proceeding: 
 +<code> 
 +# lsblk 
 +NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT 
 +sda      8:0    0 931,5G  0 disk  
 +├─sda1   8:   0   512M  0 part /boot/efi 
 +├─sda2   8:   0  18,6G  0 part / 
 +└─sdb3   8:   0   9,8G  0 part [SWAP] 
 +sdb      8:0    0 931,5G  0 disk  
 +├─sdb1   8:   0   512M  0 part /boot/efi 
 +├─sdb2   8:   0  18,6G  0 part / 
 +├─sdb3   8:   0   9,8G  0 part [SWAP] 
 +└─sdb4   8:   0 902,6G  0 part /media/storage 
 +</code> 
 +==== Write EFI Partition ==== 
 +<code> 
 +# gzip -dc efi-image.gz | dd bs=4M of=/dev/sdbX 
 +# sync 
 +</code> 
 +==== 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 /tmp/source_sdXY directory as we used for the tar. 
 +  # mount -t ext4 /dev/sdX /tmp/source_sdXY  
 +Now restore the files to the partitions. 
 +  # cd / 
 +  # tar -zxvf image-sdX1.tgz
  
-  $ sudo os-prober+===== 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. 
  
-It should find your Linux distro, the Windows installation, and possibly a memory test installation or recovery partitionTo add these findings to the GRUB menu, execute the update-grub command with root permissions. +Firstchange directories to where the new installation is mounted (e.g. /tmp/source_sdXY in the example above):
-  $ sudo update-grub+
  
-The output should show that Windows 10 has been found and added to the GRUB boot menu.+  # cd /tmp/source_sdXY
  
 +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. 
 +<code>
 +# for i in /dev /dev/pts /proc /sys /run; do mount --bind $i .$i; done
 +Which is similar to:
 +# mount --bind /dev /source_sdXY/dev
 +# mount --bind /dev/pts /source_sdXY/dev/pts
 +# mount --bind /proc /source_sdXY/proc
 +# mount --bind /sys /source_sdXY/sys  
 +</code>
  
-===== Repair, Restore, or Reinstall Grub 2 with Live USB ===== +Because we installing in EFI mode we also need to give our chroot access to the EFI partition we mounted earliermount --bind comes to the rescue again herewe 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). 
-Grub 2 typically gets overridden when you install Windows or another Operating SystemTo make Linux control the boot processyou need Reinstall (Repair/RestoreGrub using a Live CD.+First mount the new EFI partition: 
 +  # mkdir /tmp/efi-part 
 +  # mount /dev/sdX1 /tmp/efi-part 
 +Now bind it in the new croot partition:   
 +  # cd /tmp/source_sdXY 
 +  # mkdir -p boot/efi  
 +  # mount --bind /tmp/efi-part boot/efi
  
-Create a live USB and boot system from USB+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 /tmp/source_sdXY 
 + 
 +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 /sys/firmware/efi/efivars 
 + 
 +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 ===== 
 +Create a live USB and boot system from USB. Once booted into the live-cd, mount your destination filesystem.
  
 Mount the partition your broken Linux installation is on. If you are not sure which it is, launch GParted (included in the Live CD) and find out. It is usually a EXT4 Partition. Replace the XY with the drive letter, and partition number, for example: sudo mount /dev/sda1 /mnt. Mount the partition your broken Linux installation is on. If you are not sure which it is, launch GParted (included in the Live CD) and find out. It is usually a EXT4 Partition. Replace the XY with the drive letter, and partition number, for example: sudo mount /dev/sda1 /mnt.
Line 61: Line 150:
 </code> </code>
  
-Alternatively, in case of persistent problems, you can purge and reinstall grub2, make new config files: 
-<code> 
-apt-get remove --purge grub-pc grub-common 
-apt-get install grub-pc 
-grub-mkconfig 
-update-grub 
-grub-install /dev/sda 
-</code> 
- 
-Now grub is back, all that is left is to exit the chrooted system and unmount everything: 
-<code> 
-# exit 
-# umount /mnt/sys 
-# umount /mnt/proc 
-# umount /mnt/dev/pt 
-# umount /mnt/dev 
-# umount /mnt 
-</code> 
 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.
-===== Customize boot layout ===== 
-For customizing the layout of the Grub startup screen at boot, the files are located in:/boot/grub. 
-The sample below illustrates adding a background image and changing the font size. 
-Copy the background images to the folder: /boot/grub. Remember that other partitions might not yet be mounted and available at boot time.  
- 
-=== Find screen resolution === 
-To find the possible screen resolutions for Grub, do to the command line when Grub starts, by pressing 'c' in the menu to enter the command line. On the command line press: 
-  videoinfo 
-Then a list of possible option will be shown. 
- 
-=== Configure screen resolution and background image === 
-Grub prefers png images instead of jpg images. Update /etc/default/grub as follows: 
- 
-  #nano /etc/default/grub 
-   
-  Modify Lines: 
-   
-  GRUB_BACKGROUND=/boot/grub/Katwijk-Silouet-edge-grain1-grub-1920x1080.png 
-  GRUB_GFXMODE=1920x1080 
-   
-If you change this file, run 'update-grub' afterwards to update /boot/grub/grub.cfg. 
-  update-grub 
- 
-Currently a grub2 wallpaper image cannot be larger than 1024×768px, and it will (configured by default) be stretched to match your gfx resolution. One way to give the illusion of a 16:9 grub wallpaper is to take an existing 16x9 wallpaper and scale it to the 4:3 image of size 1024x768, and set the grub gfx mode for example to GRUB_GFXMODE=1920x1080, when grub loads and stretches that image its aspect ratio will appear similar to the original. 
- 
  
linux/system/disk/clone-system.1694172748.txt.gz · Last modified: by oscar