How To Convert a Linux System Disk to VirtualBox in Mac OS X
This document is provided under the terms of http://www.oracle.com/us/legal/terms/index.html.
Please do not plagiarize. Use at your on risk. Thanks!
Operating systems other than Linux do not support Linux LVM volumes and file systems. Accessing or analyzing the data on a Linux hard disk is therefore usually not feasible without running the original system. Computer virtualization can provide an easy solution for such scenarios in order to consolidate hardware, or access systems when hardware is not available.
The following instructions outline how to copy and convert a Oracle Linux 6 system hard disk to a virtual disk image and how to use it as a guest OS in VirtualBox. The task was performed on an Apple Mac Pro running OS X 10.9.5 and Oracle VirtualBox 5.0.16. Other software combinations or systems should work as well.
1. Connecting a hard drive
Attaching a hard drive to a computer obviously requires an appropriate hardware interface or suitable adapter. Most computers use IDE or SATA and offer USB to connect external devices. If the Linux hard drive is IDE or SATA, you may simply be able to replace the hard drive of an USB enclosure, or use a generic SATA/IDE to USB adapter. Such adapters are inexpensive and can be purchased from many online computer retail stores.
Your options to connect SCSI or SAS drives, which are common in enterprise servers systems and some workstations, are rather limited. Unless your computer is equipped with an extra SCSI or RAID host bus adapter, you will not be able to connect such drives directly, and probably not at all.
SCSI to Firewire or USB converters exist, but they are relatively expensive and primarily designed to connect older SCSI peripherals, such as a scanner. An external SCSI enclosure or appropriate adapter is still necessary to connect to the interface of a SCSI hard drive.
SAS is an evolution of SCSI and such controllers usually support also SATA drives, but SAS drives do not operate with SATA controllers and are keyed to prevent any chance of plugging them in incorrectly. There are simple SAS to SATA adapters, however, SAS drives will not work with SATA controllers.
The standard storage controller inside any Intel based Apple desktop or portable computer does not support SCSI or SAS drives. Searching on eBay or Amazon might be your best option to find converters and adapters, and depending on your Mac Pro model, find an appropriate host bus adapter card, such as the Apple RAID card, which supports SAS and SATA hard drives.
|Note:||As always, do not move any conventional or mechanical hard drive HDD while it is powered on, otherwise you may risk permanent damage of the disk and data.|
2. Converting a hard disk to a virtual disk image
Copying and converting a complete hard disk can be time and disk space consuming. Make sure you have enough capacity in order not to use all your remaining free disk space and cause your computer system to malfunction. As a rule of thumb, you should always have at least 15 % of free disk space.
For example, to convert a 120 GB hard disk will require 120 GB plus the disk space that is actually being used on that hard drive, e.g. 30 GB. You would need more than 152 GB of free disk space on your computer for the task to complete successfully. If you connect the hard drive via USB-2, you are not likely going to get more than 40 MB/s of data throughout, and converting a 120 GB hard disk will take at least 50 minutes.
After you have connected the hard drive to your Mac, it won't show up on the desktop, because Mac OS X does not recognize Linux LVM volumes or Linux file systems. It will display an initialization dialog instead. When prompted, choose Ignore.
Do not Initialize the disk, since this would erase it's contents.
Open the Mac OS X Terminal application (Applications:Utilities).
Use the diskutil list command to list all available disk devices. Your output may look similar to the following example:
Saturn:~ dude$ diskutil list
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *1.0 TB disk0
1: EFI EFI 209.7 MB disk0s1
2: Apple_CoreStorage 999.3 GB disk0s2
3: Apple_Boot Recovery HD 650.0 MB disk0s3
#: TYPE NAME SIZE IDENTIFIER
0: Apple_HFS Saturn *999.0 GB disk1
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *120.0 GB disk2
1: Linux 524.3 MB disk2s1
2: Linux_LVM 119.5 GB disk2s2
The Linux hard disk in the above example is obviously disk2.
Enter the following commands to copy and convert the Linux hard disk to a VirtualBox disk image.
Be sure to use /dev/rdisk and not /dev/disk as shown in the disk utility when using the dd command to copy the device. /dev/rdisk provides direct access to the physical hard disk and is not buffered or cached by the operating system, which in this particular case would slow down the copy processes drastically.
You can press control-t to display the current performance and status of the dd command:
$ sudo dd if=/dev/rdisk1 of=Linux120GB.raw bs=2m
load: 0.62 cmd: dd 2651 uninterruptible 0.00u 0.57s
370+0 records in
370+0 records out
775946240 bytes transferred in 19.262300 secs (40283156 bytes/sec)
You will need to supply your administrator password, or current password if you use an admin account already.
Saturn:~ dude$ cd
Saturn:~ dude$ cd Desktop
Saturn:Desktop dude$ mkdir hdconvert
Saturn:Desktop dude$ cd hdconvert
Saturn:Desktop dude$ diskutil unmountdisk disk2
Unmount of all volumes on disk1 was successful
Saturn:Desktop dude$ sudo dd if=/dev/rdisk2 of=Linux120GB.raw bs=2m
Saturn:Desktop dude$ sudo chown $USER Linux120GB.raw
Saturn:Desktop dude$ VBoxManage convertfromraw Linux120GB.raw Linux120GB.vdi
Converting from raw image file="Linux120GB.raw" to file="Linux120GB.vdi"...
Creating dynamic image with size 120034123776 bytes (114474MB)...
Saturn:Desktop dude$ open ./
To save disk space, delete the Linux120GB.raw file and empty the Trash. The file is no longer going to be used.
The file with the .vdi extension is a special VirtualBox disk image. The VirtualBox utility will create a dynamic disk image, that will automatically expand as more disk space is required, until the disk image reaches the maximum capacity that was chosen when it was created, e.g.120 GB. The size of the Linux120GB.vdi disk image is the amount of data that was actually used, e.g. 26 GB.
3. Creating a virtual machine
Keep in mind that VirtualBox is a type 2 Hypervisor and running a 64-bit virtual machine or guest OS is mandatory in order to take advantage of hardware assisted virtualization (VT-x). The host OS as such is irrelevant. Using a 32-bit virtual machine template or running a x86 guest OS would operate in software emulation mode, which is less efficient.
All Intel based Apple Computers ship with a 64-bit CPU with virtualization enabled. Be sure to create a virtual machine template that matches the actual operating system contained in the virtual disk image created earlier.
Click the New button from the toolbar,
or select New from the Machine menu.
Depending on your previous settings, the next series of dialogs are going to be in Guided or Expert mode.
You can choose by pressing the Guided or Expert button. For simplicity, the following shows Expert mode.
Feel free to give the virtual machine any useful name, but be sure to create the correct VM template.
For example, Linux, Oracle 64-bit.
For general purpose, 2 GB of RAM should be sufficient.
Choose "Do not add a virtual hard disk".
Click the Create button.
Open the VirtualBox application Preferences to locate the machine folder where to place the virtual disk image.
By default, the virtual machine folder is inside your home folder.
Move the virtual disk image, converted earlier, into the appropriate virtual machine folder.
Select the virtual machine (single click) in the left window
Choose Settings from the toolbar.
Click on the SATA controller.
Click on the rightmost square icon with a plus sign to add a virtual disk image.
Select the button Choose existing disk.
Navigate to the machine folder of the virtual machine.
Select the virtual disk image, e.g. Linux120GB
4. Configuring the virtual machine network
You can configure up to 4 virtual network adapters and combine their function, but keep in mind that you will need to understand how TCP/IP network routing works in combination with VirtualBox. You cannot arbitrarily assign a TCP/IP addresses or network and expect it to function.
To understand the purpose of the various network adapters, please review the VirtualBox documentation:
Below is brief overview that may help you to choose the right adapter:
VirtualBox uses the NAT adapter by default and it should work out of the box. When using the NAT interface, connections from the guest OS will appear as connection from the host OS. Incoming connection to the guest OS under NAT are however rejected and shielded by design.
Keep in mind that the NAT adapter requires a dynamic TCP/IP address assigned by the VirtualBox internal router (DHCP) in order to function. If you do not need to connect to the guest OS by SSH, or provide any other service, but allow the guest OS to access the Internet, the NAT adapter should be fine.
If you do not wish to limit your connectivity options and maintain your own simple network, the bridged network adapter is usually the easiest to configure, and you can assign any TCP/IP address to your guest OS that is available within your host system network.
For simplicity, the following configures the VirtualBox bridged network adapter.
Select the virtual machine (single click)
Choose Settings from the toolbar.
Select the Network icon.
Change Attached to from NAT to Bridged Adapter.
Flip the Advanced switch to show further details.
Write down the MAC address - Note that O's are Zero's!
Click the OK button.
Regardless of the type of network adapter you choose, the guest OS will have to use a different network adapter than the computer where the hard drive was initially installed. The hardware address of the network adapter (MAC address) will be different and require some adjustment.
It should work to obtain the MAC address of the network device that was configured in the previous computer and update the MAC address of the virtual network adapter accordingly, however, it is better to update the guest OS to avoid potential conflicts.
Start the virtual machine
Login as user root.
If you do not see the console, open a terminal command prompt.
[root@ol1 ~]# cd /etc/udev/rules.d
The file 70-persistent-net.rules belongs to the UDEV subsystem and assures persistent network adapter names across system restarts. It assigns network adapter names to physical MAC addresses and gets automatically populated when new network devices are detected during a system start up.
All entries in the current file belong to the previous computer system and are irrelevant. If there was already an entry for device eth0 from the previous system, the new network interface of the virtual machine will be assigned another name, e.g. eth1. For this reason, any existing TCP/IP configuration for eth0 will not function.
Simply delete or rename the file and let the system recreate it during the next system restart.
[root@ol1 rules.d]# mv 70-persistent-net.rules 70-persistent-net.rules.backup
[root@ol1 rules.d]# shutdown -h now
The virtual machine must shut down to recreate the udev network device entry for eth0. You should verify that the virtual machine network adapter was indeed assigned eth0 after the next system restart. If necessary, edit 70-persistent-net.rules accordingly.
After the next start up, device eth0 should reflect the MAC address of the virtual machine network adapter.
If you had a previous TCP/IP setup, analyze the content of the eth0 configuration file and make appropriate adjustments.
[root@ol1 ~]# cd /etc/sysconfig/network-scripts
[root@ol1 network-scripts]# cat ifcfg-eth0
Edit the file ifcfg-eth0 to change the TCP/IP configuration according to your requirements.
You can either remove or update HWADDR. If you update HWADDR, the value must match the MAC address of the virtual network adapter, using the correct 00:00:00:00:00:00 format.
You may need to change or remove the Gateway address, or change the default Gateway in /etc/sysconfig/network. If you do not specify any particular interface gateway, the default gateway will be used instead.
Your output may look similar to the following:
Restart the guest OS:
[root@ol1 ~]# shutdown -r now
That's all. Best of luck!