Linux Installation and Administration – Lesson 5 Tutor: George Papamarkos Topic: Devices in Linux Lesson Outline • Managing Filesystems – Mounting Disks • Managing Devices – Device Files – Loading Device Drivers – Loading Modules Automatically Managing Devices – Device Files • Device Files allow user programs access hw devices on the system through Linux kernel • Device Drives in Linux/Unix are part of the monolithic kernel • Devices are located under the /dev dir – E.g. /dev/ttyS0 for the 1st serial port – /dev/hda2 for the 2nd partition of the 1st IDE HDD drive Managing Devices – Device Files • Some device files do not correspond to actual devices – E.g. /dev/null acts as a byte sink.Write on this device succeeds always but what is written is ignored • Devices are divided to: – Block Devices: Data are read and written in “blocks” • E.g. the IDE hard drives – Character Devices: Data are read and written sequentially • E.g. serial port Managing Devices – Device Files • In ls –l command in /dev dir, the size of the files has been replaced by two numbers separated by a comma. – brw-rw---- 1 root disk 3, 0 November 2 2004 /dev/hda – The 1st value is called major number and the second is called minor number – Major Num: Specifies a particular driver within the kernel – Minor Num: Specifies a particular devices handled by the driver – E.g. All USB devices are handled by the same driver with one major num although for each of them there is a minor num Working with Device Files • Create a device file: mknod -m permissions name type major minor where: • name is the full pathname of the device to create, such as /dev/rft0 • type is either c for a character device or b for a block device • major is the major number of the device • minor is the minor number of the device • -m permissions is an optional argument that sets the permission bits of the new device file to permissions – E.g. mknod /dev/test b 42 0 • Remove a device file: rm /dev/test • You can of course create links between devices using ln command Loading Device Drivers • Drivers are cooperating with kernel and they are: – Compiled within the kernel, or – Loaded like external modules to the kernel (like .dll files in Windows) at runtime • A module is simply a single object file containing all the code for the driver. – E.g. usbcore.o the module for usb devices Loading Device Drivers • The modules are stored (in most of the systems) in /lib/modules/kernelversion where different dirs per module category exist Load a Module • Command: insmod <module_name> – insmod usbcore.o • The module may fail to load due to dependencies from other modules • You have to find by your own and resolve this • To avoid this, create a module database with the command: depmod –a, to store all the info about the module • After that you can replace insmod with modprobe command, which checks the create database and resolves the module dependencies automatically Working with modules • List the already loaded drivers with: – lsmod • Remove a loaded driver with: – Rmmod <module_name> – E.g. rmmod usbcore Mounting Filesystems on Devices • • Filesystem is the way the data are organised and stored (in physical level) on disks Common Filesystems in Linux 1. 2. 3. 4. 5. 6. Ext2/ext3 ReiserFS Swap NFS Vfat /proc filesystem Mounting filesystems • In order to access any filesystem under Linux, you must mount it on a certain directory. • The mount command is used to do this and usually must be executed as root. • The format of this command is: mount -t type device mount-point where type is the type name of the filesystem, device is the physical device where the filesystem resides (the device file in /dev), and mount-point is the directory on which to mount the filesystem. • You have to create the directory before issuing mount. Mounting Filesystems • E.g. – mount -t vfat /dev/hda2 /mnt/windows To mount the windows hda2 FAT32 partition • There are many options to the mount command, which can be specified with the -o switch. • One common option to mount is -o ro, which mounts the filesystem as read-only. E.g. CDROMs mount -t iso9660 -r /dev/cdrom /mnt/cdrom Unmounting Filesystems • • The inverse of mounting a filesystem is, naturally, unmounting it. Unmounting a filesystem has two effects: – synchronizes the system's buffers with the actual contents of the filesystem on disk – it makes the filesystem no longer available from its mount point. • Unmounting is done with the umount command – umount <mount_dir>, e.g. umount mnt/windows /etc/fstab • You can find out what devices are mounted, and where, using the mount command with no arguments • The system automatically mounts several filesystems when the system boots. • This is handled by the file /etc/fstab – Each line in this file is of the format: device mount-point type options e.g. /dev/hda2 /mnt/windows vfat defaults /etc/fstab • The option defaults should be used for most filesystems; it enables a number of other options, such as rw (read-write access), async (buffer I/O to the filesystem in memory asynchronously), and so forth. • Another potentially useful option is umask, which lets you set the default mask for the permission bits, something that is especially useful with some foreign filesystems. /etc/fstab • At boot time mount –a command is called that mounts everything listed in /etc/fstab • A filesystem does not need to be listed in /etc/fstab in order to be mounted, but it does need to be listed there in order to be mounted "automatically" by mount -a