Visualizzazione post con etichetta kvm. Mostra tutti i post
Visualizzazione post con etichetta kvm. Mostra tutti i post

mercoledì 13 febbraio 2013

Physical to virtual (P2V) GNU/Linux step by step

Hi,
I'm trying to migrate from physical machine to virtual machine GNU/Linux with KVM, continuing keeping running the physical machine during the virtualization (and thus minimization the downtime).
(still working in progress, but...)

Script to create the virtual disk with 2 partitions:

#!/bin/bash
set -e
if [ ! $UID -eq 0 ] ; then
 echo "You are not root"
 exit 1
fi
DEFAULT=5 # GB
ROOTSIZEGB=${1:-$DEFAULT}
ROOTSIZEB=$((ROOTSIZEGB * 1024 * 1024 * 1024 )) # bytes
ROOTSIZEs=$((ROOTSIZEB  / 512                )) # sector

SWAPSIZEGB=4 # GB
SWAPSIZEB=$((SWAPSIZEGB * 1024 * 1024 * 1024 )) # bytes
SWAPSIZEs=$((SWAPSIZEB  / 512                )) # sector

# 4K alignment:
#OFFSETKB=64 # KB
OFFSETKB=1024 # KB
OFFSETB=$((OFFSETKB * 1024 )) # bytes
OFFSETs=$((OFFSETB  / 512  )) # sector

DEFAULT=1
N=${2:-$DEFAULT}
LOOP=/dev/loop$N
VDISK=disk-0$N.bin
LOGFILE=$VDISK.log

if [ -f $VDISK ] ; then
   echo "$VDISK already exists!"
   echo "usage: $0 GBsize LOOPdevice"
   ls -lh $VDISK
   mount | grep $LOOP
   echo "# umount /mnt/$VDISK"
   losetup -a
   echo "# losetup -d $LOOP"
   exit 1
fi

echo "Virtual file system : $VDISK : $ROOTSIZEGB GB / + $SWAPSIZEGB swap mounted on $LOOP"
echo "Ok? (CTRL-C to quit, ENTER to continue)"
read ANSWER

CMD="qemu-img create -f raw $VDISK $((OFFSETB + ROOTSIZEB + SWAPSIZEB))"
echo "--------" | tee -a $LOGFILE
echo $CMD | tee -a $LOGFILE
echo "--------" | tee -a $LOGFILE
$CMD

#CMD="$OFFSETs,$ROOTSIZEs,83\n$((OFFSETs + ROOTSIZEs)),$((SWAPSIZEs - OFFSETs)),82"
CMD="$OFFSETs,$ROOTSIZEs,83\n$((OFFSETs + ROOTSIZEs)),,82"
echo "--------" | tee -a $LOGFILE
echo -e "$CMD  | sfdisk -S 32 -uS $VDISK" | tee -a $LOGFILE
echo "--------" | tee -a $LOGFILE
echo -e "$CMD" | sfdisk -S 32 -uS $VDISK  > /dev/null 2>&1

# check:
echo "--------" | tee -a $LOGFILE
sfdisk -l -S 32 -uS $VDISK | tee -a $LOGFILE
echo "--------" | tee -a $LOGFILE

LOOPSWAP=/dev/loop7
CMD="losetup --offset $(( OFFSETB + ROOTSIZEB )) $LOOPSWAP $VDISK"
echo -e "--------\n$CMD\n--------" | tee -a $LOGFILE
$CMD
mkswap $LOOPSWAP
sleep 1
losetup -d $LOOPSWAP

mkdir -p /mnt/$VDISK
CMD="losetup --offset $OFFSETB $LOOP $VDISK"
echo -e "--------\n$CMD\n--------" | tee -a $LOGFILE
$CMD

CMD="mkfs.ext4 -O uninit_bg,filetype,extent,dir_index -L KVM $LOOP"
echo -e "--------\n$CMD\n--------" | tee -a $LOGFILE
$CMD > /dev/null 2>&1

CMD="mount $LOOP /mnt/$VDISK"
echo -e "--------\n$CMD\n--------" | tee -a $LOGFILE
$CMD

#grub-install --force --boot-directory=/mnt/test-sda/ --no-floppy $LOOP
echo "At the end...:"
echo "umount $LOOP ; losetup -d $LOOP" | tee -a $LOGFILE

Clone physical machine:

cd /mnt/$VDISK
rsync -e ssh -au -numeric-ids --stats -h --exclude /proc --exclude /tmp --exclude /dev --exclude /sys root@REMOTEHOST:/ /mnt/$VDISK/

Chroot to VM:

mount --bind /dev/ /mnt/$VDISK/dev/
mount -t devpts pts /mnt/$VDISK/dev/pts/
mount -t tmpfs none /mnt/$VDISK/dev/shm/
mount -t proc none /mnt/$VDISK/proc/
mount -t sysfs sys /mnt/$VDISK/sys/
chroot /mnt/test/sda

Fix mtab and fstab:

vim /etc/mtab
vim /etc/fstab

Rebuild initramdisk:

mkinitrd ...

Install grub:

grub-install $LOOP

Umounting virtual image:

exit
cd ..
umount /mnt/$VDISK/sys/
umount /mnt/$VDISK/proc/
umount /mnt/$VDISK/dev/shm
umount /mnt/$VDISK/dev/pts
umount /mnt/$VDISK/dev/
umount /mnt/$VDISK/
losetup -d $LOOP

Run virtual machine:

kvm -name VMclone -m 4096 -cpu kvm32 -monitor stdio -kernel boot/vmlinuz -append "root=/dev/sda1" -initrd initrd.img $VDISK

grub:

linux /boot/vmlinux... root=/dev/sda1
initrd /boot/initrd....
boot

lunedì 8 novembre 2010

virt-manager: installazione GNU/Linux Ubuntu direttamente via network

Ciao,
giusto un appunto:
per installare direttamente via network da virt-manager basta selezionare "Network Install (HTTP, FTP, or NFS)" come sorgente dell'installer e dare come URL quello che termina con installer-i386 o installer-amd64
Esempio:
URL: http://na.mirror.garr.it/mirrors/ubuntu-archive/dists/natty/main/installer-i386/
dopo di che tutto procede in automatico.

lunedì 27 settembre 2010

conversione vmware vmdk -> kvm/qemu img


ciao
leggendo quà e là ho trovato il modo di convertire i vari file di vmware di una immagine in un unico file .img compatibile con KVM/QEMU

------------------------8<------------------------8<------------------------8<------------------------
#!/bin/bash
:> lista-raw.txt
for file in *-f???.vmdk ; do
   name=${file%%.*}
   echo "$file -> $name.raw"
   echo "$name" >> lista-raw.txt
   qemu-img convert $file -O raw $name.raw
done

for file in $(cat lista-raw.txt) ; do
   echo "$file.raw ->> KVM-QEMU.img"
   cat $file.raw >> KVM-QEMU.img
done
------------------------8<------------------------8<------------------------8<------------------------

dopo di che basta lanciare:
./kvm -hda KVM-QEMU.img

that's all.