Differences between revisions 2 and 35 (spanning 33 versions)
Revision 2 as of 2017-10-12 22:54:07
Size: 1048
Editor: shran
Comment:
Revision 35 as of 2021-12-31 12:00:02
Size: 3680
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
Once a template system has been installed, that system can be cloned. `virt-clone` will automatically change the MAC address of your network device.
Line 3: Line 4:
Once a template system has been installed, that system can be cloned. Use the following script to clone your template == Cloning ==
Use the script below to clone your template.
Line 5: Line 7:
`clonedomain.sh` `domain_clone_bullseye.sh`
Line 9: Line 11:
LVM_GROUP="vg2" SRC_VOLUME_GROUP="vg_storage"
DST_VOLUME_GROUP="vg_storage"
SRC_NAME="bullseye-template"
SRC_VOLUME="kvm_${SRC_NAME}_vda"
SRC_VOLUME_SIZE=`lvs /dev/${SRC_VOLUME_GROUP}/${SRC_VOLUME} -o LV_SIZE --noheadings --units B --nosuffix`
DST_NAME="bullseye-clone"
DST_VOLUME="kvm_${DST_NAME}_vda"
DST_MEMORY="256M"
MOUNTPOINT="/mnt/domain_root"
MAC=""
Line 11: Line 22:
NAME_ORIGINAL="stretch-template"
LVM_NAME_ORIGINAL="kvm_${NAME_ORIGINAL}_vda"
LVM_SIZE_ORIGINAL=`lvs /dev/${LVM_GROUP}/${LVM_NAME_ORIGINAL} -o LV_SIZE --noheadings --units B --nosuffix`
NAME_CLONE="stretch-clone"
LVM_NAME_CLONE="kvm_${NAME_CLONE}_vda"


if [ $# -eq 0 ]
if [ ! -e /sbin/kpartx ]
Line 20: Line 24:
        echo "${0}: domain-name"
else
        NAME_CLONE=${1}
        LVM_NAME_CLONE="kvm_${NAME_CLONE}_vda"
 apt-get install -y kpartx
Line 26: Line 27:
if [ ! -e /dev/${LVM_GROUP}/${LVM_NAME_CLONE} ] if [ $# -gt 0 ]
Line 28: Line 29:
        # create lvm partition
        lvcreate --size ${LVM_SIZE_ORIGINAL}B --name ${LVM_NAME_CLONE} ${LVM_GROUP}

        # clone the domain
        virt-clone --original ${NAME_ORIGINAL} --name ${NAME_CLONE} --file /dev/${LVM_GROUP}/${LVM_NAME_CLONE} --check path_in_use=off,path_exists=off
else
        echo "${0}: refusing to overwrite existing domain"
 DST_NAME=${1}
 DST_VOLUME="kvm_${DST_NAME}_vda"
Line 37: Line 33:
if [ "${2}" != "" ]
then
 MAC="--mac ${2} "
fi

if [ ! -e /dev/${DST_VOLUME_GROUP}/${DST_VOLUME} ]
then
 # create lvm partition
 lvcreate --yes --size ${SRC_VOLUME_SIZE}B --name ${DST_VOLUME} ${DST_VOLUME_GROUP}

 # clone the domain
 virt-clone \
  --original ${SRC_NAME} \
  --name ${DST_NAME} \
  --file /dev/${DST_VOLUME_GROUP}/${DST_VOLUME} \
  ${MAC} \
  --check path_in_use=off,path_exists=off

 # mount the domains / partition
 OFFSET=`kpartx /dev/${DST_VOLUME_GROUP}/${DST_VOLUME} | tail -n1|awk {'print $6'}`
 OFFSET_BYTES="$((${OFFSET} * 512))"
 losetup -o ${OFFSET_BYTES} /dev/loop0 /dev/${DST_VOLUME_GROUP}/${DST_VOLUME}
 pvscan
 vgscan
 vgchange -ay vg_os
 if [ ! -e ${MOUNTPOINT} ]
 then
  mkdir -p ${MOUNTPOINT}
 fi
 mount -o subvol=@rootfs /dev/vg_os/root ${MOUNTPOINT}
 # manipulate domain
 mkdir ${MOUNTPOINT}/root/scripts
 cp /root/scripts/guest_cleanup.sh ${MOUNTPOINT}/root/scripts
 # hostname
 cp ${MOUNTPOINT}/etc/hosts ${MOUNTPOINT}/etc/hosts.kk
 sed -i "s/${SRC_NAME}/${DST_NAME}/g" ${MOUNTPOINT}/etc/hosts
 cp ${MOUNTPOINT}/etc/hostname ${MOUNTPOINT}/etc/hostname.kk
 sed -i "s/${SRC_NAME}/${DST_NAME}/g" ${MOUNTPOINT}/etc/hostname

 # rc.local
 echo "#!/bin/sh" > ${MOUNTPOINT}/etc/rc.local
 # this delay is needed for the network to settle, works with 32, but using 64 to be sure
 #echo "sleep 64" >> ${MOUNTPOINT}/etc/rc.local
 echo "/root/scripts/guest_cleanup.sh ${DST_NAME}" >> ${MOUNTPOINT}/etc/rc.local
 echo "rm /etc/rc.local" >> ${MOUNTPOINT}/etc/rc.local
 echo "shutdown -h 0" >> ${MOUNTPOINT}/etc/rc.local
 echo "exit 0" >> ${MOUNTPOINT}/etc/rc.local
 chmod +x ${MOUNTPOINT}/etc/rc.local
 # ssh keys
 mkdir ${MOUNTPOINT}/root/.ssh
 cat /root/scripts/login_id_rsa.pub >> ${MOUNTPOINT}/root/.ssh/authorized_keys
 mkdir ${MOUNTPOINT}/home/kale/.ssh
 cat /root/scripts/login_id_rsa.pub >> ${MOUNTPOINT}/home/kale/.ssh/authorized_keys
 chown -R kale:kale ${MOUNTPOINT}/home/kale/
 # unmount and cleanup
 umount ${MOUNTPOINT}
 vgchange -an vg_os
 losetup -d /dev/loop0
 # cleanup domain
 virt-xml ${DST_NAME} --edit --graphics password=${DST_NAME}
 #virt-xml ${DST_NAME} --remove-device --graphics all
 virsh setmaxmem ${DST_NAME} --size ${DST_MEMORY} --config
 virsh setmem ${DST_NAME} --size ${DST_MEMORY} --config
 virsh start ${DST_NAME}
else
 echo "${0}: refusing to overwrite existing domain"
fi
Line 38: Line 101:

Make a clone.
{{{
./domain_clone_bullseye.sh clone
}}}

== Memory ==
Increase or decrease amount of memory by [[Domain Editing|editing the domain]].

== Network ==
For your convenience add the MAC address to the DHCP servers list of fixed IP-adresses before starting the system for the first time. This way you know beforehand which IP-address to connect to.

== Guest OS ==
Continue by configuring the [[KVM Guest Operating System|KVM Guest Operating System]].

Domain Cloning

Once a template system has been installed, that system can be cloned. virt-clone will automatically change the MAC address of your network device.

Cloning

Use the script below to clone your template.

domain_clone_bullseye.sh

   1 #!/bin/sh
   2 
   3 SRC_VOLUME_GROUP="vg_storage"
   4 DST_VOLUME_GROUP="vg_storage"
   5 SRC_NAME="bullseye-template"
   6 SRC_VOLUME="kvm_${SRC_NAME}_vda"
   7 SRC_VOLUME_SIZE=`lvs /dev/${SRC_VOLUME_GROUP}/${SRC_VOLUME} -o LV_SIZE --noheadings --units B --nosuffix`
   8 DST_NAME="bullseye-clone"
   9 DST_VOLUME="kvm_${DST_NAME}_vda"
  10 DST_MEMORY="256M"
  11 MOUNTPOINT="/mnt/domain_root"
  12 MAC=""
  13 
  14 if [ ! -e /sbin/kpartx ]
  15 then
  16         apt-get install -y kpartx
  17 fi
  18 
  19 if [ $# -gt 0 ]
  20 then
  21         DST_NAME=${1}
  22         DST_VOLUME="kvm_${DST_NAME}_vda"
  23 fi
  24 
  25 if [ "${2}" != "" ]
  26 then
  27         MAC="--mac ${2} "
  28 fi
  29 
  30 if [ ! -e /dev/${DST_VOLUME_GROUP}/${DST_VOLUME} ]
  31 then
  32         # create lvm partition
  33         lvcreate --yes --size ${SRC_VOLUME_SIZE}B --name ${DST_VOLUME} ${DST_VOLUME_GROUP}
  34 
  35         # clone the domain
  36         virt-clone \
  37                 --original ${SRC_NAME} \
  38                 --name ${DST_NAME} \
  39                 --file /dev/${DST_VOLUME_GROUP}/${DST_VOLUME} \
  40                 ${MAC} \
  41                 --check path_in_use=off,path_exists=off
  42 
  43         # mount the domains / partition
  44         OFFSET=`kpartx /dev/${DST_VOLUME_GROUP}/${DST_VOLUME} | tail -n1|awk {'print $6'}`
  45         OFFSET_BYTES="$((${OFFSET} * 512))"
  46         losetup -o ${OFFSET_BYTES} /dev/loop0 /dev/${DST_VOLUME_GROUP}/${DST_VOLUME}
  47         pvscan
  48         vgscan
  49         vgchange -ay vg_os
  50         if [ ! -e ${MOUNTPOINT} ]
  51         then
  52                 mkdir -p ${MOUNTPOINT}
  53         fi
  54         mount -o subvol=@rootfs /dev/vg_os/root ${MOUNTPOINT}
  55         # manipulate domain
  56         mkdir ${MOUNTPOINT}/root/scripts
  57         cp /root/scripts/guest_cleanup.sh ${MOUNTPOINT}/root/scripts
  58         # hostname
  59         cp ${MOUNTPOINT}/etc/hosts ${MOUNTPOINT}/etc/hosts.kk
  60         sed -i "s/${SRC_NAME}/${DST_NAME}/g" ${MOUNTPOINT}/etc/hosts
  61         cp ${MOUNTPOINT}/etc/hostname ${MOUNTPOINT}/etc/hostname.kk
  62         sed -i "s/${SRC_NAME}/${DST_NAME}/g" ${MOUNTPOINT}/etc/hostname
  63 
  64         # rc.local
  65         echo "#!/bin/sh" > ${MOUNTPOINT}/etc/rc.local
  66         # this delay is needed for the network to settle, works with 32, but using 64 to be sure
  67         #echo "sleep 64" >> ${MOUNTPOINT}/etc/rc.local
  68         echo "/root/scripts/guest_cleanup.sh ${DST_NAME}" >> ${MOUNTPOINT}/etc/rc.local
  69         echo "rm /etc/rc.local" >> ${MOUNTPOINT}/etc/rc.local
  70         echo "shutdown -h 0" >> ${MOUNTPOINT}/etc/rc.local
  71         echo "exit 0" >> ${MOUNTPOINT}/etc/rc.local
  72         chmod +x ${MOUNTPOINT}/etc/rc.local
  73         # ssh keys
  74         mkdir ${MOUNTPOINT}/root/.ssh
  75         cat /root/scripts/login_id_rsa.pub >> ${MOUNTPOINT}/root/.ssh/authorized_keys
  76         mkdir ${MOUNTPOINT}/home/kale/.ssh
  77         cat /root/scripts/login_id_rsa.pub >> ${MOUNTPOINT}/home/kale/.ssh/authorized_keys
  78         chown -R kale:kale ${MOUNTPOINT}/home/kale/
  79         # unmount and cleanup
  80         umount ${MOUNTPOINT}
  81         vgchange -an vg_os
  82         losetup -d /dev/loop0
  83         # cleanup domain
  84         virt-xml ${DST_NAME} --edit --graphics password=${DST_NAME}
  85         #virt-xml ${DST_NAME} --remove-device --graphics all
  86         virsh setmaxmem ${DST_NAME} --size ${DST_MEMORY} --config
  87         virsh setmem ${DST_NAME} --size ${DST_MEMORY} --config
  88         virsh start ${DST_NAME}
  89 else
  90         echo "${0}: refusing to overwrite existing domain"
  91 fi

Make a clone.

./domain_clone_bullseye.sh clone

Memory

Increase or decrease amount of memory by editing the domain.

Network

For your convenience add the MAC address to the DHCP servers list of fixed IP-adresses before starting the system for the first time. This way you know beforehand which IP-address to connect to.

Guest OS

Continue by configuring the KVM Guest Operating System.

None: Domain Cloning (last edited 2021-12-31 14:57:50 by Kristian Kallenberg)