11CloneVM - amagerard/TemplateVM GitHub Wiki

RedHat/TemplateVM.

1- Installation 2- Network 3- Firewall 4- Selinux
5- Logs 6- Certificates 7- Mail 8- PassphraseSSH
9- Sudo 10- GnomeShell 11- CloneVM 12- Volume
13- Troubleshoot

11. Clone TemplateVM.

11.1 Clone to hypervisor.

Example:
I will clone TemplateVM to create the wikijs VM.
I allow myself ssh access to the esxi.
ssh root@Hypervisor IP
I type df to view the datastores.

Filesystem        Bytes         Used    Available Use% Mounted on  
VMFS-6     999922073600 210197544960 789724528640  21% /vmfs/volumes/DATA-SSD4  

cd /vmfs/volumes/DATA-SSD4

/vmfs/volumes/41d6677e-e994f23d-b0ba-001b217d646c]  

41d6677e-e994f23d-b0ba-001b217d646c is the UUID of the disk which corresponds
in this example to DATA-SSD4.
I created the folder where I want to clone the VM (here wikijs).
mkdir /vmfs/volumes/DATA-SSD4/wikijs
I position myself exactly where the VM to be cloned is.
cd /vmfs/volumes/DATA-SSD4/TemplateVM

-rw-------    1 root     root   2949672960 Jan 28 23:06 TemplateVM-flat.vmdk  
-rw-------    1 root     root        270840 Jan 28 23:06 TemplateVM.nvram  
-rw-------    1 root     root           553 Jan 28 13:26 TemplateVM.vmdk  
-rw-r--r--    1 root     root             0 Jan 21  2005 TemplateVM.vmsd  
-rwxr-xr-x    1 root     root          3660 Jan 28 23:06 TemplateVM.vmx  
-rw-------    1 root     root           150 Jan 26 16:08 TemplateVM.vmxf  

The file to be cloned has the extension .vmdk.
This is not the file that has flat in its name.
I clone et rename the destination VM file.
vmkfstools -i TemplateVM.vmdk -d thin /vmfs/volumes/DATA-SSD4/wikijs/wikijs.vmdk
I copy files that have .nvram , .vmsd, .vmx, .vmxf extensions renaming them.
cp TemplateVM.vmxf /vmfs/volumes/DATA-SSD4/wikijs/wikijs.vmxf
cp TemplateVM.vmsd /vmfs/volumes/DATA-SSD4/wikijs/wikijs.vmsd
cp TemplateVM.nvram /vmfs/volumes/DATA-SSD4/wikijs/wikijs.nvram
cp TemplateVM.vmx /vmfs/volumes/DATA-SSD4/wikijs/wikijs.vmx
The VM configuration file is the one with the extension .vmx.
vi /vmfs/volumes/DATA-SSD4/wikijs/wikijs.vmx
I rename all of them TemplateVM to wikijs.
The command below does this automatically.
I position myself in /vmfs/volumes/DATA-SSD4/wikijs/.
sed -i -e "s/TemplateVM/wikijs/g" wikijs.vmx
My VM wikijs is ready.
From the ESXi management web interface.
I create an existing virtual machine from the file with extension vmx.

11.2 Clone to a physical machine.

The following is not intended for beginners.

Download the latest live cd version kde almalinux9.
https://repo.almalinux.org/almalinux/9/live/x86_64/
AlmaLinux-9-latest-x86_64-Live-KDE.iso
Start VM vmware remote console
Boot on live iso.
Add usb disk in removal devices and connect.
Change keyboard type if necessary in system settings.
Start Dolphin File Manager.
Click on boot,root,home,opt,usrlocal,var,varlog and your usb disk.
open a console.
type sudo su.
These folders are mounted on /run/media/liveuser.
Note the user who is /run/media/liveuser/home.
Do a tar file for each folder to your usb disk.
Example:
cd /run/media/liveuser/root
tar --acls -cvzf /run/media/liveuser/<your usb disk>/my_root.tar .
The option "--acls" is very important.
Do a copy of these files on your usb disk.
cp /run/media/liveuser/root/etc/fstab /run/media/liveuser/<your usb disk>/root-etc-fstab
cp -R /run/media/liveuser/boot/loader/entries /run/media/liveuser/<your usb disk>/boot-loader-entries
cp /run/media/liveuser/root/etc/kernel/cmdline /run/media/liveuser/<your usb disk>/root-etc-kernel-cmdline
cp /run/media/liveuser/root/etc/default/grub /run/media/liveuser/<your usb disk>/root-etc-default-grub

Return Dolphin File Manager.
Close all folders cleanly before shutting down the virtual machine.

Do a new installation on your physical machine with the minimal almalinux9 is.
Create user who was on the virtual machine.
Choose custom partition.
Do not choose lvm.
You must only have.

boot-efi  250M  
boot      600M  
/        as you want  
var      as you want  
home     as you want  
swap     same memory as  your physical machine.  

You start one time your physical machine.
Now you start with iso usb AlmaLinux-9-latest-x86_64-Live-KDE.iso. on your physical machine.
I use rufus to make an iso.
It happens that it does not start on the key.
Google is your friend when it comes to finding the right solution.
Anyway you will have to boot from the iso.

From the live cd.
open a console and type "sudo su".
dnf update answers no.
dnf install gparted
Start gparted.

You delete the partitions:

root  
var  
home  
swap  

You dont delete boot and boot-efi.

You recreate the partitions in this order.

root   space as you want  
var    space  as you want  
home   space as you want  
swap   same memory as  your physical machine  

Add your usb disk.
Start Dolphin File Manager.
Click on boot,root,home,opt,usrlocal,var,varlog and your usb disk.
open a console.
type sudo su.
These folders are mounted on /run/media/liveuser.
Extract tar files only root, var and home from your USB disk to each partition of the disk.
Example:
cd /run/media/liveuser/root
tar --acls -xvzf /run/media/liveuser/<your usb disk>/my_root.tar .
The option "--acls" is very important.
/opt, /var/log and /usr/local are missing.
Example for /var/log .
cd /run/media/liveuser/var/log
tar --acls -xvzf /run/media/liveuser/<your usb disk>/my_varlog.tar .
cd /run/media/liveuser/root/usr/local
tar --acls -xvzf /run/media/liveuser/<your usb disk>/my_usrlocal.tar .
cd /run/media/liveuser/root/opt
tar --acls -xvzf /run/media/liveuser/<your usb disk>/my_opt.tar .

Change swap uuid.
more /run/media/liveuser/<your usb disk>/root-etc-fstab
Note uuid for swap.
disable swap for the swap partition.
swapoff -a
Or with Kde Partition Manager.
note also the name of partition. Example /dev/sda6 .
mkswap -U <uuid of fstab for the swap> <name as /dev/sda6>
blkid| grep sda6

Note uuid for root var and home.
blkid| grep root
blkid| grep var
blkid |grep home

Modify fstab
vi /run/media/liveuser/root/etc/fstab
Change uuid for boot et boot/efi .
Change /dev/mapper/... by UUID=<uuid> for / , /home and /var .
Delete line for /opt , /usr/local and /var/log .
UUID for swap is good.
Example:

#  
UUID=6ac09a23-3f80-4abf-9098-316316935835    /               ext4    defaults             1 1  
UUID=2d5134da-7ede-4991-9348-58432fbd4c00    /boot           ext4    defaults             1 2  
UUID=6098-4525          /boot/efi            vfat            umask=0077,shortname=winnt   0 2  
UUID=dac337d3-5377-48d5-94fb-19a6333d1535    /home           ext4    defaults             1 2  
UUID =e26944c3-0950-47e9-a71b-51c42f12e4c6    /var           ext4    defaults            1 2  
UUID=02f635d9-68f1-4cbe-ba14-10591a28a911     none           swap    defaults            0 0  

Modify grub.
vi /run/media/liveuser/root/etc/default/grub
The resume is swap uuid.
Delete rd.lvm.lv=vgl0/root

Modify the loader.
cd /run/media/liveuser/boot/loader/entries
You take the file with the most recent kernel or if you are an apprentice, you do it for each file.
You change line options UUID for root and (swap) for resume.
Example :

options root=UUID=a68ea4be-e35b-4773-b6ca-50211e8520fd ro crashkernel=1G-4G:192M,4G-64G:256M,64G-:512M \  
resume=UUID=e23ff1c4-08d6-43f9-b160-be869c290ab0 net.ifnames=0  

Modify cmdline
I don't know if it's really useful.
cd /run/media/liveuser/root/etc/kernel
vi cmdline
You change /dev/mapper by UUID=<uuid root> for root.
Delete rd.lvm.lv=vgl0/root

Modify selinux
vi /run/media/liveuser/root/etc/selinux/config
SELINUX=disable
"disable" and not "disabled" is for opening root session if selinux is crashed.
vi /run/media/liveuser/root/etc/crontab

#@reboot root setenforce 0`  

Return Dolphin File Manager.
Close all folders cleanly before shutting down the virtual machine.
Unplugged all usb.
Poweroff the physical machine.
Unplugged all usb.
And start.

Rename ethernet card to eth0.
grubby --args ipv6.disable=1 --update-kernel DEFAULT
grubby --update-kernel=ALL --args=net.ifnames=0
grub2-mkconfig -o /boot/grub2/grub.cfg

Rename Mac address of ethernet card in ifcfg-eth0
vi /etc/sysconfig/network-scripts/ifcfg-eth0

## config ethernet card   
NAME=eth0  
DEVICE=eth0  
HWADDR="00:E0:8B:68:01:13"  # put your  Mac Address  
ONBOOT="yes  

Repair selinux.
touch /.autorelabel
reboot

Modify selinux.
vi /run/media/liveuser/root/etc/selinux/config
SELINUX=enforcing
vi /run/media/liveuser/root/etc/crontab

@reboot root setenforce 0  

reboot

The end.

11.3 Troubleshoots.

The physical machine does not start.
Message: dracut /dev/disk/by-uuid/XXX does not exist.
https://www.bleepingcomputer.com/forums/t/740193/how-to-repair-or-re-install-grub-using-the-chroot-command/
Start with a debian or almalinux live cd.
If you have logical partitions I recommend the almalinux KDE live cd.
Create a folder like /media/root
blkid to find your disk partitions.
Example for physical partitions :

/dev/sdi1 is boot-efi  
/dev/sdi2 is boot  
/dev/sdi3 is root  
/dev/sdi4 is var  

Mount these partitions on /media/root starting with the root partition and then boot.
Example:

mount /dev/sdi3 /media/root  
mount /dev/sdi2 /media/root/boot  
mount /dev/sdi1 /media /root/boot/efi  
mount /dev/sdi4 /media/root/var  

Building your chroot.

 mount --bind /dev /media/root/dev  
 mount --bind /dev/pts /media/root/dev/pts  
 mount --bind /proc /media/root/proc  
 mount --bind /sys /media/root/sys  

chroot /media/root
You must have /etc/default/grub and /etc/fstab properly configured before you begin.
cd boot
Find the latest initramfs img ( not kdump).
Example : initramfs-5.14.0-427.13.1.el9_4.x86_64.img
dracut --mdadmconf --fstab --force /boot/initramfs-5.14.0-427.13.1.el9_4.x86_64.img 5.14.0-427.13.1.el9_4.x86_64 -M
Update grub.
grub2-mkconfig -o /boot/grub2/grub.cfg
Before restarting your machine you must exit properly.
exit to exit chroot.

umount /media/root/dev/pts  
umount /media/root/dev  
umount /media/root/proc  
umount /media/root/sys  
umount /dev/sdi4  
umount /dev/sdi1  
umount /dev/sdi2  
umount /dev/sdi3  

reboot on your disk.

⚠️ **GitHub.com Fallback** ⚠️