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