Guest User

Physical to virtual GNU/Linux P2V

a guest
Feb 16th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Physical to Virtual GNU/Linux
  4. # @ Paolo Pedaletti
  5. # this is free software
  6. #
  7.  
  8. # change this when you have done:
  9. DEBUG=""
  10. DEBUG="echo "
  11.  
  12. ROOTSIZEGB="20"
  13. ROOTSIZEB=$((ROOTSIZEGB * 1024 * 1024 * 2))
  14. SWAPSIZEGB="4"
  15. SWAPSIZEB=$((SWAPSIZEGB * 1024 * 1024 * 2))
  16. OFFSET=2048
  17. VDISK=/home/kvm/test/sda.bin
  18. $DEBUG qemu-img create -f raw $VDISK $((ROOTSIZEGB + SWAPSIZEGB))G
  19. CMD="$OFFSET,$ROOTSIZEB,83\n$((ROOTSIZEB + OFFSET)),$((SWAPSIZEB - OFFSET)),82"
  20. echo -e "sfdisk : $CMD"
  21. echo -e "$CMD" | sfdisk -uS $VDISK
  22. $DEBUG sfdisk -l $VDISK
  23. $DEBUG mkdir /mnt/test-sda
  24. $DEBUG losetup --offset $OFFSET /dev/loop1 $VDISK
  25. $DEBUG mkfs.ext4 /dev/loop1
  26. $DEBUG mount /dev/loop1 /mnt/test-sda
  27. $DEBUG cd /mnt/test/sda
  28. echo "clone physical machine:"
  29. $DEBUG rsync -e ssh -au --stats -h --exclude /proc --exclude /tmp --exclude /dev --exclude /sys root@REMOTEHOST:/ /mnt/test-sda/
  30. echo "chroot to VM:"
  31. $DEBUG mount --bind /dev/ /mnt/test-sda/dev/
  32. $DEBUG mount -t devpts pts /mnt/test-sda/dev/pts/
  33. $DEBUG mount -t tmpfs none /mnt/test-sda/dev/shm/
  34. $DEBUG mount -t proc none /mnt/test-sda/proc/
  35. $DEBUG mount -t sysfs sys /mnt/test-sda/sys/
  36. $DEBUG chroot /mnt/test/sda
  37. echo "fix mtab and fstab:"
  38. $DEBUG vim /etc/mtab
  39. $DEBUG vim /etc/fstab
  40. echo "rebuild initramdisk:"
  41. $DEBUG mkinitrd ...
  42. echo "install grub:"
  43. $DEBUG grub-install /dev/loop1
  44. echo "umounting virtual image:"
  45. $DEBUG exit
  46. $DEBUG cd ..
  47. $DEBUG umount /mnt/test-sda/sys/
  48. $DEBUG umount /mnt/test-sda/proc/
  49. $DEBUG umount /mnt/test-sda/dev/shm
  50. $DEBUG umount /mnt/test-sda/dev/pts
  51. $DEBUG umount /mnt/test-sda/dev/
  52. $DEBUG umount /mnt/test-sda/
  53. $DEBUG losetup -d /dev/loop1
  54. echo "run virtual machine:"
  55. $DEBUG kvm -m 4096 -cpu kvm32 -name VMclone $VDISK
  56. echo "grub:"
  57. $DEBUG linux /boot/vmlinux... boot=/dev/sda1
  58. $DEBUG initrd /boot/initrd....
  59. $DEBUG boot
  60.  
  61. echo "All correct?"
Advertisement
Add Comment
Please, Sign In to add comment