Difference between revisions of "Moving an existing install into (or out of) a virtual machine"

From ArchWiki
Jump to: navigation, search
(See also: + related)
m (group links)
Line 7: Line 7:
 
{{Article summary wiki|VMware}}
 
{{Article summary wiki|VMware}}
 
{{Article summary wiki|QEMU}}
 
{{Article summary wiki|QEMU}}
 +
{{Article summary wiki|Migrate installation to new hardware}}
 
{{Article summary end}}
 
{{Article summary end}}
  
Line 166: Line 167:
  
 
This means that you forgot to add the drive's UUID, label or device name in {{ic|/etc/fstab}}. The UUID is different every time you format it (or in this case, create one from scratch), and they likely do not match. Check with {{ic|lsblk -f}}.
 
This means that you forgot to add the drive's UUID, label or device name in {{ic|/etc/fstab}}. The UUID is different every time you format it (or in this case, create one from scratch), and they likely do not match. Check with {{ic|lsblk -f}}.
 
==See also==
 
*[[Migrate installation to new hardware]]
 

Revision as of 02:53, 27 September 2012

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end

This article describes how to transfer your current Arch Linux installation in or out of a virtual environment (i.e. QEMU, VirtualBox, VMware), and is heavily based on the Full System Backup with rsync article. A virtual machine ("VM", for short) uses different hardware, which needs to be addressed by re-generating the initramfs image. While any Linux filesystem should work, it's recommended that you go with ext4, at least at first, until you get the hang of it.

Moving out of a VM

Moving out of a virtual environment is relatively easy.

Set up a shared folder

Set up a Linux partition as a shared folder. This part is specific to each program, so please visit their wiki page.

If you don't already have a Linux partition, see Prepare the storage drive from the Beginners' Guide.

If you're on Windows, install Ext2Fsd to be able to use the ext4 partition as a shared folder.

Transfer the system

From the virtual machine, open a terminal and transfer the system:

# rsync -aAXv /* /path/to/shared/folder --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs}

Chroot and reinstall the bootloader

Boot a "live" Linux distribution, mount the root partition and chroot into it:

# mount /dev/sdb2 /mnt
# arch-chroot /mnt

Reinstall Syslinux or GRUB, using the instructions from the Beginners' Guide.

Adjust the fstab

Since your entire root tree has been transferred to a single partition, edit the fstab file to reflect the right partition. Check with lsblk -f.

Re-generate the initramfs image

Because the hardware has changed, while you're still in the chroot, re-generate the initramfs image:

# mkinitcpio -p linux 

Moving into a VM

Moving into a virtual environment takes a little more effort.

Create the container

This will create a 10 GB raw image:

# dd if=/dev/zero of=/media/Backup/backup.img bs=1024 count=10482381

If you want to create one the exact size of your root partition, run fdisk -l and use the value from the Blocks column. Note that you will transfer you entire root tree, so that includes the /boot and /home folders. If you have any separate partitions for those, you need to take them into account when creating the container.

Now load the necessary module and mount it as a loopback device, on /dev/loop5 (for example):

# modprobe loop
# losetup /dev/loop5 /media/Backup/backup.img

Install gparted and slap a partition table on it (e.g. "msdos") and a filesystem (e.g. "ext4"):

# pacman -S gparted
# gparted /dev/loop5
Tip: If you want, you can add however many partitions you want: home, boot, var, tmp, whatever floats your boat. Of course, it will add a layer of complexity, but it's doable. The point is that you don't necessarily need another container.

Transfer the system

Mount the loopback device and transfer the system:

Note: If the container was saved somewhere other than /mnt or /media, don't forget to add it to the exclude list.
# mkdir /mnt/Virtual
# mount /dev/loop5p1 /mnt/Virtual
# rsync -aAXv /* /mnt/Virtual --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs}

Sit back, this could take a while.

Convert the container to a compatible format

Change directory to where the loopback file is located and choose the appropriate command for your virtual machine:

$ cd /media/Backup
$ qemu-img convert -c -f raw -O qcow backup.img backup.qcow2
$ VBoxManage convertfromraw --format VDI backup.img backup.vdi
$ VBoxManage convertfromraw --format VMDK backup.img backup.vmdk

Chroot and reinstall the bootloader

Hook up the converted file from above, and the latest Arch Linux ISO into the virtual CD-ROM. Then start the virtual machine and chroot into it:

# mount /dev/sda1
# arch-chroot /mnt

Reinstall Syslinux or GRUB, using the instructions from the Beginners' Guide.

Adjust the fstab

Since your entire root tree has been transferred to a single partition, edit the fstab file to look like this:

# nano /etc/fstab
tmpfs                  /tmp          tmpfs     nodev,nosuid          0      0
/dev/sda1              /             ext4      defaults,noatime      0      1

Disable any Xorg-related files

Having an nvidia entry (or nouveau, radeon, intel, etc) in the Device section from one of the Xorg configuration files will inhibit the startx command, since you will be using emulated hardware (including the video card). So it's recommended that you move/rename or delete them:

# mv /etc/X11/xorg.conf /etc/X11/xorg.conf.bak
# mv /etc/X11/xorg.conf.d/10-monitor /etc/X11/xorg.conf.d/10-monitor.bak

Re-generate the initramfs image

Because the hardware has changed, while you're still in the chroot, re-generate the initramfs image:

# mkinitcpio -p linux

Troubleshoot

"mount: special device /dev/loop5p1 does not exist"

Check with fdisk for the starting block and use it as an offset:

# fdisk -l /dev/loop5
Disk /dev/loop5: 10.7 GB, 10733958144 bytes
255 heads, 63 sectors/track, 1304 cylinders, total 20964762 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b45e8

      Device Boot      Start         End      Blocks   Id  System
/dev/loop5p1   *        2048    20963327    10480640   83  Linux
# mount -o offset=$((2048 * 512)) /dev/loop5 /mnt/Virtual/

"Missing operating system. FATAL: INT18: BOOT FAILURE"

You will need to install (reinstall) a bootloader. See the instructions from the Beginners' Guide.

"Waiting 10 seconds for device; ERROR: Unable to find root device"

Waiting 10 seconds for device [...] ...
ERROR: Unable to find root device '[...]'.
You are being dropped to a recovery shell
    Type 'exit' to try and continue booting
sh: can't access tty; job control turned off
[rootfs /]# _

You need to regenerate your initramfs image, because the hardware has changed. To do that, you can start the VM using the Fallback entry. If you don't have a Fallback entry, press Template:Keypress (for Syslinux) or Template:Keypress (for GRUB) and rename it initramfs-linux-fallback.img. After it boots, run:

# mkinitcpio -p linux

I'm asked for the root password, for maintenance

:: Checking Filesystems                [BUSY]
fsck.ext4: Unable to resolve '...'

This means that you forgot to add the drive's UUID, label or device name in /etc/fstab. The UUID is different every time you format it (or in this case, create one from scratch), and they likely do not match. Check with lsblk -f.