[SOLVED] Virtual machine clonning

F

fellipebl

Guest
Hello users,

after a long search for a VM cloning solution and some unsuccessful tests with virt-clone we figured out a different way to clone VMs.

There are some steps to follow:

  1. Take a look in this two directories: (i) /etc/qemu-server/(VM configuration file).conf and (ii) /var/lib/vz/images/(VM storage folder).
  2. Copy the script in /var/lib/vz/images/ and execute it with four parameters: sudo sh clone_vm.sh (source VM id) (destination VM id) (destination VM name) (MAC address for the VM).

    Notice:
    the MAC address of all the VMs must be unique for avoid packet looses.

    Here goes the shell script for do it:

    Code:
    #!/bin/sh
    
    SOURCE_VM_ID=$1
    DEST_VM_ID=$2
    DEST_VM_NAME=$3
    DEST_VM_MACADD=$4
    
    # new vm physic file directory
    mkdir /var/lib/vz/images/$2
    
    # new vm configuration file directory
    echo -e "name: $DEST_VM_NAME\nide2:  local:iso/ubuntu-10.04-server-amd64.iso,media=cdrom\nvlan0:  virtio=$DEST_VM_MACADD\nbootdisk: ide0\nostype: l26\nide0:  local:$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk\nmemory: 512\nonboot:  1\nsockets: 1\ncores: 1" >> /etc/qemu-server/$DEST_VM_ID.conf
    
    # clone the physic vm
    cp /var/lib/vz/images/$SOURCE_VM_ID/vm-$SOURCE_VM_ID-disk-1.vmdk /var/lib/vz/images/$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk
  3. Verify the copy process by the web-interface.
  4. Finish and have fun.
Future job: adapt for the Proxmox web-interface. (next weeks)

Regards,
Fellipe Lima (fellipebl@gmail.com)
Hrvoje Bosnjak (hrvoje.bosnjak@gmail.com)
 
macday,
I think that the process is analogue.

ansanto,
Good job! We will look the code and test it... first I have to check about DRBD.
 
Hello
First of all, thanks for the script !

Could you help me ? I try to create some "template" (installation done) of some OS like debian,ubuntu etc for my clients with KVM.
Is it possible to boot from template with promox ? How can i do ?
Template are created by cloning a good install.
How can we edit the password root after the cloning ?
Is it possible to send command to the VM ? (like vzctl exec ) ?

Regards,
QQ
 
Last edited:
Hello
First of all, thanks for the script !

Could you help me ? I try to create some "template" (installation done) of some OS like debian,ubuntu etc for my clients with KVM.
Is it possible to boot from template with promox ? How can i do ?
Template are created by cloning a good install.
How can we edit the password root after the cloning ?
Is it possible to send command to the VM ? (like vzctl exec ) ?

Regards,
QQ
Hi,
you can mount the disk of the vm and can change something inside. Or you install a script inside, which do all changes during the first boot.

Like this way:
Code:
# apt-get install kpartx
# kpartx -av /var/lib/vz/images/113/vm-113-disk-1.raw 
add map loop0p1 (254:5): 0 497952 linear /dev/loop0 63
add map loop0p2 (254:6): 0 24659775 linear /dev/loop0 498015

# mount /dev/mapper/loop0p2 /mnt

## or if lvm:
# vgscan
# vgchange -a y name-of-vmvg
# mount /dev/mapper/name-of-vmvg-root /mnt

# grep root /mnt/etc/shadow
root:$1$ikwrPUYX$K0xvl4UdDmZtx2/h9B88b1:14710:0:99999:7:::
With sed you can change passwords or whatever... perhaps also chroot will work.

Udo
 
Ok thanks for info :) i'wil try it now.
I cloned a img.
Is it possible to boot from template (img) with promox ? How can i do ?
#!/bin/sh

SOURCE_VM_ID=$1
DEST_VM_ID=$2
DEST_VM_NAME=$3
DEST_VM_MACADD=$4
SOURCE_ISO=$5
DES_MEMORY=$6

# new vm physic file directory
mkdir /var/lib/vz/images/$2

# new vm configuration file directory
echo -e "name: $DEST_VM_NAME\nide2: local:iso/$SOURCE_ISO,media=cdrom\nvlan0: virtio=$DEST_VM_MACADD\nbootdisk: ide0\nostype: l26\nide0: local:$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk\nmemory: $DES_MEMORY=$6\nonboot: 1\nsockets: 1\ncores: 4" >> /etc/qemu-server/$DEST_VM_ID.conf

# clone the physic vm
cp /var/lib/vz/images/$SOURCE_VM_ID/vm-$SOURCE_VM_ID-disk-1.vmdk /var/lib/vz/images/$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk
So after this script i have to mount the VM and edit
/etc/shadow

Right ? :)

Is it possible to send command to the VM ? (like vzctl exec ) ?
Is it possible to enter in a VM like vzctl enter ?

i tried kpartx
root@n:~# kpartx -av /var/lib/vz/images/107/vm-106-disk-1-copy.raw
add map loop1p1 (254:1): 0 50170932 linear /dev/loop1 63
add map loop1p2 (254:2): 0 2249100 linear /dev/loop1 50170995
add map loop1p5 (254:3): 0 2249037 254:2 63
and then mount /dev/mapper/loop1p1 /mnt and it's work :)

Thanks
Regards,
 
Last edited:
Hello everybody,
I tried to modify your script to create an automatic installation

#!/bin/sh

# VM SOURCE
SOURCE_VM_ID=$1
# VM DESTINATION
DEST_VM_ID=$2
# NAME OF THE VM
DEST_VM_NAME=$3
# MAC ADRESS OF THE VM
DEST_VM_MACADD=$4
# ISO TO BOOT THE VM
SOURCE_ISO=$5
# MEMORY OF THE VM
DES_MEMORY=$6
# NUMBER OF CORES OF THE VM
DES_CORES=$7
# HOST IP
IP_DU_HOTE=$8
# VM IP
VOTRE_IP_FAILOVER=$9

# we create the folder of the vm
mkdir /var/lib/vz/images/$2

# we create the config of the vm
echo -e "name: $DEST_VM_NAME\nide2: local:iso/$SOURCE_ISO,media=cdrom\nvlan0: virtio=$DEST_VM_MACADD\nbootdisk: ide0\nostype: other\nide0: local:$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk\nmemory: $DES_MEMORY\nonboot: 1\nsockets: 1\ncores: $DES_CORES" >> /etc/qemu-server/$DEST_VM_ID.conf

# clone the physic vm
cp /var/lib/vz/images/$SOURCE_VM_ID/vm-$SOURCE_VM_ID-disk-1.vmdk /var/lib/vz/images/$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk

# now we have to mount the VM
kpartx -av /var/lib/vz/images/$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk
mount /dev/mapper/loop1p1 /mnt/

# we need to be chroot to change the network and the root password
chroot /mnt/

rm -rf /etc/network/interfaces

# config network
ifconfig eth0 $VOTRE_IP_FAILOVER/32 up
# add the route for host
ip route add $IP_DU_HOTE dev eth0
# add the default route
ip route add default via $IP_DU_HOTE dir

#we have to quit the "chroot"
exit

# umount the disk
umount /dev/mapper/loop1p1/

I tried this but .. i have two problem
I'm not able to leave the chroot with exit command in the bash .. any idea ?
How can i change the root password without prompt ?
If anybody could help to make it works :)

Regards,
QQ
 
Hello everybody,
I tried to modify your script to create an automatic installation



I tried this but .. i have two problem
I'm not able to leave the chroot with exit command in the bash .. any idea ?
How can i change the root password without prompt ?
If anybody could help to make it works :)

Regards,
QQ
Hi,
your script is a little bit dangerous. If the chroot don't work, you remove your host interface-file!!
I think it's better to remove the file before chroot (rm /mnt/etc/network/interface).
password-change without prompt:
Code:
echo "root:password" | /usr/sbin/chpasswd

Udo
 
Thanks for answer Udo

#!/bin/sh

# VM SOURCE
SOURCE_VM_ID=$1
# VM DESTINATION
DEST_VM_ID=$2
# NAME OF THE VM
DEST_VM_NAME=$3
# MAC ADRESS OF THE VM
DEST_VM_MACADD=$4
# ISO TO BOOT THE VM
SOURCE_ISO=$5
# MEMORY OF THE VM
DES_MEMORY=$6
# NUMBER OF CORES OF THE VM
DES_CORES=$7
# HOST IP
IP_DU_HOTE=$8
# VM IP
VOTRE_IP_FAILOVER=$9
#password root vm
passvm=$10

# we create the folder of the vm
mkdir /var/lib/vz/images/$2

# we create the config of the vm
echo -e "name: $DEST_VM_NAME\nide2: local:iso/$SOURCE_ISO,media=cdrom\nvlan0: virtio=$DEST_VM_MACADD\nbootdisk: ide0\nostype: other\nide0: local:$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk\nmemory: $DES_MEMORY\nonboot: 1\nsockets: 1\ncores: $DES_CORES" >> /etc/qemu-server/$DEST_VM_ID.conf

# clone the physic vm
cp /var/lib/vz/images/$SOURCE_VM_ID/vm-$SOURCE_VM_ID-disk-1.vmdk /var/lib/vz/images/$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk

# now we have to mount the VM
kpartx -av /var/lib/vz/images/$DEST_VM_ID/vm-$DEST_VM_ID-disk-1.vmdk
mount /dev/mapper/loop1p1 /mnt/

#remove network vm config
rm -rf /mnt/etc/network/interfaces

# we need to be chroot to change the network and the root password
chroot /mnt/

#we change the root password of the vm
echo "root:$passvm" | /usr/sbin/chpasswd

# config network
ifconfig eth0 $VOTRE_IP_FAILOVER/32 up
# add the route for host
ip route add $IP_DU_HOTE dev eth0
# add the default route
ip route add default via $IP_DU_HOTE dir

#we have to quit the "chroot"
exit

# umount the disk
umount /dev/mapper/loop1p1/

How can i quit the chroot ? the exit command don't work :(
How can i be sure to be chrooted ? There is an other way to do what i want to do ?

Big thanks for help !
 
Thanks for answer Udo



How can i quit the chroot ? the exit command don't work :(
How can i be sure to be chrooted ? There is an other way to do what i want to do ?

Big thanks for help !
Hi,
chroot is not the right thing for your purpose I think.
ifconfig inside chroot don't work because you don't have your vm-nic and there are nothing set permanently.
But you can build your own interfaces-file and overwrite the existing (/mnt/etc/network/interfaces).
You must also remove the /mnt/etc/udev/rules.d/70-persistent-net.rules

In your case i would cp an script inside the VM (like /mnt/etc/rc2.d/S27chpasswd) which ask for a new password (twice with comparision) and remove after that the script.

Udo
 
Thanks for answer .
I will try to do it now, if it's work i will post the script here ;)
I'm not sure what you mean with
In your case i would cp an script inside the VM (like /mnt/etc/rc2.d/S27chpasswd) which ask for a new password (twice with comparision) and remove after that the script.

The script will ask a new root password at the boot isn't it ?
The goal of the script is to send a command line by libssh and create the VM automatically.
Regards,
 
The new script that i tried :

I forgetted to kpartx -dv the img and i don't understand why the script don't rm -rf the hostname etc.
It don't do the actions :(

Can you help ?

Regards,
QQ
Hi,
you mount loop1p1 - the firstpartition is often /boot - in this case you have to mount loop1p2.
Perhaps this is the failure.

why you use "rm -rf" and not only "rm" - interfaces and so on are no directorys.

my script-start-at-firstboot is only for settings the password. You can change all things with your script and during the first boot the new password will requested.

Udo
 
Hi!

Is it possible to mount the VMDK disk as LVM or RAW, to change its contents when cloning?
Or, alternatively, whether it is possible to convert raw to vmdk after editing the raw?
 
Last edited:

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!