Difference between revisions of "VirtualBox Arch Linux Guest On Physical Drive"

From ArchWiki
Jump to: navigation, search
(Created page with 'Lots of users run a dual boot between Arch Linux and another Operating System (Windows for instance). It can be tedious to switch back and forth if you need to work in both. Usin…')
 
Line 3: Line 3:
 
This guide will help you set up your dual boot ArchLinux/Windows system so you can still run your native ArchLinux while in Windows and be able to boot back into the same ArchLinux natively.
 
This guide will help you set up your dual boot ArchLinux/Windows system so you can still run your native ArchLinux while in Windows and be able to boot back into the same ArchLinux natively.
  
First, we have to establish some requirements for this setup. Naturally, you need to have both OSes installed and set up for dual boot correctly. Here we will assume you are using GRUB to boot, but I'm sure other boot managers can be set up in a similar manner. This guide is focusing on setting up the environment using VirtualBox. Similar guide exists for VMWare (link) if you are using that. Assumption is that you have downloaded and installed the latest version of VirtualBox in Windows. So to sum up:
+
Prerequirements
* Running dual-boot ArchLinux/Windows system using Grub
+
 
 +
First, we have to establish some requirements for this setup. Naturally, you need to have both OSes installed and set up for dual boot correctly. Here we will assume you are using GRUB to boot, but I'm sure other boot managers can be set up in a similar manner. This guide is focusing on setting up the environment using VirtualBox. Similar guide is in the works for VMWare (link) if you are using that. Assumption is that you have downloaded and installed the latest version of VirtualBox in Windows.  
 +
 
 +
Finally, depending on your hard drive set up, device files for your hard drives may be different when you run Arch Linux natively and in a VM. For instance in my setup I run fake raid so my root partition is natively /dev/mapper/isw_ci...Systemp6 while under VM it is /dev/sda6 because Windows abstracts away fake raid for us. Your setup may be different but the same problem may apply. To circumvent this problem we need an addressing scheme that is persistent to both systems (e.g. doesn't change). One sure way of doing it is through UUIDs, which is how we are going to do it.
 +
 
 +
To sum up, you need:
 +
* Running dual-boot ArchLinux/Windows system using Grub  
 +
* Hard drive partitions mapped using UUIDs in Grub and /etc/fstab
 
* Have installed VirtualBox in Windows
 
* Have installed VirtualBox in Windows
y
+
 
 +
So lets get started. Boot into your Arch Linux and open up a terminal.
 +
 
 +
Preliminary steps in Arch Linux
 +
 
 +
Step 1 - Addressing using UUIDs
 +
 
 +
If you don't have this set up already we are going to switch addressing scheme in Grub and /etc/fstab to UUIDs. First step is to find out UUIDs for your partitions. Type in:
 +
blkid
 +
Your output should be something like:
 +
/dev/sda1: UUID="4AD8593BD859270B" LABEL="System Reserved" TYPE="ntfs"
 +
/dev/sda2: UUID="82D27570D27568F5" LABEL="System" TYPE="ntfs"
 +
/dev/sda3: UUID="3cbcd99c-399c-43b1-88bf-cc8a2269fd8a" TYPE="ext2"
 +
/dev/sda5: UUID="bf5adc7a-5a5c-46ca-814d-07a7db8b1f63" TYPE="swap"
 +
/dev/sda6: UUID="3ca3b8f2-4d61-4da0-9833-005d606422b3" TYPE="ext4"
 +
This tells you what is the UUID of each partition. Write it down or copy paste to the editor because we'll need it later. Next we need to update /etc/fstab so it maps partitions using UUIDs and not through device files:
 +
sudo nano /etc/fstab
 +
For all your hard drive partitions switch out their device name in the first column with the UUID given above using the following example as a guide:
 +
 
 +
Before:
 +
/dev/sda6 / ext4 defaults 0 1
 +
/dev/sda2 /mnt/Win7 ntfs-3g defaults 0 0
 +
After:
 +
UUID=3ca3... / ext4 defaults 0 1
 +
UUID=82D2... /mnt/Win7 ntfs-3g defaults,<font color=red>noauto,ro</font color> 0 0
 +
 
 +
Please note the ro flag marked in red. You must NOT allow ArchLinux write access to the Windows Partition where system is. Because they will be both running at the same time you are risking corruption of data due to concurrent writes. This is <font color=red>EXTREMELY DANGEROUS</font color> so you must put a read only flag here.
 +
Noauto flag is added because at boot Arch will try to mount the partition and give an error because it has been locked for mounting by the running Windows. We will get to mounting it back later.
 +
 
 +
Now we need to tell grub to use UUIDs as well so we can reach mkinitcpio image and the kernel to boot properly. Lets go to grub config:
 +
cd /boot/grub
 +
sudo nano menu.lst
 +
Find your boot entry for Arch Linux and copy paste a new entry with the same config. Rename it to "Arch Linux VirtualBox" and modify the root option to use UUIDs using the following guide as example:
 +
# (5) Arch Linux VirtualBox
 +
title  Arch Linux VirtualBox
 +
root
 +
kernel /vmlinuz26 root=/dev/disk/by-uuid/3ca3b8f2-... ro vga=773
 +
initrd /kernel26-vbox.img
 +
Substitute the root= device file name with the UUID of the partition you used in that place like in the example (vga flag gives us a nice framebuffer you may omit it if you don't want it). Make note of the initrd line. We are telling GRUB we want to use a new mkinitcpio image that we are about to build.
 +
 
 +
Repeat the process here, for the fallback entry. Create a new GRUB entry by copy pasting default Arch Linux fallback entry, call it "Arch Linux VirtualBox Fallback" and update its kernel and initrd lines so they show:
 +
kernel /vmlinuz26 root=/dev/disk/by-uuid/3ca3b8f2-... ro
 +
initrd /kernel26-vbox-fallback.img
 +
Make note the mkinitcpio image in this case is named kernel26-vbox-fallback.img.
 +
 
 +
Step 2 - Create new mkinitpcio image
 +
Next we need to generate a new mkinitcpio image that will comply with VirtualBox hardware configuration. Lets setup a new mkinitcpio image. Go to /etc/mkinitcpio.d/
 +
cd /etc/mknitcpio.d/
 +
Here mkinitpcio keeps all the presets for generating images. We want to create a new preset, stemming from the default one "kernel26". So lets make a copy and call it kernel26-vbox and open it in your favorite editor:
 +
sudo cp kernel26.preset kernel26-vbox.preset
 +
sudo nano kernel26-vbox.preset
 +
Here we need to change a few things. Change all occurrences of "kernel26" for "kernel26-vbox" except the line that is sourcing kernel26.ver file. Here are the parts that need to be changed:
 +
ALL_config="/etc/mkinitcpio-vbox.conf"
 +
...
 +
#default_config="/etc/mkinitcpio-vbox.conf"
 +
default_image="/boot/kernel26-vbox.img"
 +
#default_options=""
 +
...
 +
#fallback_config="/etc/mkinitcpio-vbox.conf"
 +
fallback_image="/boot/kernel26-vbox-fallback.img"
 +
fallback_options="-S autodetect"
 +
Now we need to create the configuration file for the preset. I keep them in /etc along with the default mkinitcpio.conf but you may want to do differently. If so, update the preset with proper paths to mkinitcpio-vbox.conf. Again we are going to use the default mkinitpcio as a guide:
 +
cd /etc/
 +
sudo cp mkinitcpio.conf mkinitcpio-vbox.conf
 +
nano /etc/mkinitcpio-vbox.conf

Revision as of 17:53, 27 October 2009

Lots of users run a dual boot between Arch Linux and another Operating System (Windows for instance). It can be tedious to switch back and forth if you need to work in both. Using Virtual Machines, we can install one OS in the VM and have both running. This is not always convenient, because for performance reasons we might want to be able to run both OSes natively when needed and still keep the convenience of having access to both OSes at the same time.

This guide will help you set up your dual boot ArchLinux/Windows system so you can still run your native ArchLinux while in Windows and be able to boot back into the same ArchLinux natively.

Prerequirements

First, we have to establish some requirements for this setup. Naturally, you need to have both OSes installed and set up for dual boot correctly. Here we will assume you are using GRUB to boot, but I'm sure other boot managers can be set up in a similar manner. This guide is focusing on setting up the environment using VirtualBox. Similar guide is in the works for VMWare (link) if you are using that. Assumption is that you have downloaded and installed the latest version of VirtualBox in Windows.

Finally, depending on your hard drive set up, device files for your hard drives may be different when you run Arch Linux natively and in a VM. For instance in my setup I run fake raid so my root partition is natively /dev/mapper/isw_ci...Systemp6 while under VM it is /dev/sda6 because Windows abstracts away fake raid for us. Your setup may be different but the same problem may apply. To circumvent this problem we need an addressing scheme that is persistent to both systems (e.g. doesn't change). One sure way of doing it is through UUIDs, which is how we are going to do it.

To sum up, you need:

  • Running dual-boot ArchLinux/Windows system using Grub
  • Hard drive partitions mapped using UUIDs in Grub and /etc/fstab
  • Have installed VirtualBox in Windows

So lets get started. Boot into your Arch Linux and open up a terminal.

Preliminary steps in Arch Linux

Step 1 - Addressing using UUIDs

If you don't have this set up already we are going to switch addressing scheme in Grub and /etc/fstab to UUIDs. First step is to find out UUIDs for your partitions. Type in:

blkid

Your output should be something like:

/dev/sda1: UUID="4AD8593BD859270B" LABEL="System Reserved" TYPE="ntfs" 
/dev/sda2: UUID="82D27570D27568F5" LABEL="System" TYPE="ntfs" 
/dev/sda3: UUID="3cbcd99c-399c-43b1-88bf-cc8a2269fd8a" TYPE="ext2" 
/dev/sda5: UUID="bf5adc7a-5a5c-46ca-814d-07a7db8b1f63" TYPE="swap" 
/dev/sda6: UUID="3ca3b8f2-4d61-4da0-9833-005d606422b3" TYPE="ext4" 

This tells you what is the UUID of each partition. Write it down or copy paste to the editor because we'll need it later. Next we need to update /etc/fstab so it maps partitions using UUIDs and not through device files:

sudo nano /etc/fstab

For all your hard drive partitions switch out their device name in the first column with the UUID given above using the following example as a guide:

Before:

/dev/sda6 / ext4 defaults 0 1
/dev/sda2 /mnt/Win7 ntfs-3g defaults 0 0

After:

UUID=3ca3... / ext4 defaults 0 1
UUID=82D2... /mnt/Win7 ntfs-3g defaults,noauto,ro 0 0

Please note the ro flag marked in red. You must NOT allow ArchLinux write access to the Windows Partition where system is. Because they will be both running at the same time you are risking corruption of data due to concurrent writes. This is EXTREMELY DANGEROUS so you must put a read only flag here. Noauto flag is added because at boot Arch will try to mount the partition and give an error because it has been locked for mounting by the running Windows. We will get to mounting it back later.

Now we need to tell grub to use UUIDs as well so we can reach mkinitcpio image and the kernel to boot properly. Lets go to grub config:

cd /boot/grub
sudo nano menu.lst

Find your boot entry for Arch Linux and copy paste a new entry with the same config. Rename it to "Arch Linux VirtualBox" and modify the root option to use UUIDs using the following guide as example:

# (5) Arch Linux VirtualBox
title  Arch Linux VirtualBox
root
kernel /vmlinuz26 root=/dev/disk/by-uuid/3ca3b8f2-... ro vga=773
initrd /kernel26-vbox.img

Substitute the root= device file name with the UUID of the partition you used in that place like in the example (vga flag gives us a nice framebuffer you may omit it if you don't want it). Make note of the initrd line. We are telling GRUB we want to use a new mkinitcpio image that we are about to build.

Repeat the process here, for the fallback entry. Create a new GRUB entry by copy pasting default Arch Linux fallback entry, call it "Arch Linux VirtualBox Fallback" and update its kernel and initrd lines so they show:

kernel /vmlinuz26 root=/dev/disk/by-uuid/3ca3b8f2-... ro
initrd /kernel26-vbox-fallback.img

Make note the mkinitcpio image in this case is named kernel26-vbox-fallback.img.

Step 2 - Create new mkinitpcio image Next we need to generate a new mkinitcpio image that will comply with VirtualBox hardware configuration. Lets setup a new mkinitcpio image. Go to /etc/mkinitcpio.d/

cd /etc/mknitcpio.d/

Here mkinitpcio keeps all the presets for generating images. We want to create a new preset, stemming from the default one "kernel26". So lets make a copy and call it kernel26-vbox and open it in your favorite editor:

sudo cp kernel26.preset kernel26-vbox.preset
sudo nano kernel26-vbox.preset

Here we need to change a few things. Change all occurrences of "kernel26" for "kernel26-vbox" except the line that is sourcing kernel26.ver file. Here are the parts that need to be changed:

ALL_config="/etc/mkinitcpio-vbox.conf"
...
#default_config="/etc/mkinitcpio-vbox.conf"
default_image="/boot/kernel26-vbox.img"
#default_options=""
...
#fallback_config="/etc/mkinitcpio-vbox.conf"
fallback_image="/boot/kernel26-vbox-fallback.img"
fallback_options="-S autodetect"

Now we need to create the configuration file for the preset. I keep them in /etc along with the default mkinitcpio.conf but you may want to do differently. If so, update the preset with proper paths to mkinitcpio-vbox.conf. Again we are going to use the default mkinitpcio as a guide:

cd /etc/
sudo cp mkinitcpio.conf mkinitcpio-vbox.conf 
nano /etc/mkinitcpio-vbox.conf