Devices and Linux File System Management

From Colwiki.org

Jump to: navigation, search


Outcomes

In this Module you will learn how to:
  • Create partitions and File Systems
  • File Permissions
  • Monitoring Disk Usage
  • Control File System Mounting and Unmounting


Contents

Devices and Linux File System Management

Image:Terminology.png Partitions:

A hard disk partition is a defined storage space on a hard drive. Disk partitioning is the act or practice of dividing the storage space of a hard disk drive into separate data areas known as partitions.

Creating Linux Partitions

One has the choice to associate a piece of hardware (or resource) to a directory. For example the root directory “/” which is more or less like the C:\ drive for DOS could correspond to the /dev/hda2 partition, and the subdirectory /boot could correspond to the partition /dev/hda3. “/dev/hda3 is said to be mounted on /boot”. The directory on which a block device is mounted is then ntuitive installation tools such as DiskDruid.Finally, for beginners and busy sysadmin’s, the latest Linux distributions will automatically assign a partition scheme.

Once the operating system is installed you can use the fdisk utility to configure new partitions. We will next look at the basic syntax for fdisk

  • Start partitioning the first hard drive:
  fdisk  /dev/hda
  • Type m for help. Then create a new partition with n.
  • To write the changes to disk type w.
  • REBOOT

These four points outline the steps you would follow to create new partitions. The last point is often overlooked. This forces the partition table in the master boot record MBR to be reread.

NOTICE

You need to create a filesystem on a new partition with mkfs or mke2fs before using it

This ends the survey of available partitioning tools. We next take a look at bootloaders.

Managed Devices

At boot time the /etc/fstab file assigns mount points for block devices.

The /etc/fstab format

 device    mount-point     fstype       options   dump-number  fsck-number

Sample /etc/fstab

 LABEL=/            /               ext2    defaults                1 1
 LABEL=/boot        /boot           ext2    defaults                1 2
 LABEL=/home        /home           ext3    defaults                1 2
 /dev/fd0           /mnt/floppy     auto    noauto,owner            0 0
 LABEL=/usr         /usr            ext2    defaults                1 2
 LABEL=/var         /var            ext3    defaults                1 2
 none               /proc           proc    defaults                0 0
 none               /dev/shm        tmpfs   defaults                0 0
 none               /dev/pts        devpts  gid=5,mode=620          0 0
 /dev/hdc9          swap,pri=-1     swap    defaults                0 0
 /dev/cdrom         /mnt/cdrom      iso9660 noauto,owner,kudzu,ro   0 0

On a running system the /etc/fstab file also acts as a shortcut for assigning a resource to a specific directory. For example:

 mount /dev/cdrom

The mount utility reads fstab and deduces where to mount the resource. Notice that some of the devices are accessed using a label. Labels are assigned to devices with the tune2fs tool:

 tune2fs -L /usr/local /dev/hdb12

Option summary for mount:

 rw,ro -  read-write and read-only
 users -  the device can be read and unmounted by all users 
 user  -  the device can unmounted only by the user
 owner -  the device will change it's permission and belong to the user that mounted it
 usrquota - start user quotas on the device
 grpquota - start group quotas on the device

NOTICE

Remember that mount -a will mount all filesytems in /etc/fstab that have not been mounted and do not have the option noauto

File Permissions

Image:Permission.png

Permissions can be acted upon with chmod. There are 3 categories of ownership for each file and directory:

The symbolic values for the owner fields:

u: a valid user with an entry in /etc/passwd g: a valid group with an entry in /etc/group o: other

Example:

 -rw-rw-r--    1 jade   sales         24880 Oct 25 17:28 libcgic.a

Changing Permission:

 chmod g=r,o-r libcgic.a 
 chmod g+w  libcgic.a

Changing user and group:

 chown  root  libcgic.a 
 chgrp  apache libcgic.a

NOTICE

A useful option for chmod, chown and chgrp is –R which recursively changes ownership and permissions through all files and directories indicated.


Symbolic and octal notation

Permissions can be read=r, write=w and execute=x. The octal values of these permissions are listed in the next table.

Octal and symbolic permissions.

 Symolic           octal
 read               4
 write              2
 execute            1

Permissions apply to the user, the group and to others. An item has a set of 3 grouped permissions for each of these categories.

How to read a 755 or -rwxr-xr-x permission

 user        group       other
 rwx         r_x         r_x
 4+2+1=7     4+1=5       4+1=5

The standard permission

UNIX systems create files and directories with standard permissions as follows:

Standard permission for:

 Files         666         -rw-rw-rw-
 Directories   777         -rwxrwxrwx

Umask

Every user has a defined umask that alters the standard permissions. The umask has an octal value and is subtracted(*) from the octal standard permissions to give the files permission (this permission doesn't have a name and could be called the file's effective permission).(*) While subtraction works in most cases, it should be noted that technically the standard permissions and the umask are combined as follows:

Final Permissions = Standard Permissions (logical AND) (NOT)Umask

On systems where users belong to separate groups, the umask can have a value of 002. For systems which place all users in the users group, the umask is likely to be 022.

SUID permissions

It is possible for root to give users permission to execute programs they would usually be unable to. This permission is the SUID permission with a symbolic value s or a numerical value 4000. For example root can write a C shell script that executes a program and set the SUID of the script with chmod 4777 script or chmod u+s script. (NB Bourne and Bash scripts do not honour SUID bits set on the script files.)

Examples:

 chmod 4755 /bin/cat
 chmod u+s /bin/grep

SGID permissions

The SGID is a similar permission set for group members. The symbolic value is s and the octal value of 2000.

Setting SGID on a directory changes the group ownership used for files subsequently created in that directory to the directories group ownership. No need to use newgrp to change the effective group of the process prior to file creation.

Examples:

 chmod 2755 /home/data
 chmod g+s /bin/wc

The sticky bit

The sticky bit permission with value 1000 has the following effect:

  1. Applied to a directory it prevents users from deleting files unless they are the owner (ideal for directories shared by a group)
  2. Applied to a file this used to cause the file or executable to be loaded into memory and caused later access or execution to be faster. The symbolic value for an executable file is t while for a non executable file this is T. As file system caching is more generic and faster, file sticky bits tend not be supported any more.
 chmod 1666 /data/store.txt
 chmod o+t /bin/bash


Summary

In this module you learned how to work with partitions and allocate the appropriate rights to files that you will work with. Linux is very particular on the rights issues. The appropriate ownership is required to be able to execute and run applications within Linux.

It is important that you understand how Linux maintains and manages its access rights.




Assignment

Create 2 new partitions (larger than 50M) on the /dev/hda device using fdisk. HINT: To create a new partition type n. The partition type defaults to 83 (Linux)
  • To write the new partition table type w.
  • The partition table needs to be read: REBOOT the computer!

Format the first partition using the ext2 filesystem type and the second with reiserfs. HINT: The mkfs tool is a front for mkfs.ext2 or mkfs.reiserfs, etc. The syntax is

 mkfs –t <fstype> <device>

Make directories in /mnt and mount the new partitions

 mkdir /mnt/ext2
 mkdir /mnt/reiserfs

Use mount to verify which devices are mounted. The permissions set in fstab are visible too.

Use df to check the total number of blocks used. The –k option will convert the number of blocks in kilobytes (the default block size for ext2)

Run fsck on one of the newly created filesystems. The fsck utility is a front for fsck.ext2, fsck.ext3, fsck.reiserfs, etc. The syntax is:

 fsck     <device>

Notice that there are no tools to create ext3 formated partitions. In fact the ext3 format is the same as the ext2 format with a journal added. These are the steps:

 mke2fs /dev/hda10
 tune2fs –j /dev/hda10

At this stage the system has added a journal to the /dev/hda10 partition, making it an ext3 formated partition. This process is non-destructive and reversible. If you mount an ext3 as an ext2 filesystem, the .journal file will be erased. You can add it again with tune2fs. .


Image:somerights20.png This work is licenced under a Creative Commons - By Attribution Licence - Share Alike License.

Personal tools
News & Events