TranslateProject/sources/tech/20210629 Try Linux on any operating system with VirtualBox.md
DarkSun 9e078d82f7 选题[tech]: 20210629 Try Linux on any operating system with VirtualBox
sources/tech/20210629 Try Linux on any operating system with VirtualBox.md
2021-06-30 05:03:23 +08:00

16 KiB

Try Linux on any operating system with VirtualBox

VirtualBox helps anyone—even a command line novice—set up a virtual machine. Person programming on a laptop on a building

VirtualBox makes it easy for anyone to try Linux. You don't even need experience with the command line to set up a simple virtual machine to tinker with Linux. I'm kind of a power user when it comes to virtual machines, but this article will show even novices how to virtualize a Linux system. In addition, it provides an overview of how to run and install a Linux system for testing purposes with the open source hypervisor VirtualBox.

Terms

Before starting, you should understand the difference between the two operating systems (OSes) in this setup:

  • Host system: This is your actual OS on which you install VirtualBox.
  • Guest system: This is the system you want to run virtualized on top of your host system.

Both systems, host and guest, must interact with each other when it comes to input/output, networking, file access, clipboard, audio, and video.

In this tutorial, I'll use Windows 10 as the host system and Fedora 33 as the guest system.

Prerequisites

When we talk about virtualization, we actually mean hardware-assisted virtualization. Hardware-assisted virtualization requires a compatible CPU. Almost every ordinary x86 CPU from the last decade comes which this feature. AMD calls it AMD-V, and Intel calls it VT-x. The virtualization feature adds some additional CPU instructions, and it can be enabled or disabled in the BIOS.

To start with virtualization:

  • Make sure that AMD-V or VT-x is enabled in the BIOS.
  • Download and install VirtualBox.

Prepare the virtual machine

Download the image of the Linux distribution you want to try out. It does not matter if it's a 32-bit or 64-bit OS image. You can even start a 64-bit OS image on a 32-bit host system (with limitations in memory usage, of course) and vice versa.

Considerations: If possible, choose a Linux distribution that comes with the Logical Volume Manager (LVM). LVM decouples the filesystem from the physical hard drives. This allows you to increase the size of your guest system's hard drive if you are running out of space.

Now, open VirtualBox and click on the yellow New button:

VirtualBox New VM

(Stephan Avenwedde, CC BY-SA 4.0)

Next, configure how much memory the guest OS is allowed to use:

Set VM memory size

(Stephan Avenwedde, CC BY-SA 4.0)

My recommendation: Don't skimp on memory! When memory is low, the guest system will start paging memory from RAM to the hard drive, worsening the system's performance and responsiveness extremely. If the underlying host system starts paging, you might not notice. For a Linux workstation system with a graphical desktop environment, I recommend at least 4GB of memory.

Next, create the hard disk:

Create virtual hard disk

(Stephan Avenwedde, CC BY-SA 4.0)

Choose the default option, VDI:

Selecting hard disk file type

(Stephan Avenwedde, CC BY-SA 4.0)

In this window, I recommend choosing dynamically allocated, as this allows you to increase the size later. If you choose fixed size, the disk will be probably faster, but you won't be able to modify it:

Dynamically allocating hard disk

(Stephan Avenwedde, CC BY-SA 4.0)

With a Linux distribution that uses LVM, you can start with a small hard disk. If you are running out of space, you can increase it on demand.

Note

: Fedora's website says it requires a minimum of 20GB free disk space. I highly recommend you stick to that specification. I chose 8GB here so that I can demonstrate how to increase it later. If you are new to Linux or inexperienced with the command line, choose 20GB.

Setting hard disk size

(Stephan Avenwedde, CC BY-SA 4.0)

After creating the hard drive, select the newly created virtual machine from the list in VirtualBox's main window and click on Settings. In the Settings menu, go to System and select the Processor tab. By default, VirtualBox assigns only one CPU core to the guest system. On a modern multicore CPU, it should not be any problem to assign at least two cores, which will speed up the guest system significantly:

Assigning cores to guest system

(Stephan Avenwedde, CC BY-SA 4.0)

Network adapter setup

The next thing to take care of is the network setup. By default, VirtualBox creates one NAT connection, which should be OK for most use cases:

Network settings

(Stephan Avenwedde, CC BY-SA 4.0)

You can create more than one network adapter. Here are the most common types:

  • NAT: The NAT adapter performs a network address translation. From the outside, it looks like the host and the guest system use the same IP address. You are not able to access the guest system from within the host system over the network. (Although you could define port forwarding to access certain services.) When your host system has access to the internet, the guest system will have access, too. NAT requires no further configuration.
    • Choose NAT if you only need internet access for the guest system.
  • Bridged adapter: Here, the guest and the host system share the same physical Ethernet device. Both systems will have independent IP addresses. From the outside, it looks like there are two separate systems in the network, both sharing the same physical Ethernet adapter. This setup is more flexible but requires more configuration.
    • Choose Bridged adapter if you want to share the guest system's network services.
  • Host-only adapter: In this configuration, the guest system can only talk to the host or other guest systems running on the same host. The host system can also connect to the guest system. There is no internet nor physical network access for the guest.
    • Choose Host-only adapter for advanced security.

Assign the OS image

Navigate to Storage and select the virtual optical drive. Click on the CD icon on the right, and select Choose a disk file…. Then assign the downloaded Linux distribution image you want to install:

Assigning OS image

(Stephan Avenwedde, CC BY-SA 4.0)

Install Linux

The virtual machine is now configured. Leave the Settings menu and go back to the main window. Click on the Green arrow (i.e., the start button). The virtual machine will start up and boot from the virtual optical drive, and you will find yourself in your Linux distribution's installer:

VirtualBox Fedora installer

(Stephan Avenwedde, CC BY-SA 4.0)

Partitioning

The installer will ask you for partitioning information during the installation process. Choose Custom:

Selecting Custom partition configuration

(Stephan Avenwedde, CC BY-SA 4.0)

Note: I'm assuming you're creating this virtual machine just for testing purposes. Also you don't need to care about hibernation for your guest system, as this function is implicitly provided by VirtualBox. Therefore, you can omit the swap partition to save disk space on your host system. Keep in mind that you can add a swap partition later if needed. In An introduction to swap space on Linux systems, David Both explains how to add a swap partition and choose the correct size.

Fedora 33 and later offer a zram partition, a compressed part of the memory used for paging and swap. The zram partition is resized on demand, and it is much faster than a hard disk swap partition.

To keep it simple, just add these two mount points:

Adding mount points

(Stephan Avenwedde, CC BY-SA 4.0)

Apply the changes and proceed with the installation.

Install VirtualBox Guest Additions

After you finish the installation, boot from the hard drive and log in. Now you can install VirtualBox Guest Additions, which include special device drivers and system applications that provide:

  • Shared clipboard
  • Shared folders
  • Better performance
  • Freely scalable window size

To install them, click on the top menu in Devices and select Insert Guest Additions CD image…:

Selecting Guest Additions CD image

(Stephan Avenwedde, CC BY-SA 4.0)

On most Linux distributions, the CD image with the Guest Additions is mounted automatically, and they are available in the file browser. Fedora will ask you if you want to run the installation script. Click Run and enter your credentials to grant the process root rights:

Enabling Guest Additions autorun

(Stephan Avenwedde, CC BY-SA 4.0)

When the installation is finished, reboot the system.

LVM: Enlarge disk space

Creating an 8GB hard disk was a dumb decision, as Fedora quickly starts signaling that it is running out of space:

Fedora hard disk running out of space

(Stephan Avenwedde, CC BY-SA 4.0)

As I mentioned, a disk space of 20GB is recommended, and 8GB is the absolute minimum for a Fedora 33 installation to boot up. A fresh installation with no additional software (except the VirtualBox Guest Additions) takes nearly the whole 8GB of available space. Don't open the GNOME Software center or anything else that might download files from the internet in this condition.

Luckily, I chose to use LVM, so I can easily fix this mishap.

To increase the filesystem's space within the virtual machine, you must first increase the virtual hard drive on your host system.

Shut down the virtual machine. If your host system is running Windows, open a command prompt and navigate to C:\Program Files\Oracle\VirtualBox. Resize the disk to 12,000MB with the following command:

`VBoxManage.exe modifyhd "C:\Users\StephanA\VirtualBox VMs\Fedora_33\Fedora_33.vdi" --resize 12000`

Boot the virtual machine and open the Disks utility. You should see the newly created unassigned free space. Select Free Space and click the + button:

Free space before adding

(Stephan Avenwedde, CC BY-SA 4.0)

Now, create a new partition. Select the amount of free space you want to use:

Creating a new partition and setting size

(Stephan Avenwedde, CC BY-SA 4.0)

You don't want to create a filesystem or anything else on your new partition, so select Other:

Selecting "other" for partition volume type

(Stephan Avenwedde, CC BY-SA 4.0)

Select No Filesystem:

Setting "No filesystem" on new partition

(Stephan Avenwedde, CC BY-SA 4.0)

The overview should now look like this:

VirtualBox after adding new partition

(Stephan Avenwedde, CC BY-SA 4.0)

There is a new partition device, /dev/sda3. Check your LVM volume group by typing vgscan:

Checking LVM volume group by typing vgscan:

(Stephan Avenwedde, CC BY-SA 4.0)

Now you have everything you need. Extend the volume group in the new partition:

`vgextend fedora_localhost-live /dev/sda3`

vgextend command output

(Stephan Avenwedde, CC BY-SA 4.0)

Because the volume group is larger, you can increase the size of the logical volume. The command vgdisplay shows that it has 951 free extends available:

vgdisplay command output

(Stephan Avenwedde, CC BY-SA 4.0)

Increase the logical volume by 951 extends:

`lvextend -l+951 /dev/mapper/fedora_localhost--live-root`

lvextend command output

(Stephan Avenwedde, CC BY-SA 4.0)

After you increase the logical volume, the last thing to do is to resize the filesystem:

`resize2fs /dev/mapper/fedora_localhost--live-root`

resize2fs command output

(Stephan Avenwedde, CC BY-SA 4.0)

Done! Check the Disk Usage Analyzer, and you should see that the extended space is available for the filesystem.

Summary

With a virtual machine, you can check how a piece of software behaves with a specific operating system or a specific version of an operating system. Besides that, you can also try out any Linux distribution you want to test without worrying about breaking your system. For advanced users, VirtualBox offers a wide range of possibilities when it comes to testing, networking, and simulation.


via: https://opensource.com/article/21/6/try-linux-virtualbox

作者:Stephan Avenwedde 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出