30
Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Embed Size (px)

Citation preview

Page 1: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Tutorial and Demos on Linux Virtual Machine

CSC 4320/6320 - Operating SystemsSummer 2015

Page 2: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Outline

• How to install Virtual Machine(VM)?• How to install a Linux Operating using VM?• Install Guest Addition for Linux.• Demo: linux Kernel module

Page 3: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

How to install Virtual Machine?(1)

• Install a virtual machine, e.g. Virtual Box.– Download Virtual Box from website

https://www.virtualbox.org/wiki/Downloads .(Note:Please choose the right platform package for your host OS).– Install Virtubal Box in your computer.

Page 4: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

How to install Virtual Machine?(2)

• Once the installation is done, you can find a similar GUI for Virtual Box as follows……

Page 5: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

How to install Virtual Machine?(2)

• Once the installation is done, you can find the GUI for Virtual Box.

Page 6: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

How to install a Linux system using VM?(1)

• Install Ubuntu System using Virtual Box– Download the latest Ubuntu system from website

http://www.ubuntu.com/download/desktop.– Follow the tutorial to install Ubuntu.– Note: Please install a new Ubuntu instead try

Ubuntu. The installation takes about 20 minutes.

Page 7: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

How to install a Linux system using VM?(2)

• Once the installation is done, you can login in it and the system is as follows……

Page 8: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015
Page 9: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Install Guest Addition for Linux(1)

• Goal: enable other strong features(e.g. folder sharing with host , screen enlarging).

• Steps:o In menu bar, choose Devices -> Insert Guest addition

CD Image.o Click Yes when asked to start VBOXADDITIONS.o Authenticate the installation.o The installation opens a terminal and displays the

procedure. Press Return when it is finished.o Log out and then log in.

Page 10: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Install Guest Addition for Linux(2)

• Share folders with host OS.• Steps:

o Create a folder on Host computer. (e.g. shareName)

o Open Virtual Box and your Ubuntu System. o In Menu Bar, select Devices-> Shared folder

settings.

Page 11: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Install Guest Addition for Linux(3)

o Click at right most of popped up window to add a folder.

Page 12: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Install Guest Addition for Linux(4)

o Select the folder path and choose Auto-mount and Make Permanent.

Page 13: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Install Guest Addition for Linux(5)

o Click Ok to finish the settings.

Page 14: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Install Guest Addition for Linux(6)

o Open a terminalOpen the dash, type “terminal”, select Terminal from the appeared results.o Type the following command sudo adduser $USER vboxsfNote: you have to add current user into group vboxsf by above command, otherwise you will be warned permission denied when access to the shared folder.

Page 15: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Install Guest Addition for Linux(7)

Page 16: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Install Guest Addition for Linux(8)

o Shut down Ubuntu, then reboot Virtual Box and Ubuntu.

o Find the shared folder sf_shareName in /media.

Page 17: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Demo: Linux Kernel Modules

• List all kernel modules loaded currently.$lsmodModule Size Used byVboxsf 39690 1………………….…..

Page 18: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Demo: Linux Kernel Modules-----Part I (1)

• Create a Kernel Module “simple”. (Page 96-99 )– Create a program named simple.c (prints

appropriate messages when the kernel module is loaded and unloaded).

– Create a Makefile for compiling the program.

Page 19: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>

/* This cis called when the module is loaded. */int simple_init(void){ printk(KERN_INFO "Loading Module\n"); return 0;}/* This function is called when the module is removed. */void simple_exit(void) {

printk(KERN_INFO "Removing Module\n");}

/* Macros for registering module entry and exit points. */module_init( simple_init );module_exit( simple_exit );

MODULE_LICENSE("GPL");MODULE_DESCRIPTION("Simple Module");MODULE_AUTHOR("SGG");

simple.c

Page 20: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

obj-m += simple.oall:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Makefile

Note: Makefile should be in the same directory as simple.c

Page 21: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Demo: Linux Kernel Modules-----Part I (2)

• Compile kernel module simple.c$makeNote: if the compiling succeeds, several files are produced.

Page 22: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Demo: Linux Kernel Modules-----Part I (3)

• Load Kernel Module$sudo insmod simple.ko

• Check out contents in kernel log buffer.$dmesg

Page 23: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Demo: Linux Kernel Modules-----Part I (4)

• Remove Kernel Module$sudo rmmod simple

• Check out contents in kernel log buffer.$dmesg

Page 24: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Demo 1

Page 25: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Demo: Linux Kernel Modules-----Part II

• Create a Kernel Module “simple-solution”.(Page 99-101 )– Create a program named simple-solution.c.

• In the module entry point, create a linked list containing five struct birthday elements and traverse the linked list.

• The name of first struct birthday element should be your own name.

• In the module exit point, remove the elements from the linked list and return the free memory back to the kernel

– Create a Makefile for compiling the program.

Page 26: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/list.h>#include <linux/slab.h>

struct birthday {

int month;int day;int year;

char *name; struct list_head list;};

static LIST_HEAD(birthday_list);

int simple_init(void){

/* Create a linked list containing

five struct birthday elements*/ /* Traverse the linked list */ }

void simple_exit(void) { /* Remove the elements from the linked list and return the free memory back to the kernel */ }

module_init( simple_init );module_exit( simple_exit );

MODULE_LICENSE("GPL");MODULE_DESCRIPTION("Kernel Data Structures");MODULE_AUTHOR("SGG");

simple-solution.c (incomplete version )

Page 27: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

obj-m += simple-solution.oall:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modulesclean:

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Makefile

Note: Makefile should be in the same directory as simple_solution.c

Page 28: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Demo 2

Page 29: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

Practice project1 to-do list

• See Project 1 requirement from D2L. • Download files from D2L and follow the steps. • For Part II:– Add codes in simple-solution.c based on the instruction in

Project Part 2 (Page 99-101) of textbook.– Compile simple-solution.c and then try to load and remove

kernel module.• Use dmesg to check out the kernel log content right

after loading and removing kernel module.• Save the kernel log contents into screenshots.

Page 30: Tutorial and Demos on Linux Virtual Machine CSC 4320/6320 - Operating Systems Summer 2015

What to submit?

• A complete version of simple-solution.c• Project report including required screenshots

and source code of simple-solution.c (a sample is provided in D2L).