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

From ArchWiki
Jump to: navigation, search
m (Create the container: grammar)
(remove beginners' guide link, see Talk:Installation guide#The Great Merge)
 
(35 intermediate revisions by 10 users not shown)
Line 1: Line 1:
 
[[Category:Getting and installing Arch]]
 
[[Category:Getting and installing Arch]]
 
[[Category:Virtualization]]
 
[[Category:Virtualization]]
{{Article summary start}}
+
[[ja:既存環境を仮想マシンに (から) 移動]]
{{Article summary text|Instructions on transferring your current install in or out of a virtual machine.}}
+
{{Related articles start}}
{{Article summary heading|Related}}
+
{{Related|VirtualBox}}
{{Article summary wiki|VirtualBox}}
+
{{Related|VMware}}
{{Article summary wiki|VMware}}
+
{{Related|QEMU}}
{{Article summary wiki|QEMU}}
+
{{Related|Migrate installation to new hardware}}
{{Article summary wiki|Migrate installation to new hardware}}
+
{{Related articles end}}
{{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 and possibly adjusting the fstab – especially if it's an [[SSD]].
+
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 and possibly adjusting the fstab – especially if it is an [[SSD]].
 
+
While any Linux [[File_Systems|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 VM ==
Line 20: Line 17:
 
=== Set up a shared folder ===
 
=== Set up a shared folder ===
  
This part is specific to each program, so please visit their wiki page.
+
Setting up a shared folder between the guest virtual machine and the host depends on the hypervisor you use. Please thus refer to their specific wiki page or manual.
 +
 
 +
If you do not already have an ext4 partition, see [[File systems]].
  
If you don't already have an ext4 partition, see [[Beginners'_Guide#Prepare_the_storage_drive|Prepare the storage drive]] from the Beginners' Guide.
+
{{Accuracy|Ext2Fsd 0.66 does not support extended attributes and ACL.}}
  
If you're on Windows, install [http://www.ext2fsd.com/ Ext2Fsd] to be able to mount it.
+
If you are on Windows, install [http://www.ext2fsd.com/ Ext2Fsd] to be able to mount ext volumes.
  
 
=== Transfer the system ===
 
=== Transfer the system ===
  
From the virtual machine, open a terminal and [[Full_System_Backup_with_rsync|transfer]] the system:
+
From the virtual machine, open a terminal and [[Full system backup with rsync|transfer]] the system:
  
 
  # rsync -aAXv /* /path/to/shared/folder --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs}
 
  # rsync -aAXv /* /path/to/shared/folder --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/home/*/.gvfs}
Line 34: Line 33:
 
=== Chroot and reinstall the bootloader ===
 
=== Chroot and reinstall the bootloader ===
  
Boot a "live" Linux distribution, mount the root partition and [[chroot]] into it:
+
Boot a "live" GNU/Linux distribution, mount the root partition and [[chroot]] into it.
  
# mount /dev/sdb2 /mnt
+
Reinstall your bootloader/boot manager: either [[Syslinux]], [[GRUB]] or [[Gummiboot]]. Do not forget to update the configuration file: {{ic|syslinux.cfg}} for Syslinux, {{ic|grub.cfg}} for Grub, or the Gummiboot boot entries located in {{ic|/boot/loader/entries/}}.
# arch-chroot /mnt
+
 
+
Reinstall either Syslinux or GRUB, using the [[Beginners'_Guide#Install_and_configure_a_bootloader|instructions]] from the Beginners' Guide. Don't forget to update the configuration file (i.e. {{ic|syslinux.cfg}} or {{ic|grub.cfg}}).
+
  
 
=== Adjust the fstab ===
 
=== Adjust the fstab ===
  
Since your entire root tree has been transferred to a single partition, edit the [[fstab]] file to reflect the right partition(s):
+
Since your entire root tree has been transferred to a single partition, edit the [[fstab|/etc/fstab]] file to reflect the right partition(s).
 
+
# nano /etc/fstab
+
  
 
Check with the {{ic|blkid}} command, since {{ic|lsblk}} is not very useful inside a chroot.
 
Check with the {{ic|blkid}} command, since {{ic|lsblk}} is not very useful inside a chroot.
Line 51: Line 45:
 
=== Re-generate the initramfs image ===
 
=== Re-generate the initramfs image ===
  
Because the hardware has changed, while you're still in the chroot, re-generate the initramfs image:
+
Because the hardware has changed, while you are still in the chroot, re-generate the initramfs image:
  
 
  # mkinitcpio -p linux  
 
  # mkinitcpio -p linux  
  
And that's about it.
+
And that is about it.
  
You'll most likely need to set up the network, since the virtual machine was probably piggybacking on the host OS's network settings. See [[Beginners'_Guide#Configure_the_network|Configure the network]] from the Beginners' Guide.
+
You will most likely need to set up the network, since the virtual machine was probably piggybacking on the host OS's network settings. See [[Network configuration]].
  
 
== Moving into a VM  ==
 
== Moving into a VM  ==
Line 69: Line 63:
 
  # dd if=/dev/zero of=/media/Backup/backup.img bs=1024 count=10482381
 
  # 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 {{ic|fdisk -l}} and use the value from the {{ic|Blocks}} column. Note that you will transfer you entire root tree, so that includes the {{ic|/boot}} and {{ic|/home}} folders. If you have any separate partitions for those, you need to take them into account when creating the container.
+
{{Tip| Using {{ic|fallocate}} is much faster:
 +
 
 +
# fallocate -l 10GiB -o 1024 /media/Backup/backup.img
 +
}}
 +
 
 +
If you want to create one the exact size of your root partition, run {{ic|fdisk -l}} and use the value from the {{ic|Blocks}} column for the {{ic|1=count=}} parameter. Note that you will transfer your entire root tree, so that includes the {{ic|/boot}} and {{ic|/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 {{ic|/dev/loop5}} (for example):
 
Now load the necessary module and mount it as a loopback device, on {{ic|/dev/loop5}} (for example):
Line 76: Line 75:
 
  # losetup /dev/loop5 /media/Backup/backup.img
 
  # losetup /dev/loop5 /media/Backup/backup.img
  
Install {{Pkg|gparted}} and slap a partition table on it (e.g. "msdos") and a filesystem (e.g. "ext4"):
+
Next, partition the {{ic|/dev/loop5}} device by running your favourite [[Partitioning#Partitioning_tools|partitioning tool]]. Create a partition table on it (e.g. {{ic|msdos}}), choose the [[partition scheme]] and create the partitions. Then create a [[file system]] on the partitions, which will appear as {{ic|/dev/loop5p1}}, {{ic|/dev/loop5p2}}, etc.
 
+
# 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.}}
+
{{Note|If you use the command-line parted instead of gparted, don't forget to leave 1 or 2 megabytes of unpartitioned space at the start of the disk for GRUB's embedded partition.}}
+
  
 
=== Transfer the system ===
 
=== Transfer the system ===
  
Mount the loopback device and [[Full_System_Backup_with_rsync|transfer]] the system:
+
Mount the loopback device and [[Full system backup with rsync|transfer]] the system:
  
{{Note|If the container was saved somewhere other than {{ic|/mnt}} or {{ic|/media}}, don't forget to add it to the exclude list.}}
+
{{Note|If the container was saved somewhere other than {{ic|/mnt}} or {{ic|/media}}, do not forget to add it to the exclude list.}}
  
 
  # mkdir /mnt/Virtual
 
  # mkdir /mnt/Virtual
Line 105: Line 98:
 
=== Chroot and reinstall the bootloader ===
 
=== 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:
+
Connect the container to the VM, along with a Linux LiveCD (e.g. the latest Arch Linux ISO) in the VM's virtual CD-ROM, then start the VM and [[chroot]] into it:
  
 
  # mount /dev/sda1 /mnt
 
  # mount /dev/sda1 /mnt
  # arch-chroot /mnt
+
  # arch-chroot /mnt /bin/bash
  
Reinstall either Syslinux or GRUB, using the [[Beginners'_Guide#Install_and_configure_a_bootloader|instructions]] from the Beginners' Guide. Don't forget to update its configuration file:
+
Reinstall either [[Syslinux]] or [[GRUB]]. Do not forget to update its configuration file:
  
 
* For Syslinux, it should be {{ic|1=APPEND root=/dev/sda1 ro}} in {{ic|syslinux.cfg}}.
 
* For Syslinux, it should be {{ic|1=APPEND root=/dev/sda1 ro}} in {{ic|syslinux.cfg}}.
  
* For GRUB, it's recommended that you automatically re-generate a {{ic|grub.cfg}}.
+
* For GRUB, it is recommended that you automatically re-generate a {{ic|grub.cfg}}.
  
 
=== Adjust the fstab ===
 
=== Adjust the fstab ===
Line 120: Line 113:
 
Since your entire root tree has been transferred to a single partition, edit the [[fstab]] file. You may use the UUID or label if you want, but those are more useful in multi-drive, multi-partition configurations (to avoid confusions). For now, {{ic|/dev/sda1}} for your entire system is just fine.
 
Since your entire root tree has been transferred to a single partition, edit the [[fstab]] file. You may use the UUID or label if you want, but those are more useful in multi-drive, multi-partition configurations (to avoid confusions). For now, {{ic|/dev/sda1}} for your entire system is just fine.
  
{{hc|# nano /etc/fstab|
+
{{hc|/etc/fstab|
 
tmpfs                    /tmp      tmpfs    nodev,nosuid          0  0
 
tmpfs                    /tmp      tmpfs    nodev,nosuid          0  0
 
/dev/sda1                /        ext4      defaults,noatime      0  1}}
 
/dev/sda1                /        ext4      defaults,noatime      0  1}}
Line 126: Line 119:
 
=== Disable any Xorg-related files ===
 
=== Disable any Xorg-related files ===
  
Having an {{ic|nvidia}}, {{ic|nouveau}}, {{ic|radeon}}, {{ic|intel}}, etc., entry in the {{ic|Device}} section from one of the Xorg configuration files will prevent it from starting, since you will be using ''emulated'' hardware (including the video card). So it's recommended that you move/rename or delete the following:
+
Having an {{ic|nvidia}}, {{ic|nouveau}}, {{ic|radeon}}, {{ic|intel}}, etc., entry in the {{ic|Device}} section from one of the Xorg configuration files will prevent it from starting, since you will be using ''emulated'' hardware (including the video card). So it is recommended that you move/rename or delete the following:
  
 
  # mv /etc/X11/xorg.conf /etc/X11/xorg.conf.bak
 
  # mv /etc/X11/xorg.conf /etc/X11/xorg.conf.bak
Line 133: Line 126:
 
=== Re-generate the initramfs image ===
 
=== Re-generate the initramfs image ===
  
Because the hardware has changed, while you're still in the chroot, re-generate the initramfs image and do a proper shutdown:
+
Because the hardware has changed, while you are still in the chroot, re-generate the initramfs image and do a proper shutdown:
  
 
  # mkinitcpio -p linux
 
  # mkinitcpio -p linux
 +
# exit
 +
# umount -R /mnt
 
  # poweroff
 
  # poweroff
  
Finally, pull out the LiveCD (the ISO file) and start the virtual machine.
+
Finally, pull out the LiveCD (the ISO file), so that you don't boot back into it, and start the virtual machine.
 
+
{{Note|At this point you may notice that you no longer have a wallpaper. Don't worry about it. It's most likely because it is located on a different partition mounted in {{ic|/media}} or {{ic|/mnt}}, folders which were excluded from the transfer.}}
+
  
 
Enjoy your new virtual environment.
 
Enjoy your new virtual environment.
  
== Troubleshoot ==
+
== Troubleshooting ==
  
 
=== "mount: special device /dev/loop5p1 does not exist" ===
 
=== "mount: special device /dev/loop5p1 does not exist" ===
Line 172: Line 165:
 
  You are being dropped to a recovery shell
 
  You are being dropped to a recovery shell
 
     Type 'exit' to try and continue booting
 
     Type 'exit' to try and continue booting
  sh: can't access tty; job control turned off
+
  sh: cannot access tty; job control turned off
 
  [rootfs /]# _
 
  [rootfs /]# _
  
It most likely means that you didn't run {{ic|poweroff}} like ''you were instructed to'', and closed the VM with the "close" button, which is the equivalent of a power outage. Now you need to regenerate your initramfs image. To do that, you can start the VM using the Fallback entry. If you don't have a Fallback entry, press {{Keypress|Tab}} (for Syslinux) or {{Keypress|e}} (for GRUB) and rename it {{ic|initramfs-linux-fallback.img}}. After it boots, open up a terminal and run:
+
It most likely means that you did not run {{ic|poweroff}} like ''you were instructed to'', and closed the VM with the "close" button, which is the equivalent of a power outage. Now you need to regenerate your initramfs image. To do that, you can start the VM using the Fallback entry. If you do not have a Fallback entry, press {{ic|Tab}} (for Syslinux) or {{ic|e}} (for GRUB) and rename it {{ic|initramfs-linux-fallback.img}}. After it boots, open up a terminal and run:
  
 
  # mkinitcpio -p linux
 
  # mkinitcpio -p linux
Line 182: Line 175:
 
=== "Missing operating system. FATAL: INT18: BOOT FAILURE" ===
 
=== "Missing operating system. FATAL: INT18: BOOT FAILURE" ===
  
You will need to install (reinstall) a bootloader. See the [[Beginners'_Guide#Install_and_configure_a_bootloader|instructions]] from the Beginners' Guide.
+
* You either need to install or reinstall a bootloader. See [[Boot loaders]].
 +
 
 +
* You are using a [[Btrfs]] filesystem with compression for {{ic|/boot}}, for which [[Syslinux]] currently cannot boot from.
  
Also, check the boot order from the BIOS or from the VM's settings and make sure that the drive containing the bootloader is the first to boot.
+
* The boot order from the BIOS or from the VM's settings is not properly set up. Make sure that the drive containing the bootloader is the first one to boot.
  
 
=== I'm asked for the root password, for maintenance ===
 
=== I'm asked for the root password, for maintenance ===

Latest revision as of 23:55, 26 August 2016

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 and possibly adjusting the fstab – especially if it is an SSD.

Moving out of a VM

Moving out of a virtual environment is relatively easy.

Set up a shared folder

Setting up a shared folder between the guest virtual machine and the host depends on the hypervisor you use. Please thus refer to their specific wiki page or manual.

If you do not already have an ext4 partition, see File systems.

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: Ext2Fsd 0.66 does not support extended attributes and ACL. (Discuss in Talk:Moving an existing install into (or out of) a virtual machine#)

If you are on Windows, install Ext2Fsd to be able to mount ext volumes.

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" GNU/Linux distribution, mount the root partition and chroot into it.

Reinstall your bootloader/boot manager: either Syslinux, GRUB or Gummiboot. Do not forget to update the configuration file: syslinux.cfg for Syslinux, grub.cfg for Grub, or the Gummiboot boot entries located in /boot/loader/entries/.

Adjust the fstab

Since your entire root tree has been transferred to a single partition, edit the /etc/fstab file to reflect the right partition(s).

Check with the blkid command, since lsblk is not very useful inside a chroot.

Re-generate the initramfs image

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

# mkinitcpio -p linux 

And that is about it.

You will most likely need to set up the network, since the virtual machine was probably piggybacking on the host OS's network settings. See Network configuration.

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
Tip: Using fallocate is much faster:
# fallocate -l 10GiB -o 1024 /media/Backup/backup.img

If you want to create one the exact size of your root partition, run fdisk -l and use the value from the Blocks column for the count= parameter. Note that you will transfer your 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

Next, partition the /dev/loop5 device by running your favourite partitioning tool. Create a partition table on it (e.g. msdos), choose the partition scheme and create the partitions. Then create a file system on the partitions, which will appear as /dev/loop5p1, /dev/loop5p2, etc.

Transfer the system

Mount the loopback device and transfer the system:

Note: If the container was saved somewhere other than /mnt or /media, do not 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}

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

Connect the container to the VM, along with a Linux LiveCD (e.g. the latest Arch Linux ISO) in the VM's virtual CD-ROM, then start the VM and chroot into it:

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

Reinstall either Syslinux or GRUB. Do not forget to update its configuration file:

  • For Syslinux, it should be APPEND root=/dev/sda1 ro in syslinux.cfg.
  • For GRUB, it is recommended that you automatically re-generate a grub.cfg.

Adjust the fstab

Since your entire root tree has been transferred to a single partition, edit the fstab file. You may use the UUID or label if you want, but those are more useful in multi-drive, multi-partition configurations (to avoid confusions). For now, /dev/sda1 for your entire system is just fine.

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

Disable any Xorg-related files

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

# 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 are still in the chroot, re-generate the initramfs image and do a proper shutdown:

# mkinitcpio -p linux
# exit
# umount -R /mnt
# poweroff

Finally, pull out the LiveCD (the ISO file), so that you don't boot back into it, and start the virtual machine.

Enjoy your new virtual environment.

Troubleshooting

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

First, check the loopback device with fdisk for the starting block:

# 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

Then use it as an offset when mounting it:

# mount -o offset=$((2048 * 512)) /dev/loop5 /mnt/Virtual/

"Waiting 10 seconds for device /dev/sda1; ERROR: Unable to find root device '/dev/sda1'"

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

It most likely means that you did not run poweroff like you were instructed to, and closed the VM with the "close" button, which is the equivalent of a power outage. Now you need to regenerate your initramfs image. To do that, you can start the VM using the Fallback entry. If you do not have a Fallback entry, press Tab (for Syslinux) or e (for GRUB) and rename it initramfs-linux-fallback.img. After it boots, open up a terminal and run:

# mkinitcpio -p linux
# poweroff

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

  • You either need to install or reinstall a bootloader. See Boot loaders.
  • You are using a Btrfs filesystem with compression for /boot, for which Syslinux currently cannot boot from.
  • The boot order from the BIOS or from the VM's settings is not properly set up. Make sure that the drive containing the bootloader is the first one to boot.

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 blkid.