I was assigned the task of transferring a site from external hosting to a client server. The peculiarity was that the virtual server was well configured, and really did not want to reinstall and configure the operating system, database and all environment packages.
The site was spinning in the cloud of a famous provider on a virtual machine with the XEN hypervisor. I was hoping that the support would meet and give the image of a virtual machine, but the miracle did not happen: the refusal was cold and courteous, probably, as usual. As a result, an approximate transfer scheme was born: make a disk image, transfer it to your server, create a virtual machine, specify the resulting image as the source.
Experienced experts already guess what I had to face in the process of executing the plan. For the rest I cite details and ways to solve the arisen difficulties
1. Connecting to your server
Since making a disk image on this disk itself is rather troublesome, it was necessary to connect a file system that is different from the one used. The first thing that came to mind was to mount the home folder of your server in the file system on the virtual machine using sshfs. I bring the commands for the Debian distribution:
')
# apt-get install sshfs
# mkdir / mnt / vasya
# sshfs vasya@pupkin.ru: / home / vasya / mnt / vasya
Password: Cheburashka 2014
2. Translation of FS in read-only
In order for the file system not to lose its integrity, it was necessary to transfer it to write protection mode (read-only). To do this, Linux provides the ability to remount the FS:
mount -o ro,remount /
. The command naturally did not work, the kernel blocked the execution of the command with a
mount: / is busy
error
mount: / is busy
. After some googling, there was a reason: many files were opened by demons. On the server, I had to stop ntp, apache2, mysql, rsyslogd (maybe I missed something, but you can see the list of running processes using the htop or top command).
# invoke-rc.d ntp stop
# invoke-rc.d apache2 stop
# invoke-rc.d mysql stop
# invoke-rc.d rsyslogd stop
# mount -o ro, remount /
# touch / test
touch: cant touch `/ test ': Read-only file system
3. Image creation and transfer
To create an image, I used the dd program. I also used the data pipeline and sent them through the gzip compression program to a file located in a folder that is mounted on a remote server.
# dd if = / dev / xvda bs = 64K | gzip -c> /mnt/vasya/xvda.raw.gz
After an hour and a half I had the file /home/vasya/xvda.raw.gz on my server with the image I needed. The compression ratio turned out to be quite good: 8.5 / 12.
4. Connecting the image to the new virtual machine
On local hardware I use Proxmox VE 3.1. I created a virtual machine with the following configuration:
balloon: 256
bootdisk: virtio0
cores: 2
ide2: nfs-da: iso / debian-7.2.0-amd64-netinst.iso, media = cdrom, size = 222M
memory: 768
name: pupkin
net0: virtio = 26: 95: 81: 17: D9: 14, bridge = vmbr0
ostype: l26
sockets: 1
virtio0: nfs-da: 143 / vm-143-disk-1.raw, format = raw
I, of course, unzipped the image file of the virtual machine and put it in the right place with the necessary access rights (/ mnt / nfs is a section configured as a Proxmox VE storage, 143 is the identifier of the new virtual machine):
# zcat /home/vasya/xvda.raw.gz> /mnt/nfs/images/143/vm-143-disk-1.raw
# chown nobody: nogroup /mnt/nfs/images/143/vm-143-disk-1.raw
Due to my inexperience, I was looking forward to the victory, but this was not the case: the Grub bootloader dropped out into its console. Moreover, attempts to boot manually were doomed to failure: the resulting image lacked the initrd environment initializer, and the kernel was highly specialized and did not contain drivers for virtio devices (network and hard disk). Experienced specialists can add me and explain how Linux booting is configured in XEN, I didn’t have a case before, I already had a new plan: I need to put a linux kernel, its drivers and an initrd from a running virtual machine with a similar operating system.
5. Bootloader fix
I had a working virtual machine, to which I added a new disk, and then deleted its image from the file system and replaced it with a symlink for a broken image. Previously, I, of course, turned off the virtual 143, so that the image does not accidentally open 2 processes simultaneously.
# rm /mnt/nfs/images/200/vm-200-disk-2.raw
# ln -s /mnt/nfs/images/143/vm-143-disk-1.raw /mnt/nfs/images/200/vm-200-disk-2.raw
I downloaded the virtual 200, mounted the FS of the broken image and copied the linux kernel, the working bootloader and the device drivers there.
# mkdir / mnt / vdb1
# mount / dev / vdb1 / mnt / vdb1
# cp /boot/vmlinuz-3.2.0-4-amd64 /mnt/vdb1/boot/vmlinuz-3.2.0-4-amd64
# cp /boot/initrd.img-3.2.0-4-amd64 /mnt/vdb1/boot/initrd.img-3.2.0-4-amd64
# cp /boot/System.map-3.2.0-4-amd64 /mnt/vdb1/boot/System.map-3.2.0-4-amd64
# cp -r /lib/modules/3.2.0-4-amd64 / mnt / vdb1 / lib / modules
I turned off 200, turned on 143 and entered the Grub manual boot commands:
grub> root (hd0, msdos1)
grub> linux /boot/vmlinuz-3.2.0-4-amd64 root = / dev / vda1
grub> initrd /boot/initrd.img-3.2.0-4-amd64
grub> boot
The system booted and even successfully started mysql and apache2. Of course, I had to fix
/etc/network/interfaces
so that the system went to the local network and was able to communicate with the router and the Internet.
On a fully working machine, there were no further problems: I started the grub installation and installed the linux kernel from the repository to finally straighten the bootloader and follow the debian-way:
# grub-install / dev / vda
# apt-get update
# apt-get install linux-image-2.6.32-5-486
# reboot
Conclusion
As a result of all the manipulations, I got a virtual machine, almost identical to the original, but working in the Proxmox VE environment with the KVM hypervisor and not requiring a special bootloader. The image is scheduled to be transferred to the client to run in a VMware ESX cluster.