Operating Systems 2018F: Tutorial 5: Difference between revisions
Created page with "In this tutorial you will be creating filesystems and installing kernel modules. It is '''highly recommended''' that you use a comp3000 openstack instance for the exercises b..." |
No edit summary |
||
Line 210: | Line 210: | ||
default: | default: | ||
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules | $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules | ||
</source> | |||
==newgetpid module== | |||
===newgetpid.c=== | |||
<source lang="C" line> | |||
/* Code derived from: | |||
https://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/ | |||
and | |||
http://pete.akeo.ie/2011/08/writing-linux-device-driver-for-kernels.html | |||
*/ | |||
#include <linux/module.h> | |||
#include <linux/string.h> | |||
#include <linux/fs.h> | |||
#include <linux/device.h> | |||
#include <linux/init.h> | |||
#include <linux/kernel.h> | |||
#include <linux/sched.h> | |||
#include <asm/uaccess.h> | |||
#define dbg(format, arg...) do { if (debug) pr_info(CLASS_NAME ": %s: " format, __FUNCTION__, ## arg); } while (0) | |||
#define err(format, arg...) pr_err(CLASS_NAME ": " format, ## arg) | |||
#define info(format, arg...) pr_info(CLASS_NAME ": " format, ## arg) | |||
#define warn(format, arg...) pr_warn(CLASS_NAME ": " format, ## arg) | |||
#define DEVICE_NAME "newgetpid" | |||
#define CLASS_NAME "comp3000" | |||
static struct class* newgetpid_class = NULL; | |||
static struct device* newgetpid_device = NULL; | |||
static int newgetpid_major; | |||
static int newgetpid_open(struct inode *the_inode, struct file *f) | |||
{ | |||
return 0; | |||
} | |||
static ssize_t newgetpid_read(struct file *f, char *buf, size_t len, loff_t *offset) | |||
{ | |||
size_t i, msglen; | |||
pid_t thepid; | |||
char message[100]; | |||
if (*offset > 0) { | |||
return 0; | |||
} | |||
thepid = task_tgid_vnr(current); | |||
snprintf(message, 100, "Your PID is %d!\n", thepid); | |||
msglen = strlen(message); | |||
if (len < msglen) { | |||
msglen = len; | |||
} | |||
for (i = 0; i < msglen; i++) { | |||
put_user(message[i], buf++); | |||
} | |||
*offset = i; | |||
return i; | |||
} | |||
static int newgetpid_release(struct inode *the_inode, struct file *f) | |||
{ | |||
printk(KERN_ALERT "Newgetpid device closed\n"); | |||
return 0; | |||
} | |||
static struct file_operations newgetpid_fops = { | |||
.open = newgetpid_open, | |||
.read = newgetpid_read, | |||
.release = newgetpid_release, | |||
}; | |||
static char *newgetpid_devnode(struct device *dev, umode_t *mode) | |||
{ | |||
if (mode) | |||
*mode = 0444; | |||
return NULL; | |||
} | |||
static int __init newgetpid_init(void) | |||
{ | |||
int retval; | |||
newgetpid_major = register_chrdev(0, DEVICE_NAME, &newgetpid_fops); | |||
if (newgetpid_major < 0) { | |||
err("failed to register device: error %d\n", newgetpid_major); | |||
retval = newgetpid_major; | |||
goto failed_chrdevreg; | |||
} | |||
newgetpid_class = class_create(THIS_MODULE, CLASS_NAME); | |||
if (IS_ERR(newgetpid_class)) { | |||
err("failed to register device class '%s'\n", CLASS_NAME); | |||
retval = PTR_ERR(newgetpid_class); | |||
goto failed_classreg; | |||
} | |||
newgetpid_class->devnode = newgetpid_devnode; | |||
newgetpid_device = device_create(newgetpid_class, NULL, MKDEV(newgetpid_major, 0), | |||
NULL, DEVICE_NAME); | |||
if (IS_ERR(newgetpid_device)) { | |||
err("failed to create device '%s'\n", DEVICE_NAME); | |||
retval = PTR_ERR(newgetpid_device); | |||
goto failed_devreg; | |||
} | |||
info("Newgetpid device registered using major %d.\n", newgetpid_major); | |||
return 0; | |||
failed_devreg: | |||
class_unregister(newgetpid_class); | |||
class_destroy(newgetpid_class); | |||
failed_classreg: | |||
unregister_chrdev(newgetpid_major, DEVICE_NAME); | |||
failed_chrdevreg: | |||
return -1; | |||
} | |||
static void __exit newgetpid_exit(void) | |||
{ | |||
device_destroy(newgetpid_class, MKDEV(newgetpid_major, 0)); | |||
class_unregister(newgetpid_class); | |||
class_destroy(newgetpid_class); | |||
unregister_chrdev(newgetpid_major, "newgetpid"); | |||
info("Unloading Newgetpid module.\n"); | |||
return; | |||
} | |||
module_init(newgetpid_init); | |||
module_exit(newgetpid_exit); | |||
MODULE_LICENSE("GPL"); | |||
MODULE_AUTHOR("Anil Somayaji <soma@scs.carleton.ca>"); | |||
MODULE_DESCRIPTION("A write newgetpid character device module"); | |||
</source> | |||
===Makefile=== | |||
<source lang=make line> | |||
obj-m := newgetpid.o | |||
KDIR := /lib/modules/$(shell uname -r)/build | |||
PWD := $(shell pwd) | |||
default: | |||
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules | |||
</source> | </source> |
Revision as of 02:02, 15 October 2018
In this tutorial you will be creating filesystems and installing kernel modules. It is highly recommended that you use a comp3000 openstack instance for the exercises below for two reasons. In particular you may have difficulties compiling kernel modules on other systems. You will need root access to install kernel modules and mount filesystems. These operations are potentially dangerous and mistakes could destroy all data on the Linux system. Consider yourself warned!
Filesystems
- Run ls -lai (by itself or for a specific directory). What are the numbers appearing in the left column?
- dd if=/dev/zero of=foo bs=8192 count=32K What is the logical size of the file? How much space does it consume on disk? (Hint: Look at the size option to ls.)
- Run mkfs.ext4 foo. (If asked, say "yes" to operating on a regular file.) Does foo consume any more space?
- Run dumpe2fs foo. What does the output of this command mean?
- What command do you run to check the filesystem in foo for errors?
- Run sudo mount foo /mnt. How does this command change what files are accessible?
- Run df. What device is mounted on /mnt? What is this device?
- Run rsync -a -v /etc /mnt. What does this command do? Explain the arguments as well.
- Run sudo umount /mnt. What files can you still access, and what have gone away?
- Run dd if=/dev/zero of=foo conv=notrunc count=10 bs=512. How does the "conv=notrunc" change dd's behavior (versus the command in question 1)?
- Run sudo mount foo /mnt. What error do you get?
- What command can you run to make foo mountable again? What characteristic of the file system enables this command to work?
- Run the command truncate -s 1G bar. What is the logical size of bar, and how much space does it consume on disk? How does this compare with foo?
- How does the logical size of bar change when you create an ext4 filesystem in it? What about the space consumed on disk?
A simple kernel module
- Download the source for this simple module, unpack, and build it by typing "make". Use
wget
- Install the module using "sudo insmod simple.ko". The hello message is recorded in the kernel logs. How do you view the kernel logs?
- Check to see that the module has been loaded. How do you do this?
- Remove the module from the kernel. What did you do?
A character device kernel module
- Download the source for ones, a kernel module implementing a character device that ouputs an unbounded string of "1"'s. Build, compile, and run it as before.
- What kernel messages does the module generate? Does it create any new files (other than /dev/ones)? If so, where?
- What happens when you "cat" the device /dev/ones? How can you limit the output?
- How can you modify your module to generate a kernel "Oops" as reported in the kernel logs or outright crash the kernel?
Simple module
simple.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
static int __init simple_init(void)
{
printk ("Hello kernel world!\n");
return 0;
}
static void __exit simple_exit(void)
{
printk ("Goodbye kernel world.\n");
return;
}
module_init(simple_init);
module_exit(simple_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Somayaji <soma@scs.carleton.ca>");
MODULE_DESCRIPTION("A simple module");
Makefile
obj-m := simple.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
Ones module
ones.c
/* Code derived from:
https://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/
and
http://pete.akeo.ie/2011/08/writing-linux-device-driver-for-kernels.html
*/
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#define dbg(format, arg...) do { if (debug) pr_info(CLASS_NAME ": %s: " format, __FUNCTION__, ## arg); } while (0)
#define err(format, arg...) pr_err(CLASS_NAME ": " format, ## arg)
#define info(format, arg...) pr_info(CLASS_NAME ": " format, ## arg)
#define warn(format, arg...) pr_warn(CLASS_NAME ": " format, ## arg)
#define DEVICE_NAME "ones"
#define CLASS_NAME "comp3000"
static struct class* ones_class = NULL;
static struct device* ones_device = NULL;
static int ones_major;
static int ones_open(struct inode *the_inode, struct file *f)
{
return 0;
}
static ssize_t ones_read(struct file *f, char *buf, size_t len, loff_t *offset)
{
size_t i;
for (i = 0; i < len; i++) {
put_user('1', buf++);
}
return i;
}
static int ones_release(struct inode *the_inode, struct file *f)
{
printk(KERN_ALERT "Ones device closed\n");
return 0;
}
static struct file_operations ones_fops = {
.open = ones_open,
.read = ones_read,
.release = ones_release,
};
static char *ones_devnode(struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0444;
return NULL;
}
static int __init ones_init(void)
{
int retval;
ones_major = register_chrdev(0, DEVICE_NAME, &ones_fops);
if (ones_major < 0) {
err("failed to register device: error %d\n", ones_major);
retval = ones_major;
goto failed_chrdevreg;
}
ones_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(ones_class)) {
err("failed to register device class '%s'\n", CLASS_NAME);
retval = PTR_ERR(ones_class);
goto failed_classreg;
}
ones_class->devnode = ones_devnode;
ones_device = device_create(ones_class, NULL, MKDEV(ones_major, 0),
NULL, DEVICE_NAME);
if (IS_ERR(ones_device)) {
err("failed to create device '%s'\n", DEVICE_NAME);
retval = PTR_ERR(ones_device);
goto failed_devreg;
}
info("Ones device registered using major %d.\n", ones_major);
return 0;
failed_devreg:
class_unregister(ones_class);
class_destroy(ones_class);
failed_classreg:
unregister_chrdev(ones_major, DEVICE_NAME);
failed_chrdevreg:
return -1;
}
static void __exit ones_exit(void)
{
device_destroy(ones_class, MKDEV(ones_major, 0));
class_unregister(ones_class);
class_destroy(ones_class);
unregister_chrdev(ones_major, "ones");
info("Unloading Ones module.\n");
return;
}
module_init(ones_init);
module_exit(ones_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Somayaji <soma@scs.carleton.ca>");
MODULE_DESCRIPTION("A write ones character device module");
Makefile
obj-m := ones.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
newgetpid module
newgetpid.c
/* Code derived from:
https://appusajeev.wordpress.com/2011/06/18/writing-a-linux-character-device-driver/
and
http://pete.akeo.ie/2011/08/writing-linux-device-driver-for-kernels.html
*/
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#define dbg(format, arg...) do { if (debug) pr_info(CLASS_NAME ": %s: " format, __FUNCTION__, ## arg); } while (0)
#define err(format, arg...) pr_err(CLASS_NAME ": " format, ## arg)
#define info(format, arg...) pr_info(CLASS_NAME ": " format, ## arg)
#define warn(format, arg...) pr_warn(CLASS_NAME ": " format, ## arg)
#define DEVICE_NAME "newgetpid"
#define CLASS_NAME "comp3000"
static struct class* newgetpid_class = NULL;
static struct device* newgetpid_device = NULL;
static int newgetpid_major;
static int newgetpid_open(struct inode *the_inode, struct file *f)
{
return 0;
}
static ssize_t newgetpid_read(struct file *f, char *buf, size_t len, loff_t *offset)
{
size_t i, msglen;
pid_t thepid;
char message[100];
if (*offset > 0) {
return 0;
}
thepid = task_tgid_vnr(current);
snprintf(message, 100, "Your PID is %d!\n", thepid);
msglen = strlen(message);
if (len < msglen) {
msglen = len;
}
for (i = 0; i < msglen; i++) {
put_user(message[i], buf++);
}
*offset = i;
return i;
}
static int newgetpid_release(struct inode *the_inode, struct file *f)
{
printk(KERN_ALERT "Newgetpid device closed\n");
return 0;
}
static struct file_operations newgetpid_fops = {
.open = newgetpid_open,
.read = newgetpid_read,
.release = newgetpid_release,
};
static char *newgetpid_devnode(struct device *dev, umode_t *mode)
{
if (mode)
*mode = 0444;
return NULL;
}
static int __init newgetpid_init(void)
{
int retval;
newgetpid_major = register_chrdev(0, DEVICE_NAME, &newgetpid_fops);
if (newgetpid_major < 0) {
err("failed to register device: error %d\n", newgetpid_major);
retval = newgetpid_major;
goto failed_chrdevreg;
}
newgetpid_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(newgetpid_class)) {
err("failed to register device class '%s'\n", CLASS_NAME);
retval = PTR_ERR(newgetpid_class);
goto failed_classreg;
}
newgetpid_class->devnode = newgetpid_devnode;
newgetpid_device = device_create(newgetpid_class, NULL, MKDEV(newgetpid_major, 0),
NULL, DEVICE_NAME);
if (IS_ERR(newgetpid_device)) {
err("failed to create device '%s'\n", DEVICE_NAME);
retval = PTR_ERR(newgetpid_device);
goto failed_devreg;
}
info("Newgetpid device registered using major %d.\n", newgetpid_major);
return 0;
failed_devreg:
class_unregister(newgetpid_class);
class_destroy(newgetpid_class);
failed_classreg:
unregister_chrdev(newgetpid_major, DEVICE_NAME);
failed_chrdevreg:
return -1;
}
static void __exit newgetpid_exit(void)
{
device_destroy(newgetpid_class, MKDEV(newgetpid_major, 0));
class_unregister(newgetpid_class);
class_destroy(newgetpid_class);
unregister_chrdev(newgetpid_major, "newgetpid");
info("Unloading Newgetpid module.\n");
return;
}
module_init(newgetpid_init);
module_exit(newgetpid_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Somayaji <soma@scs.carleton.ca>");
MODULE_DESCRIPTION("A write newgetpid character device module");
Makefile
obj-m := newgetpid.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules