advance linux filesystem concepts - inodes, soft links and hard links

6
Laboratory Lecture 1: Advance Filesystem Concepts Filesystems – A Review Commonly used to refer to two distinct concepts The hierarchy of directories and files which humans used to organize data on a system (unified filesystem). The formatting system which the kernel uses to store blocks of data on physical media such as disks(filesystem types). The Unified Filesystem Unix and Linux systems have a unified filesystem Any file, on any disk drive or network share can be accessed through a name beginning with / The unified filesystem is made up of one or more individual filesystems (branches of the unified hierarchy) Each individual filesystem has its own root That root can be grafted onto any directory in the unified filesystem The directory where an individual filesystem is grafted into the unified filesystem is the individual filesystem's mount point An individual filesystem lives on a physical device (such as a disk drive), though not necessarily on the same computer. File Types Files directly contain data Directories provide a hierarchy of files: they can contain both files and other directories Files and directories are both file types. Other file types exist, including device special files Device files provide a way of asking the kernel for access to a given physical device The data that the device file seems to contain is actually the raw sequence of bytes or

Upload: erwinmacaraig

Post on 22-Nov-2014

413 views

Category:

Documents


3 download

TRANSCRIPT

Laboratory Lecture 1: Advance Filesystem Concepts

Filesystems A Review Commonly used to refer to two distinct concepts The hierarchy of directories and files which humans used to organize data on a system (unified filesystem). The formatting system which the kernel uses to store blocks of data on physical media such as disks(filesystem types).

The Unified Filesystem Unix and Linux systems have a unified filesystem Any file, on any disk drive or network share can be accessed through a name beginning with / The unified filesystem is made up of one or more individual filesystems (branches of the unified hierarchy) Each individual filesystem has its own root That root can be grafted onto any directory in the unified filesystem The directory where an individual filesystem is grafted into the unified filesystem is the individual filesystem's mount point An individual filesystem lives on a physical device (such as a disk drive), though not necessarily on the same computer.

File Types Files directly contain data Directories provide a hierarchy of files: they can contain both files and other directories Files and directories are both file types. Other file types exist, including device special files Device files provide a way of asking the kernel for access to a given physical device The data that the device file seems to contain is actually the raw sequence of bytes or

sectors on the device itself Device files are by convention stored under the /dev directory

Inodes and Directories The inode (index node) is the fundamental concept of the Linux and UNIX filesystem. Each object in the filesystem is represented by an inode. Each and every file in Linux (and UNIX) has the following attributes File type Permissions Owner Group File size File access, change and modification time File deletion time Number of links Extended attribute such as append only or 'no one can delete file' including root user (immutability) Access Control List All of the above information are stored in an inode. The inode identifies the file and its attributes (as above) Each inode is identified by a unique inode number within the filesystem. Inode is also known as index number. Technically, an inode is a data structure on a traditional UNIX-style file system. An inode store information about a regular file, directory or other filesystem object.

Symbolic Links A symbolic link (or symlink) is a pseudo-file which behaves as an alternative name for some other file or directory Symbolic links are like shortcuts or references to the actual file or a directory. Symbolic links are used all the time to link libraries and make sure files are in consistent places without moving or copying the original. Links are often used to store multiple copies of the same file in different places but still reference to one file. The 'contents' of the symlink are the real name pointed to When you try to use a file name including a symlink, the kernel replaces the symlink component with its 'contents' and starts again. Symlinks allow you to keep a file (or directory) in one place, but pretend it lives in another

For example, to ensure that an obsolete name continues to work for older software Or to spread data from a single filesystem hierarchy over multiple disk partitions Note that a file's name is stored not in its inode, but in a directory A directory is stored on disk as a list of file and directory names Each name has an inode number associated with it Separating names from inodes names that you can have multiple directory entries referring to the same file.

Hard Links Where symlinks refer to other files by name, a hard link refers to another file by inode number A directory entry contains a name and an inode number So a file's name is not considered to be part of the file itself. You get a hard link when different directory entries on a filesystem refer to the same inode number.

IllustrationA symbolic link refers to filename, which in turn refers to an inodeDirectory entry

inode

Directory entry

data

A hard link is a normal directory entry, referring directly to an inodeDirectory entry

inode

Directory entry

data

Differences/Comparison Between Symlinks and Hard LinksSymlinks Symlinks are distinctly different from normal files, therefore we can distinguish a symlink from the original it points to Hard Links Multiple hard-link style names for the same file are indistinguishable; the term 'hard link' is merely conventional.

Symlinks can point to any type of file Symlinks refer to names, so they can point to files on other filesystems If you rename or delete the original file pointed to by a symlink, the symlink gets broken Symlinks may take up additional disk space (to store the name pointed to)

Hard links may not point to a directory (or some non-Linux systems, to a symlink) Hard links work by inode number, so they can only work within a single filesystem Renaming or deleting the 'original' file pointed to by a hard link has no effect on the hard link Hard links only need as much disk space as a directory entry

Finding Symbolic Links to a File The find command has a -lname option which searches for symbolic links containing some text $ find / -name linktarget This will print the names for all symbolic links whose target is linktarget

Finding Hard Links to a File Hard links can be found by searching for directory entries with a given inode number. First, identify the filesystem and inode number of the file you're interested in: $ df file Filesystem 1k_blocks Used Available Use% Mounted on /dev/sdb1 12358369 8326523 4002569 40% /home $ ls -i file 2010 file Then use find with -inum option to look for directory entries in that filesystem with that inode $ find /home -xdev -inum 2010 The -xdev option prevents find from recursing down into other filesystem.

Examples:1. Determining the inode number$ ls -i /etc/passwd 22998 /etc/passwd

You can use ls -i command to see inode number of a file

$ stat /etc/passwd File: `/etc/passwd' Size: 1893 Blocks: 8 IO Block: 4096 Device: 801h/2049d Inode: 22998 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( Access: 2010-11-20 12:14:57.489234215 +0800 Modify: 2010-09-28 15:50:36.744745063 +0800

regular file 0/ root)

Change: 2010-09-28 15:50:36.781434655 +0800

inode and its attribute are shown using the stat command

2. Many commands used by system administrators in UNIX/Linux OS often give inode numbers to designate a file $ cd /tmp $ touch \la* $ ls -l Try removing la*. 3. Symbolic link example: Go to the /tmp directory $ cd /tmp Make a dircctory named original $ mkdir original Copy /etc/host.conf to original directory $ cd original $ cp /etc/host.conf . List (using ls with options ila )the contents and take note of the inode (first column) $ ls -ila Create a symbolic link to host.conf called linkhost.conf $ ln -s host.conf linkhost.conf A symlink is created with the ln -s command Its syntax: ls -s real-file file-link ls -s real_directory directory-link List out the inodes $ ls -ila Notice how the inode for the link is different. 4. Creating Hard Links In the same directory, create a hard link to host.conf name hardhost.conf $ ln host.conf hardhost.conf List the inodes $ ls -ila Notice how the inode numbers are exactly the same for the hard link and the actual file 5. Effects on hard links and symbolic links on manipulating the original file

Open up linkhost.conf, edit and then save it. $ vim linkhost.conf Open host.conf and take note if the changes have been made $ cat host.conf We move host.conf $ mv host.conf ~ Open again the contents of linkhost.conf $ cat linkhost.conf Take note of the output. We now open hardhost.conf $ cat hardhost.conf What is the output?

In the previous example, the hard link still works even though the original file has been moved. This is because the hard link was linked to the inode the physical reference on the hard drive where the file resides. The symlink on the other hand was linked to the abstract file name and was broken the moment the original file has been moved. What happens if we delete the hard link? Even though the hard link is referenced to the physical location of the file on the hard drive though an inode, removing a hard link will not delete the original file. Symbolic link will remain intact but will point to a non existent file.