Sistemas Operativos 3ยบ Ano MIEEC
Project - Lab 1

1. Introduction

In the next lab classes you'll develop a project that consists of a Linux device driver for the PC's serial port. This project will be developed in groups of two students.

Because device drivers are part of the kernel, and you do not have root permission on the lab computers, you'll be using a virtual machine (VM), more specifically a VM created by VirtualBox.

2020 Update

This year, you will have to use your own PC. Even though you may have Linux installed and root permissions, it is safer to use a virtual machine (VM). Indeed, as written above you will be modifying the kernel, and the Linux kernel has a monolithic structure. Therefore a bug in your code may make your system unusable.

A VM is a program that emulates the HW of a PC. The VirtualBox is a "virtualization application". I.e. it is an application that can be used to create, configure and run virtual machines.

In this lab class you'll learn how to create a VM under VirtualBox.

2. Installing the VirtualBox on your PC or Laptop

If the VirtualBox is not yet installed in your PC, you will have to install it now.

When we use a VM we need to consider two OS's. The first one is the OS that runs on the physical machine: this is usually known as the host OS. The second is the OS that runs on the VM: this is usually known as the guest OS. Note that, when running, a VM is just a set of processes/threads on the host OS.

The VirtualBox is available for different host OSs, including Linux, MacOS and Windows. It is also able to create and run VM with different guest OSs. In this course, we will use as guest OS only Linux, more specifically version 4.0 of the Debian distribution, Debian 4.0 for short.

The VirtualBox is installed only on the Linux images of the computers in the labs of the B building. So, in the classes you'll be using Linux also as the host OS. To run the VirtualBox all you need to do is to find the appropriate sub-menu in the Applications menu, and select the Virtual Box OSE item.

For a more detailed overview of the VirtualBox and its capabilities, you may wish to read the first chapter of its User Manual

In your computer, you may choose whatever host OS you have. Note however, that these notes were written specifically for the Linux OS. This usually applies to the MacOS as well. However, most of it does not apply to Windows, since I do not have it installed on my laptop. So, if you choose Windows as your host OS, you'll be mostly on your own. (Future notes will also be written for Linux.)

2020 Update

Actually, if you install a bash shell on Windows via the Windows Subsystem for Linux, you should be able to use most of the instructions provided in these handouts without major changes on a Windows 10 host OS. I suggest that you follow the instructions to install a bash shell on Windows 10 from HowToGeek.

If you choose Linux, I suggest that you check first if your distribution has packages with the VirtualBox. I expect it to be the case for the major distributions. E.g. the Debian and Ubuntu distributions have several packages with the VirtualBox. Just in case, try to install all of them.

Anyway, it is a good idea to install the so-called Guest Additions, which are software components that provide additional features.

On Windows, you may want to try the Microsoft store and check if the VirtualBox is available from there.

If the host OS you plan to use does not have "packages" with VirtualBox, you will have to download it directly from the VirtualBox site.

For details regarding the installation of the VirtualBox you can take a look at the second chapter of its User Manual.

2020 Update

The following paragraph describes an alternative to instal VirtualBox on your PC's host OS. This alternative had in mind students that were running Windows. However, now, with the the Windows Subsystem for Linux, we recomment that you use a bash shell, rather than having the trouble of installing yet another Linux OS in yet another VM.

Please note that an alternative to install VirtualBox on your PC's host OS, is to install it on the guest OS of another VM. For example, if your host OS is Windows, you may install VirtualBox on another VM running directly on Windows. A consequence of this is that the programs on the VirtualBox will run slower than they would if VirtualBox was running directly on your PC's host OC, but the performance will still be acceptable, since the guest OS you'll be using for the Operating System's class is not resource hungry.

3. Setting up the VirtualBox

Once you have installed the VirtualBox, you will now have to create a virtual machine for this course. For that you will have to download this VM appliance which has a basic Debian installation for use in this course. Save the appliance in some file in your host OS and remember its pathname because you will need it in the setup of VirtualBox.

3.1 Creating a Virtual Machine

A virtual machine is an emulation of the HW of a PC, on top of which runs the guest OS.

The VirtualBox may have several virtual machines each with its own configuration and its own guest OS. When you first start the VirtualBox OSE it has no virtual machine defined.

VM start window

Thus your first task will be to create one, by impporting an appliance. In the following paragraphs, we'll detail the steps you'll need to take to complete this task. All these steps assume that you are working on a computer in one of the computer labs.

Step 1 Select the Import Appliance option from File Menu

2020 Update

This and the following images are from an installation process on FEUP's computer labs. So, they are most likely different from what you see on the screen of your system.

VirtualBox import appliance

Step 2 In the dialog box, select the VM appliance CICA2.ova that you have downloaded in the previous step:

2020 Update

Rembember, the path here is the path that you would have to use in FEUP's computers. You will need to use the pathname of the file you created upon saving the VM appliance.

VirtualBox import dialog

press next.

Step 3 Change the Virtual Disk Image directory to some directory in your own area in the appliance settings dialog:

2020 Update

In your own computer you should not use the /tmp directory. This is because its contents are deleted every time the host OS reboots. Therefore you would have to setup the VM again the first time you want to run the VM after rebooting the host OS.

IMP.-Please, uncheck also the USB controller box, because otherwise your image will fail to start.

Appliance settings dialog

and press the Import button. The VirtualBox image will then be copied to a local directory, i.e. a directory in the local computer, that you are authorized to access, both for reading and for writing. Given the size of the image and that you are copying it to the same local disk this may take around one minute. Once the process is complete you will see one VM image named CICA2 in the list of VM installed on the left side of the VirtualBox OSE's main window:

3.2 Shared Folders

Because the image used by the VM, i.e. the guest OS, has no GUI, you'll find yourself doing most of the coding in the host OS, and then copying that code to the guest OS, and compiling it there. This modus operandi is made easier by the means of the shared folder mechanism, which allows the guest OS to access selected portions of the host OS filesystem.

In order to setup a shared folder you must first define it. One way to do it is to select the Settings orange icon. This pops up the Settings window. Then on the list at the left of this window you should select the Shared Folders item. Next select the icon with the folder and the plus sign on the top right corner. This pops up another window where you need to configure the shared folder by 1) specifying the path of the folder in the host OS you wish to share with the guest OS; 2) giving it a name. To specify the (absolute) path of the folder you can either type it directly in the corresponding text box or you can select it using a file system browser. In any case, I suggest that you create a folder specifically for this project, and define it as a shared folder. The folder name should be host. (Actually, this name may be arbitrary, but the guest OS installed on the disk image is configured to mount a folder with name host, so by choosing this name you'll avoid some additional configuration changes.)

For more details regarding shared folders you can read the respective section in Chapter 4 of the User Manual.

3.3 Removing the VMs

2020 Update

This section assumes you are using the /tmp/ folder to store your VM's disk image. If you followed the above instructions, that should not be the case anymore. So, you can just skip this section.

Because the /tmp/ directory is temporary, your VM image will be deleted every time you shutdown the host OS. This means that, when you start the VirtualBox for the first time in every class, VirtualBox OSE will list VM CICA2 as a configured VM, but the VM's image will have been lost.

To recover that image you will need to repeat all the steps described above in Section 3.1. However, for these steps to succeed, first you must delete the VM. For that, the easiest is to use a terminal and execute the commands shown next:

$ cd ~/VirtualBox\ VMs/ $ rm -rf CICA2

4. Booting the VM

At this point you can start your newly created and configured VM, by pressing the green Start icon, at the right of the Settings icon.

Please read carefully the first message regarding keyboard capture. You can find more info regarding this feature in the Capturing and releasing keyboard and mouse in VirtualBox Manual Chapter 1. Also useful is the section that follows the previous one: Typing special characters.

After pressing the OK button, you get the boot screen. You need not take any action. After about 30 seconds you'll get a login prompt. The Linux system running on the VM has one account so whose password is: so. For the rare occasions you need root permissions, you must use the sudo utility.

4.1 A sample session

Now, you can try out the development process outlined above.

In the host OS, create the hello.c file under the shared folder with an "hello world" program, using your favorite text editor.

In the guest OS, log in as so. Change to the subdirectory under /host where the hello.c file you've just created resides. Now, compile it, using the standard command:

gcc -Wall hello.c -o hello

If there is no error, you can run it (on the guest OS):

./hello

Otherwise, you need to fix the errors in your text editor running on the host OS and again compile the file in the window running the guest OS.

As you can see, this is not very different from the steps you would take when you develop code without a VM and compile and test it in a separate terminal.