Evolution of ext2 Filesystem:
The standard On-disk Filesystem used by Linux is called the ext2 filesystem or ext2fs, for historical reasons. Linux was originally programmed with a Minix compatible filesystem, to ease exchanging data with Minix development system, but that file system was severely restricted by 14 Character filename limits and the maximum filesystem size of 64MB. The Minix filesystem was superseded by a new filesystem which was christened the extended filesystem (extfs). It was designed by Rémy Card and was implemented in April 1992 as the first file system created specifically for the Linux operating system. It has metadata structure inspired by the traditional Unix File System (UFS) and was designed to overcome certain limitations of the Minix file system.
Rémy Card later redesigned this filesystem to improve performance and scalability and to add a few missing features led to the creation of second extended filesystem (ext2fs). It was introduced in January 1993. A contender of ext2 was xiafs created by Frank Xia and was based on Minix filesystem. Initially, Xiafs was more powerful and more stable than Ext2, but being a fairly minimalistic modification of the Minix file system, it was not very well suited for future extension. The end result was that Xiafs changed very little while ext2 evolved considerably, rapidly improving stability, performance and adding extensions. ext2, after some shakedown time, quickly became the standard file system of Linux. Since then, ext2 has developed into a very mature and robust file system.
ext2 was the default filesystem in several Linux distributions, including Debian and Red Hat Linux, until supplanted more recently by ext3, which is almost completely compatible with ext2 and is a journaling file system. ext2 is still the filesystem of choice for flash-based storage media (such as SD cards, and USB flash drives) since its lack of a journal minimizes the number of writes and flash devices have only a limited number of write cycles.
ext2 Disk Data Structures:
A file system usually comprises of blocks of data. We have two kinds of blocks: Physical and Logical. Physical blocks are those that reside on the actual storage media, where the data is kept and has a fixed size. Logical blocks, on the other hand, are those whose size is specified once the filesystem is created. Logical blocks are further divided into smaller logical units called as fragments. A logical block consists of an integral number of fragments. This logical block size need not be the same as the physical block size. It is the job of the file system driver to provide the mapping from the logical block size to the physical block size. A single logical block is divided into an integral number of physical blocks. Ext2 has a default logical file size of 4k.
The filesystem volume starts with block 0, which is called the ‘boot’ block, which is available on the ext2 filesystem too. This first block in each ext2 partition is never managed by the ext2 Filesystem, because it is reserved for the partition’s boot sector, which might be used to store the boot loader of the Linux Operating System. This Boot Loader can either be loaded by a small program in the MBR or may be selected by a user when he/she is presented with multiple options operating systems available on the HDD by the bootloader of another Operating System.
The blocks on disk are divided into groups. Each of these groups duplicates critical information of the file system. Moreover, the presence of block groups on disk allows the use of efficient disk allocation algorithms.
Each group contains the following:
The Super Block:
In the ext2 filesystem, superblock is the area which can be accessed by the superuser only. The superuser is that area of the filesystem, which stores information about the number of free blocks, free inodes, logical block size, the number of times the volume has been mounted, and other accounting information about the filesystem.
The Superblock contains a description of the basic size and shape of this file system. The information within it allows the file system manager to use and maintain the file system. Usually only the Superblock in Block Group 0 is read when the file system is mounted but each Block Group contains a duplicate copy in case of file system corruption. Amongst other information it holds the:
Magic Number: This allows the mounting software to check that this is indeed the Superblock for an EXT2 file system. For the current version of EXT2 this is 0xEF53.
Revision Level: The major and minor revision levels allow the mounting code to determine whether or not this file system supports features that are only available in particular revisions of the file system. There are also feature compatibility fields which help the mounting code to determine which new features can safely be used on this file system,
Mount Count and Maximum Mount Count: Together these allow the system to determine if the file system should be fully checked. The mount count is incremented each time the file system is mounted and when it equals the maximum mount count the warning message "maximal mount count reached, running e2fsck is recommended" is displayed,
Block Group Number: The Block Group number that holds this copy of the Superblock,
Block Size: The size of the block for this file system in bytes, for example 1024 bytes,
Blocks per Group: The number of blocks in a group. Like the block size this is fixed when the file system is created,
Free Blocks: The number of free blocks in the file system,
Free Inodes: The number of free Inodes in the file system,
First Inode: This is the inode number of the first inode in the file system. The first inode in an EXT2 root file system would be the directory entry for the '/' directory.
Group Descriptors:
Immediately following the Super block on the disk are the group descriptors. They hold critical information about the groups in the filesystem. Each group descriptor describes one group.
Each Block Group has a data structure describing it. Like the Superblock, all the group descriptors for all of the Block Groups are duplicated in each Block Group in case of file system corruption.
Each Group Descriptor contains the following information:
Blocks Bitmap: The block number of the block allocation bitmap for this Block Group. This is used during block allocation and deallocation,
Inode Bitmap:The block number of the inode allocation bitmap for this Block Group. This is used during inode allocation and deallocation,
Inode Table:The block number of the starting block for the inode table for this Block Group. Each inode is represented by the EXT2 inode data structure described below.
Free blocks count, Free Inodes count, Used directory count
The group descriptors are placed on after another and together they make the group descriptor table. Each Blocks Group contains the entire table of group descriptors after its copy of the Superblock. Only the first copy (in Block Group 0) is actually used by the EXT2 file system. The other copies are there, like the copies of the Superblock, in case the main copy is corrupted.
Block Bitmap of the Group: In order to account for the usage of the blocks on the filesystem, the ext2 filesystem consists of a block bitmap. This keeps track of blocks that have been used and those that are free. Each bit in the Block Bitmap denotes an integral number of fragments. So if a bit is allocated to a file and marked as used, then an entire set of fragments are allocated to it.
The Block Bitmap is a clever way to keep track of new empty and old used ones. In order to look for a block, one needs to check the group to which the file belongs. Then the Block Bitmap of the appropriate `Group’ is selected and searched for the required block.
Inode Bitmap of the Group: Similar to the blocks, the inodes assigned to the various files need to be taken into account. The inode stores all information about a file. A fixed number of blocks are usually allocated for storing the Inode Table, which stores all the file inodes. The inodes also contain pointers to disk blocks where the actual files are stored. The Inode bitmap is provided to check for allocated blocks and free them when files are deleted from the system.
Inode Table of the group: Inode Table contains an array of records (inodes), which are basically arrays of structures, containing Meta information about the file. This information includes the filename, the file size, a pointer to the disk blocks containing the file, the file creation, access and modification times, the number of links to the file, as well as the user and group ids of the file. The structures of the pointers to disk blocks are arranged in such a manner that they can be easily accessed.
The inode contains 15 pointers to blocks. Of these pointers, the first 12 are direct pointers to data. The following entry is indirect pointers to data. The next one points to `a block of pointers’ to `blocks of pointers’ to `data’ (double indirect). And another entry points to a `block of pointers’ to `a block of pointers’ to `a block of pointers’ to `data’ (triple indirect). After these data structures follow the disk blocks on which the actual blocks of data are stored.
In the EXT2 file system, the inode is the basic building block; every file and directory in the file system is described by one and only one inode. The EXT2 inodes for each Block Group are kept in the inode table together with a bitmap that allows the system to keep track of allocated and unallocated inodes. Figure 9.2 shows the format of an EXT2 inode, amongst other information, it contains the following fields:
Mode: This holds two pieces of information; what this inode describes and the permissions that users have to it. For EXT2, an inode can describe one of file, directory, symbolic link, block device, character device or FIFO.
Owner Information: The user and group identifiers of the owners of this file or directory. This allows the file system to correctly allow the right sort of accesses,
Size: The size of the file in bytes,
Timestamps: The time that the inode was created and the last time that it was modified,
Datablocks: Pointers to the blocks that contain the data that this inode is describing. The first twelve are pointers to the physical blocks containing the data described by this inode and the last three pointers contain more and more levels of indirection. For example, the double indirect blocks pointer points at a block of pointers to blocks of pointers to data blocks. This means that files less than or equal to twelve data blocks in length are more quickly accessed than larger files.
It should be should noted that EXT2 inodes can describe special device files. These are not real files but handles that programs can use to access devices. All of the device files in /dev are there to allow programs to access Linux's devices. For example the mount program takes as an argument the device file that it wishes to mount.
Directories:
A `directory, is used for logical organization and security of files on a file system. It is a standard feature of most file systems. On an ext2-based system, a directory has a specific structure. This structure contains the length of the record, the inode number associated with it, as well as the name and the length of the filename. The first two entries are ‘.’ and ‘..’ respectively for every directory.
Easy operations:
It is interesting to note how some of these features make common operations on a filesystem a lot easier. Take the example of moving a file or a directory to another directory. Here we merely change the inode number for the ‘..’ entry in the directory, and this accomplishes our ‘move’ operation. Creating a symlink to a file simply requires a change in the inode entry of the newly created ‘symlink’ file.
Access Control Lists:
The Ext2 filesystem provides support for Access Control Lists (ACL), although they are not fully used. ACLs basically consist of a list of resources for which users of a system give requests for access. Accesses are of three types: Read, Write and Execute. Control Lists store the permissions for each resource for each user of those resources. Any access to the resources is granted only after checking with the Access Control List. Once permitted, the ACL provides mutual exclusion for resources, although it may allow read/share access depending on the policies.
Troubleshooting the ext2 Filesystem:
A sector may contain many Bad Blocks. When one or more of these blocks get irrevocably corrupted, e.g. for physical reasons such as decaying of disk surface, mechanical shock, etc., the entire sector gets marked as ‘bad’ and the inaccessible blocks get remapped onto spare blocks provided by the disk itself. This is very common and some utilities like smartd can provide us with no. of bad sectors present on our disk. As a general rule the disk itself can take care of these by remapping the block to free spare sectors.
In order to force check for Bad blocks, we can use the badblocks command:
#badblocks -b 4096 -sv n /dev/sda1 –o badblocks.out
This command checks the filesystem for bad sectors/blocks and if it finds any, it will store their information into the file badblocks.out. Note that it is necessary to specify the blocksize of the filesystem in order to get the precise idea of which blocks are marked as bad. We can use this file later to perform a filesystem check:
#e2fsck –l badblocks.out /dev/sda1
In this way the command e2fsck will use the bad blocks on the list to perform its operation. However it is much more convenient and safe to run e2fsck with a –cc option (twice the –c option means it does a safe non-destructive check for Bad Blocks).
# e2fsck –cc /dev/sda1
Sometimes it may be necessary to perform very delicate operations on files or directories. Examples include salvaging what is still recoverable from the curroupted filesystem or working at a lower level with bloclks and inodes. The debugfs utility is used for this purpose.
The debugfs utility may be used for undeleting accidently removed files. However this will work only on an ext2 filesystem, and not on ext3. Infact when a file is deleted from an ext3 filesystem, not only does the inode gets unlinked, from the associated filename, but the block pointer gets zeroed. As a result the data will still be on the disk but without anything pointing to it. Without using third party application such as foremost, which scans the entire block space in search of certain patterns, it is unlikely that you can recover any data once you remove a file in the ext3 filesystem.
It is often said that an ext3 filesystem is simply an ext2 filesystem with a journal, we now know that this is not the only significant difference.
When attempting to recover a deleted file from an ext2 filesystem, follow these steps:
1. Unmount the filesystem:
#umount /dev/sda1
2. Open the filesystem in read/write mode for debugging purposes:
#debugfs –w /dev/sda1
This will return the debugfs command prompt.
3. Use the lsdel command to determine if any deleted inodes exist. Save the data into a file so that we can recover the associated data, and then exit out of debugfs:
debugfs: lsdel
Inode Owner Mode Size Blocks Time deleted
6044 0 100644 5 1 1 Mon Feb 18 16:30:00 2010
debugfs: dump 6044/root/output.file
debugfs: quit
4. Analyse the resulting file:
#file /root/output.file
output.file OpenDocument Spreadsheet
The debugfs command has many uses. For Example, you can determine which inode is associated with a filesystem block:
debugfs: icheck 48091
Block Inode number
48091 10085
Once you know the inode number, you can determine the filename associated with the inode:
debugfs: ncheck 10085
Inode Pathname
10085 /vmlinuz-2.6.18-53.1.6.el5
Linux Administration and Troubleshooting
Monday, September 13, 2010
Wednesday, August 18, 2010
Linux Superblock: Basic Concept and Troubleshooting
When you create file system on hard drive it will sub divided into multiple file system blocks. Blocks are used for:
* To store user data
* Some blocks used for to store file system's metadata.
The content of files is stored in contiguous blocks of 1024 bytes on a Red Hat Linux System by default (the actual size depends on command line parameters passed to mke2fs when the file system was first created and can be 1024, 2048 or 4096 bytes).
Each file system is different and they have type like ext2, ext3 etc. Further each file system has size like 5 GB, 10 GB and status such as mount status. In short each file system has a superblock, which contains information about file system such as:
* File system type
* Size
* Status
* Information about other metadata structures
The Superblock contains a description of the basic size and shape of this file system. The information within it allows the file system manager to use and maintain the file system. Usually only the Superblock is read when the file system is mounted but each Block Group contains a duplicate copy in case of file system corruption. Amongst other information it holds the:
Magic Number: This allows the mounting software to check that this is indeed the Superblock for an EXT2 file system. For the current version of EXT2 this is 0xEF53.
Revision Level: The major and minor revision levels allow the mounting code to determine whether or not this file system supports features that are only available in particular revisions of the file system. There are also feature compatibility fields which help the mounting code to determine which new features can safely be used on this file system, Mount Count and Maximum Mount Count Together these allow the system to determine if the file system should be fully checked. The mount count is incremented each time the file system is mounted and when it equals the maximum mount count the warning message ``maximal mount count reached, running e2fsck is recommended'' is displayed.
Block Group Number: The Block Group number that holds this copy of the Superblock,
Block Size: The size of the block for this file system in bytes, for example 1024 bytes.
Blocks per Group: The number of blocks in a group. Like the block size this is fixed when the file system is created,
Free Blocks: The number of free blocks in the file system,
Free Inodes: The number of free Inodes in the file system,
First Inode: This is the inode number of the first inode in the file system. The first inode in an EXT2 root file system would be the directory entry for the '/' directory.
The dumpe2fs command displays information from the superblock:
#dumpe2fs -h /dev/sda1
Filesystem volume name: /boot
Last mounted on: "not available"
Filesystem UUID: b8c5462a-76a6-481a-826c-b56e525451bc
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
[ output truncated ]
If for some reason the superblock gets corrupted or becomes unreadable, the entire filesystem becomes unusable. In order to avoid this problem a backup copy of the information contained into the superblock gets stored on the filesystem at regular intervals, depending on the blockize, creating what are actually called the backup superblocks. If the main superblock is not recoverable, there is a chance we can use the information provided by the backup superblocks in order to restore the filesystem to a usable state.
To use a backup superblock, we must first locate them. To locate them, it helps us to know the blocksize used during the filesystem creation.
We can use of the utility mke2fs with the non-destructive option -n in order to see what backup superblocks would be created on the filesystemgiven a certain blocksize:
#mke2fs -b 1024 -n /dev/sda1
Superblock backups stored on blocks:
8194 24578 40962 [ output truncated ]
This will not actually make a filesystem, but will pretend to, displaying the output as if it had made a filesystem using a block-size of 1K.
We can now test the validity of the superblock backup by trying to mount the partition using the information using information provided by the superblock, avoiding the default superblockwhich is presumable corrupted.
#mount -o sb=8194 /dev/sda1 /path-to-mountpoint
If mounts successfuly, then it is likely that only the first superblock is curroupted; the backup superblocks are sane. You can restore the main superblock by running a filesystem consistency check on filesystem:
#e2fsck -b 8194 -B 1024 /dev/sda1
The e2fsck command will ensure that primary superblock is properly updated upon completion of the check.
In the most unlikely case where the most backup superblocks are corrupted you can manually force the rebuild of all superblocks, issuing something like this:
#mke2fs -S -b 1024 /dev/sda1
WARNING: This will force the recreation of all the superblocks and block descriptors only. This means that it will not touch the actual data. For this reason you must apply the right block size to the command. However there is no certainity this will restore the filesystem to an operating state. Use it at your own risk as the very last resort.
* To store user data
* Some blocks used for to store file system's metadata.
The content of files is stored in contiguous blocks of 1024 bytes on a Red Hat Linux System by default (the actual size depends on command line parameters passed to mke2fs when the file system was first created and can be 1024, 2048 or 4096 bytes).
Each file system is different and they have type like ext2, ext3 etc. Further each file system has size like 5 GB, 10 GB and status such as mount status. In short each file system has a superblock, which contains information about file system such as:
* File system type
* Size
* Status
* Information about other metadata structures
The Superblock contains a description of the basic size and shape of this file system. The information within it allows the file system manager to use and maintain the file system. Usually only the Superblock is read when the file system is mounted but each Block Group contains a duplicate copy in case of file system corruption. Amongst other information it holds the:
Magic Number: This allows the mounting software to check that this is indeed the Superblock for an EXT2 file system. For the current version of EXT2 this is 0xEF53.
Revision Level: The major and minor revision levels allow the mounting code to determine whether or not this file system supports features that are only available in particular revisions of the file system. There are also feature compatibility fields which help the mounting code to determine which new features can safely be used on this file system, Mount Count and Maximum Mount Count Together these allow the system to determine if the file system should be fully checked. The mount count is incremented each time the file system is mounted and when it equals the maximum mount count the warning message ``maximal mount count reached, running e2fsck is recommended'' is displayed.
Block Group Number: The Block Group number that holds this copy of the Superblock,
Block Size: The size of the block for this file system in bytes, for example 1024 bytes.
Blocks per Group: The number of blocks in a group. Like the block size this is fixed when the file system is created,
Free Blocks: The number of free blocks in the file system,
Free Inodes: The number of free Inodes in the file system,
First Inode: This is the inode number of the first inode in the file system. The first inode in an EXT2 root file system would be the directory entry for the '/' directory.
The dumpe2fs command displays information from the superblock:
#dumpe2fs -h /dev/sda1
Filesystem volume name: /boot
Last mounted on: "not available"
Filesystem UUID: b8c5462a-76a6-481a-826c-b56e525451bc
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
[ output truncated ]
If for some reason the superblock gets corrupted or becomes unreadable, the entire filesystem becomes unusable. In order to avoid this problem a backup copy of the information contained into the superblock gets stored on the filesystem at regular intervals, depending on the blockize, creating what are actually called the backup superblocks. If the main superblock is not recoverable, there is a chance we can use the information provided by the backup superblocks in order to restore the filesystem to a usable state.
To use a backup superblock, we must first locate them. To locate them, it helps us to know the blocksize used during the filesystem creation.
We can use of the utility mke2fs with the non-destructive option -n in order to see what backup superblocks would be created on the filesystemgiven a certain blocksize:
#mke2fs -b 1024 -n /dev/sda1
Superblock backups stored on blocks:
8194 24578 40962 [ output truncated ]
This will not actually make a filesystem, but will pretend to, displaying the output as if it had made a filesystem using a block-size of 1K.
We can now test the validity of the superblock backup by trying to mount the partition using the information using information provided by the superblock, avoiding the default superblockwhich is presumable corrupted.
#mount -o sb=8194 /dev/sda1 /path-to-mountpoint
If mounts successfuly, then it is likely that only the first superblock is curroupted; the backup superblocks are sane. You can restore the main superblock by running a filesystem consistency check on filesystem:
#e2fsck -b 8194 -B 1024 /dev/sda1
The e2fsck command will ensure that primary superblock is properly updated upon completion of the check.
In the most unlikely case where the most backup superblocks are corrupted you can manually force the rebuild of all superblocks, issuing something like this:
#mke2fs -S -b 1024 /dev/sda1
WARNING: This will force the recreation of all the superblocks and block descriptors only. This means that it will not touch the actual data. For this reason you must apply the right block size to the command. However there is no certainity this will restore the filesystem to an operating state. Use it at your own risk as the very last resort.
Tuesday, August 10, 2010
How to boot a Red Hat Linux System if either MBR is corrupt or Rescue mode is unavailable:
Sometimes we face a problem when our system fails to boot because of Corrupt MBR and sometimes our luck is so bad that there is no rescue environment available.
In such a case how should we move things such that we are in a position to either repair MBR or recover our Data?
Well, faced with such a situation you should log on to another machine and find a file named stage2_eltorito.
This file is a special stage-2 for grub designed to run CD-ROM instead of a Hard-Drive.
#find / -name stage2_eltorito
You can also locate the "stage2_eltorito" file using the rpm command:
#rpm -ql grub | grep eltorito
Now create a directory and copy the "stage2_eltorito" file into that directory.
#mkdir -p /tmp/iso/boot/grub
Copy the "stage2_eltorito" file into the newly created directory (/tmp/iso/boot/grub):
#cp /usr/share/grub/i386-redhat/stage2_eltorito /tmp/iso/boot/grub
Now create the ISO Image of the "stage2_eltorito" file:
#cd /tmp/iso
#mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o /tmp/grub.iso
You can now burn the /tmp/grub.iso file onto a CD and boot it. From there you can use the full GRUB command-line and restore the MBR if it is curroupted, oe you can just load the config file on the system to get to the normal boot menu and boot your machine.
When you boot your system now using the GRUB iso CD, you will get the grub> prompt as shown below. Set the GRUB Root:
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
Find the "grub.conf" file:
grub> find /grub/grub.conf
(hd0,0)
grub> configfile (hd0,0)/grub/grub.conf
This will load the normal grub menu which you can use to boot your machine or do further GRUB maintenance.
Catch: What if the configuration file "grub.conf" is missing?
If the configuration file does not exist, you will need to locate the partitions that contain the kernel and initial RAM-Disk (initrd). Now first set the GRUB Root:
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
And then use the tab completion to get the list of all the files in this directory.
grub> cat /TAB
This will cause the GRUB to display a list of all the files contained in that directory. You should see the full names of both the kernel and the initial RAN-Disk. With this information, it should now be possible to construct the kernel and initrd lines, thus booting the system without configuration file:
grub> kernel /vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet
grub> initrd /initrd-2.6.18-53.el5.img
grub> boot
This will boot the system.
In such a case how should we move things such that we are in a position to either repair MBR or recover our Data?
Well, faced with such a situation you should log on to another machine and find a file named stage2_eltorito.
This file is a special stage-2 for grub designed to run CD-ROM instead of a Hard-Drive.
#find / -name stage2_eltorito
You can also locate the "stage2_eltorito" file using the rpm command:
#rpm -ql grub | grep eltorito
Now create a directory and copy the "stage2_eltorito" file into that directory.
#mkdir -p /tmp/iso/boot/grub
Copy the "stage2_eltorito" file into the newly created directory (/tmp/iso/boot/grub):
#cp /usr/share/grub/i386-redhat/stage2_eltorito /tmp/iso/boot/grub
Now create the ISO Image of the "stage2_eltorito" file:
#cd /tmp/iso
#mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o /tmp/grub.iso
You can now burn the /tmp/grub.iso file onto a CD and boot it. From there you can use the full GRUB command-line and restore the MBR if it is curroupted, oe you can just load the config file on the system to get to the normal boot menu and boot your machine.
When you boot your system now using the GRUB iso CD, you will get the grub> prompt as shown below. Set the GRUB Root:
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
Find the "grub.conf" file:
grub> find /grub/grub.conf
(hd0,0)
grub> configfile (hd0,0)/grub/grub.conf
This will load the normal grub menu which you can use to boot your machine or do further GRUB maintenance.
Catch: What if the configuration file "grub.conf" is missing?
If the configuration file does not exist, you will need to locate the partitions that contain the kernel and initial RAM-Disk (initrd). Now first set the GRUB Root:
grub> root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
And then use the tab completion to get the list of all the files in this directory.
grub> cat /TAB
This will cause the GRUB to display a list of all the files contained in that directory. You should see the full names of both the kernel and the initial RAN-Disk. With this information, it should now be possible to construct the kernel and initrd lines, thus booting the system without configuration file:
grub> kernel /vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet
grub> initrd /initrd-2.6.18-53.el5.img
grub> boot
This will boot the system.
Monday, August 9, 2010
SUID Concept with Demonstration in Red Hat Linux
SUID stands for Set User ID. This means that if the SUID bit is set for any application then your user ID would be set as that of the owner of application/file rather than the current user, while running that application.
That means in case you have an application whose owner is ' root ' and it has its SUID bit set, then when you run this application as a normal user (a user who is not root, and does not have any root credentials), that application would still run as root. Since the SUID bit tells Linux that the the User ID root is set for thisapplication and whenever this application executes it must execute as if root was executing it (since root owns this file).
Let me demonstrate SUID with an example:
If you try to execute a command "useradd" as a normal user, you will get an error as shown below:
$/usr/sbin/useradd user1
-bash: /usr/sbin/useradd: Permission Denied
Now in this case, check the permissions on "useradd" command itself:
$ll /usr/sbin/useradd
-rwxr--r-- 1 root root 74512 Jan 17 2007 /usr/sbin/useradd
The permission set shows that a normal user who is not the owner of the file and belongs to Others category does not have an
execute permission to the "useradd" command.
Now as a root user perform the following operations:
#chmod o+x /usr/sbin/useradd
#ll /usr/sbin/useradd
-rwxr--r-x 1 root root 74512 Jan 17 2007 /usr/sbin/useradd
Now, as a normal user, execute the useradd command again:
$/usr/sbin/useradd user1
useradd: unable to lock password file
Even if others have an execute permission to the "useradd" command, you still get an error "unable to lock password file".
Infact giving write access to others on "/etc/passwd" file will not help and you will get the same error when you try to
execute theuseradd command as ordinary user.
Now what to do????????????
The solution is to set the SUID bit on the "useradd" command as a root user as shown below:
#chmod u+s /usr/sbin/useradd
#ll /usr/sbin/useradd
-rwsr--r-x 1 root root 74512 Jan 17 2007 /usr/sbin/useradd
You will now see a "s" (alphabetically small "s") in place of the execute permission for the owner (i.e. root). This would signify a presence of SUID permission on that command.
Had there been a "S" (alphabetically capital "S") in place of small "s", it would mean that the execute permission is missing from owner's permission for the file.
Now since the SUID bit is set on the "useradd" command, you can now go on to create a user from a normal user's login as shown below:
$/usr/sbin/useradd user1
/usr/sbin/nscd: Only root is allowed to use this option
$grep user1 /etc/passwd
user1:x:508:508::/hime/user1:/bin/bash
Here you might get a warning "/usr/sbin/nscd: Only root is allowed to use this option" after executing the "useradd" command as a normal user, but you need not worry because your user is already created in the system which is shown by the output of grep command on "/etc/passwd" file.
The benefit of setting SUID bit on "useradd" command is that now when you try to execute "useradd" command, the command runs with root's permission and can edit files owned by root.
Note: You need not put Write permission or SUID permission on the files "useradd" command accesses. Only putting SUID on "useradd" command will do the trick.
Catch: Is it possible to include the "useradd" command in a script and then put the SUID for owner and Execute permission for owner and others on the script instead of putting the same permissions on the "useradd" command as we did in the above mentioned steps?
Let's Try:
Remove the SUID bit from the "useradd" command preserving the Execute permission:
#chmod u-s /usr/sbin/useradd
#ll /usr/sbin/useradd
-rwxr--r-x 1 root root 74512 Jan 17 2007 /usr/sbin/useradd
Also write the following script:
#vi /tmp/test
/usr/sbin/useradd user2
grep user2 /etc/passwd
Save the script and put SUID and Execute Permission for User and Others respectively on the script "test" as shown below:
#chmod u+sx,o+x /tmp/test
#ll /tmp/test
-rwsr--r-x 1 root root 46 Jan 17 2007 /tmp/test
Now when you execute the script "/tmp/test" as a normal user, will the "useradd" command inside the script execute?
The answer is "No", as when you try to execute the script, it is "bash shell" which runs the script, and not the normal user.
So the below mentioned command will again give an error as shown below:
$/tmp/test
useradd: unable to lock password file
Warning: The above mentioned example is only to provide information about the working of SUID. Please do not try this on your Production/Support Servers or any other Linux Machines in your Office because it will open a potential Security Loophole using which your Linux Systems could be compromised in many ways.
That means in case you have an application whose owner is ' root ' and it has its SUID bit set, then when you run this application as a normal user (a user who is not root, and does not have any root credentials), that application would still run as root. Since the SUID bit tells Linux that the the User ID root is set for thisapplication and whenever this application executes it must execute as if root was executing it (since root owns this file).
Let me demonstrate SUID with an example:
If you try to execute a command "useradd" as a normal user, you will get an error as shown below:
$/usr/sbin/useradd user1
-bash: /usr/sbin/useradd: Permission Denied
Now in this case, check the permissions on "useradd" command itself:
$ll /usr/sbin/useradd
-rwxr--r-- 1 root root 74512 Jan 17 2007 /usr/sbin/useradd
The permission set shows that a normal user who is not the owner of the file and belongs to Others category does not have an
execute permission to the "useradd" command.
Now as a root user perform the following operations:
#chmod o+x /usr/sbin/useradd
#ll /usr/sbin/useradd
-rwxr--r-x 1 root root 74512 Jan 17 2007 /usr/sbin/useradd
Now, as a normal user, execute the useradd command again:
$/usr/sbin/useradd user1
useradd: unable to lock password file
Even if others have an execute permission to the "useradd" command, you still get an error "unable to lock password file".
Infact giving write access to others on "/etc/passwd" file will not help and you will get the same error when you try to
execute theuseradd command as ordinary user.
Now what to do????????????
The solution is to set the SUID bit on the "useradd" command as a root user as shown below:
#chmod u+s /usr/sbin/useradd
#ll /usr/sbin/useradd
-rwsr--r-x 1 root root 74512 Jan 17 2007 /usr/sbin/useradd
You will now see a "s" (alphabetically small "s") in place of the execute permission for the owner (i.e. root). This would signify a presence of SUID permission on that command.
Had there been a "S" (alphabetically capital "S") in place of small "s", it would mean that the execute permission is missing from owner's permission for the file.
Now since the SUID bit is set on the "useradd" command, you can now go on to create a user from a normal user's login as shown below:
$/usr/sbin/useradd user1
/usr/sbin/nscd: Only root is allowed to use this option
$grep user1 /etc/passwd
user1:x:508:508::/hime/user1:/bin/bash
Here you might get a warning "/usr/sbin/nscd: Only root is allowed to use this option" after executing the "useradd" command as a normal user, but you need not worry because your user is already created in the system which is shown by the output of grep command on "/etc/passwd" file.
The benefit of setting SUID bit on "useradd" command is that now when you try to execute "useradd" command, the command runs with root's permission and can edit files owned by root.
Note: You need not put Write permission or SUID permission on the files "useradd" command accesses. Only putting SUID on "useradd" command will do the trick.
Catch: Is it possible to include the "useradd" command in a script and then put the SUID for owner and Execute permission for owner and others on the script instead of putting the same permissions on the "useradd" command as we did in the above mentioned steps?
Let's Try:
Remove the SUID bit from the "useradd" command preserving the Execute permission:
#chmod u-s /usr/sbin/useradd
#ll /usr/sbin/useradd
-rwxr--r-x 1 root root 74512 Jan 17 2007 /usr/sbin/useradd
Also write the following script:
#vi /tmp/test
/usr/sbin/useradd user2
grep user2 /etc/passwd
Save the script and put SUID and Execute Permission for User and Others respectively on the script "test" as shown below:
#chmod u+sx,o+x /tmp/test
#ll /tmp/test
-rwsr--r-x 1 root root 46 Jan 17 2007 /tmp/test
Now when you execute the script "/tmp/test" as a normal user, will the "useradd" command inside the script execute?
The answer is "No", as when you try to execute the script, it is "bash shell" which runs the script, and not the normal user.
So the below mentioned command will again give an error as shown below:
$/tmp/test
useradd: unable to lock password file
Warning: The above mentioned example is only to provide information about the working of SUID. Please do not try this on your Production/Support Servers or any other Linux Machines in your Office because it will open a potential Security Loophole using which your Linux Systems could be compromised in many ways.
Sunday, August 8, 2010
How to Format a USB Pen-Drive or a USB Flash-Drive in Red Hat Linux
Connect your pen drive to the system running Linux and let it get detected and mounted. Now open a Terminal, and type a command:
#dmesg | tail
Make sure that you have Root priviledges or you login as root to run the above mentioned command or the commands mentioned below:
The command (#dmesg | tail) lets you know what form of drive letters have Linux assingned to your pendrive. Remember pendrive is detected as a sata device so it starts with letter “sd” in Linux. Consider the output of following command at terminal:
sdb: assuming drive cache: write through
SCSI device sdb: 1994752 512-byte hdwr sectors (1021 MB)
sdb: Write Protect is off
sdb: Mode Sense: 23 00 00 00
sdb: assuming drive cache: write through
sdb:sdb1
sd 2:0:0:0: Attached scsi removable disk sdb
sd 2:0:0:0: Attached scsi generic sg1 type 0
usb-storage: devices scan complete
Note the output that have been shown in bold. The 'sdb' is the extension that Linux has assingned to your pen drive. “b” is the unique character that Linux has assingned to sata drive. The above term 'sdb' may differ in your system so make sure you replace the command below with the appropriate term as occur in your system.
Unmount your pendrive using the following command:
# umount /dev/sdb1 (Replace sdb as appropriate)
mkfs i.e make filesystem is the command used to format the pendrive here. so use mkfs.vfat to make a fat file system for your pendrive or mkfs.ext3 to make a Linux filesystem. Use the following command:
#mkfs.vfat -n ‘Label’ -I /dev/sdb1 (Replace sdb as appropriate)
Here Label is the word you want to assign to your pendrive so choose as suites you.
Once processing is done you are done and your pen drive is formatted.
#dmesg | tail
Make sure that you have Root priviledges or you login as root to run the above mentioned command or the commands mentioned below:
The command (#dmesg | tail) lets you know what form of drive letters have Linux assingned to your pendrive. Remember pendrive is detected as a sata device so it starts with letter “sd” in Linux. Consider the output of following command at terminal:
sdb: assuming drive cache: write through
SCSI device sdb: 1994752 512-byte hdwr sectors (1021 MB)
sdb: Write Protect is off
sdb: Mode Sense: 23 00 00 00
sdb: assuming drive cache: write through
sdb:sdb1
sd 2:0:0:0: Attached scsi removable disk sdb
sd 2:0:0:0: Attached scsi generic sg1 type 0
usb-storage: devices scan complete
Note the output that have been shown in bold. The 'sdb' is the extension that Linux has assingned to your pen drive. “b” is the unique character that Linux has assingned to sata drive. The above term 'sdb' may differ in your system so make sure you replace the command below with the appropriate term as occur in your system.
Unmount your pendrive using the following command:
# umount /dev/sdb1 (Replace sdb as appropriate)
mkfs i.e make filesystem is the command used to format the pendrive here. so use mkfs.vfat to make a fat file system for your pendrive or mkfs.ext3 to make a Linux filesystem. Use the following command:
#mkfs.vfat -n ‘Label’ -I /dev/sdb1 (Replace sdb as appropriate)
Here Label is the word you want to assign to your pendrive so choose as suites you.
Once processing is done you are done and your pen drive is formatted.
Tuesday, August 3, 2010
If the Linux Kernel finds Wrong Memory size at the boot time:
The Linux kernel will detect various settings from your computer configuration. This includes the size of memory you have.
In some cases, it will find the wrong size. For example, it could find only 256 MB of memory when in fact you have 512 MB.
The trick here is to specify the amount of RAM memory you have with the "mem=" parameter.
Here is what you would type when your system boots if you have 512 MB of memory:
•use the arrows to select the boot entry you want to modify on the GRUB splash screen at boot time.
•press e to edit the entry. You will find the following entries as shown below:
root (hd0,0)
kernel /vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet
initrd /initrd-2.6.18-53.el5.img
•use the arrows to go to kernel line
•press e to edit this entry
•Type the entry as shown below at the end of Kernel line:
root (hd0,0)
kernel /vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet linux mem=512M
initrd /initrd-2.6.18-53.el5.img
•press ESC to go back to the parent menu
•press b to boot this kernel
This will tell GRUB to load the linux kernel with 512 MB of memory.
The above steps will be persistent only for the first boot time. Incase you want to make it permanent i.e. at every time the computer boots then make the same change in the "/etc/grub.conf" file in the kernel line below the Linux Operating System Title.
In some cases, it will find the wrong size. For example, it could find only 256 MB of memory when in fact you have 512 MB.
The trick here is to specify the amount of RAM memory you have with the "mem=" parameter.
Here is what you would type when your system boots if you have 512 MB of memory:
•use the arrows to select the boot entry you want to modify on the GRUB splash screen at boot time.
•press e to edit the entry. You will find the following entries as shown below:
root (hd0,0)
kernel /vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet
initrd /initrd-2.6.18-53.el5.img
•use the arrows to go to kernel line
•press e to edit this entry
•Type the entry as shown below at the end of Kernel line:
root (hd0,0)
kernel /vmlinuz-2.6.18-53.el5 ro root=LABEL=/ rhgb quiet linux mem=512M
initrd /initrd-2.6.18-53.el5.img
•press ESC to go back to the parent menu
•press b to boot this kernel
This will tell GRUB to load the linux kernel with 512 MB of memory.
The above steps will be persistent only for the first boot time. Incase you want to make it permanent i.e. at every time the computer boots then make the same change in the "/etc/grub.conf" file in the kernel line below the Linux Operating System Title.
How to add more Swap Space on Red Hat Linux System if you are left with no free Hard Disk space to create a new partition for Swap
You installed a new Linux system, but forgot to set enough swap space for your needs. Do you need to repartition and reinstall? No, the swap utilities on Linux allow you to make a real file and use it as swap space even if you have no free space to make another swap partition.
The trick is to make a file and then tell the swapon program to use it. Here's how to create, for example, a 512 MB swap file on your root partition (of course make sure you have at least 512 MB free on the filesystem on which you are going to create it):
#dd if=/dev/zero of=/swapfile bs=1024 count=524288
This will make a 512 MB file on your hard drive. You now need to initialize it:
#mkswap /swapfile 524288
#sync
And you can then add it to your swap pool:
#swapon /swapfile
With that you have 512 MB of swap added. Don't forget to add the swapon command to your startup files so the command will be repeated at each reboot.
The trick is to make a file and then tell the swapon program to use it. Here's how to create, for example, a 512 MB swap file on your root partition (of course make sure you have at least 512 MB free on the filesystem on which you are going to create it):
#dd if=/dev/zero of=/swapfile bs=1024 count=524288
This will make a 512 MB file on your hard drive. You now need to initialize it:
#mkswap /swapfile 524288
#sync
And you can then add it to your swap pool:
#swapon /swapfile
With that you have 512 MB of swap added. Don't forget to add the swapon command to your startup files so the command will be repeated at each reboot.
Friday, July 9, 2010
Basics of initrd
Many Linux distributions ship a single, generic kernel image that is intended to boot as wide a variety of hardware as possible. The device drivers for this generic kernel image are included as loadable module, as it is not possible to statically compile them all into the one kernel without making it too large to boot from computers with limited memory or from lower-capacity media like floppy disks.
This then raises the problem of detecting and loading the modules necessary to mount the root file system at boot time (or, for that matter, deducing where or what the root file system is).
To further complicate matters, the root file system may be on a software RAID volume, LVM, NFS (on diskless workstations), or on an encrypted partition. All of these require special preparations to mount.
Another complication is kernel support for hibernation, which suspends the computer to disk by dumping an image of the entire system to a swap partition or a regular file, then powering off. On next boot, this image has to be made accessible before it can be loaded back into memory.
To avoid having to hardcode handling for so many special cases into the kernel, an initial boot stage with a temporary root file system—now dubbed early user space—is used. This root file system would contain user-space helpers that would do the hardware detection, module loading and device discovery necessary to get the real root file system mounted.
An image of this initial root file system (along with the kernel image) must be stored somewhere accessible by the Linux bootloader or the boot firmware of the computer. This can be:
• The root file system itself
• A boot image on an optical disc
• A small ext2 or FAT-formatted partition on a local disk (a boot partition)
• A TFTP server (on systems that can boot from Ethernet)
The bootloader will load the kernel and initial root file system image into memory and then start the kernel, passing in the memory address of the image. At the end of its boot sequence, the kernel tries to determine the format of the image from its first few blocks of data:
• In the initrd scheme, the image may be an (optionally compressed) file system image, which is made available in a special block device (/dev/ram) that is then mounted as the initial root file system. The driver for that file system must be compiled statically into the kernel. Many distributions originally used compressed ext2 file system images. Others (including Debian 3.1) used cramfs in order to boot on memory-limited systems, since the cramfs image can be mounted in-place without requiring extra space for decompression.
Once the initial root file system is up, the kernel executes /linuxrc as its first process. When it exits, the kernel assumes that the real root file system has been mounted and executes "/sbin/init" to begin the normal user-space boot process.
• In the initramfs scheme (available in Linux 2.6.13 onwards), the image may be an (optionally compressed) cpio archive. The archive is unpacked by the kernel into a special instance of a tmpfs that becomes the initial root file system. This scheme has the advantage of not requiring an intermediate file system or block drivers to be compiled into the kernel.
On an initramfs, the kernel executes /init as its first process. /init is not expected to exit.
Depending on which algorithms were compiled statically into it, the kernel can currently unpack initrd/initramfs images compressed with gzip, bzip2 and LZMA.
The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system.
The initrd contains a minimal set of directories and executables to achieve this, such as the insmod tool to install kernel modules into the kernel.
In the case of desktop or server Linux systems, the initrd is a transient file system. Its lifetime is short, only serving as a bridge to the real root file system. In embedded systems with no mutable storage, the initrd is the permanent root file system.
The initrd image contains the necessary executables and system files to support the second-stage boot of a Linux system.
Depending on which version of Linux you're running, the method for creating the initial RAM disk can vary. Prior to Fedora Core 3, the initrd is constructed using the loop device. The loop device is a device driver that allows you to mount a file as a block device and then interpret the file system it represents. The loop device may not be present in your kernel, but you can enable it through the kernel's configuration tool (make menuconfig) by selecting Device Drivers > Block Devices > Loopback Device Support. You can inspect the loop device as follows (your initrd file name will vary):
Inspecting the initrd (prior to FC3):
# mkdir temp ; cd temp
# cp /boot/initrd.img.gz .
# gunzip initrd.img.gz
# mount -t ext -o loop initrd.img /mnt/initrd
# ls -la /mnt/initrd
You can now inspect the /mnt/initrd subdirectory for the contents of the initrd. Note that even if your initrd image file does not end with the .gz suffix, it's a compressed file, and you can add the .gz suffix to gunzip it.
Beginning with Fedora Core 3, the default initrd image is a compressed cpio archive file. Instead of mounting the file as a compressed image using the loop device, you can use a cpio archive. To inspect the contents of a cpio archive, use the following commands:
Inspecting the initrd: (RHEL-4, FC-3 or Later)
# mkdir temp ; cd temp
# cp /boot/initrd-2.6.14.2.img initrd-2.6.14.2.img.gz
# gunzip initrd-2.6.14.2.img.gz
# cpio -i --make-directories < initrd-2.6.14.2.img
The result is a small root file system, as shown in Listing 3. The small, but necessary, set of applications are present in the ./bin directory, including nash (not a shell, a script interpreter), insmod for loading kernel modules, and lvm (logical volume manager tools).
Listing 3. Default Linux initrd directory structure
# ls -la
#
drwxr-xr-x 10 root root 4096 May 7 02:48 .
drwxr-x--- 15 root root 4096 May 7 00:54 ..
drwxr-xr-x 2 root root 4096 May 7 02:48 bin
drwxr-xr-x 2 root root 4096 May 7 02:48 dev
drwxr-xr-x 4 root root 4096 May 7 02:48 etc
-rwxr-xr-x 1 root root 812 May 7 02:48 init
-rw-r--r-- 1 root root 1723392 May 7 02:45 initrd-2.6.14.2.img
drwxr-xr-x 2 root root 4096 May 7 02:48 lib
drwxr-xr-x 2 root root 4096 May 7 02:48 loopfs
drwxr-xr-x 2 root root 4096 May 7 02:48 proc
lrwxrwxrwx 1 root root 3 May 7 02:48 sbin -> bin
drwxr-xr-x 2 root root 4096 May 7 02:48 sys
drwxr-xr-x 2 root root 4096 May 7 02:48 sysroot
#
This then raises the problem of detecting and loading the modules necessary to mount the root file system at boot time (or, for that matter, deducing where or what the root file system is).
To further complicate matters, the root file system may be on a software RAID volume, LVM, NFS (on diskless workstations), or on an encrypted partition. All of these require special preparations to mount.
Another complication is kernel support for hibernation, which suspends the computer to disk by dumping an image of the entire system to a swap partition or a regular file, then powering off. On next boot, this image has to be made accessible before it can be loaded back into memory.
To avoid having to hardcode handling for so many special cases into the kernel, an initial boot stage with a temporary root file system—now dubbed early user space—is used. This root file system would contain user-space helpers that would do the hardware detection, module loading and device discovery necessary to get the real root file system mounted.
An image of this initial root file system (along with the kernel image) must be stored somewhere accessible by the Linux bootloader or the boot firmware of the computer. This can be:
• The root file system itself
• A boot image on an optical disc
• A small ext2 or FAT-formatted partition on a local disk (a boot partition)
• A TFTP server (on systems that can boot from Ethernet)
The bootloader will load the kernel and initial root file system image into memory and then start the kernel, passing in the memory address of the image. At the end of its boot sequence, the kernel tries to determine the format of the image from its first few blocks of data:
• In the initrd scheme, the image may be an (optionally compressed) file system image, which is made available in a special block device (/dev/ram) that is then mounted as the initial root file system. The driver for that file system must be compiled statically into the kernel. Many distributions originally used compressed ext2 file system images. Others (including Debian 3.1) used cramfs in order to boot on memory-limited systems, since the cramfs image can be mounted in-place without requiring extra space for decompression.
Once the initial root file system is up, the kernel executes /linuxrc as its first process. When it exits, the kernel assumes that the real root file system has been mounted and executes "/sbin/init" to begin the normal user-space boot process.
• In the initramfs scheme (available in Linux 2.6.13 onwards), the image may be an (optionally compressed) cpio archive. The archive is unpacked by the kernel into a special instance of a tmpfs that becomes the initial root file system. This scheme has the advantage of not requiring an intermediate file system or block drivers to be compiled into the kernel.
On an initramfs, the kernel executes /init as its first process. /init is not expected to exit.
Depending on which algorithms were compiled statically into it, the kernel can currently unpack initrd/initramfs images compressed with gzip, bzip2 and LZMA.
The initial RAM disk (initrd) is an initial root file system that is mounted prior to when the real root file system is available. The initrd is bound to the kernel and loaded as part of the kernel boot procedure. The kernel then mounts this initrd as part of the two-stage boot process to load the modules to make the real file systems available and get at the real root file system.
The initrd contains a minimal set of directories and executables to achieve this, such as the insmod tool to install kernel modules into the kernel.
In the case of desktop or server Linux systems, the initrd is a transient file system. Its lifetime is short, only serving as a bridge to the real root file system. In embedded systems with no mutable storage, the initrd is the permanent root file system.
The initrd image contains the necessary executables and system files to support the second-stage boot of a Linux system.
Depending on which version of Linux you're running, the method for creating the initial RAM disk can vary. Prior to Fedora Core 3, the initrd is constructed using the loop device. The loop device is a device driver that allows you to mount a file as a block device and then interpret the file system it represents. The loop device may not be present in your kernel, but you can enable it through the kernel's configuration tool (make menuconfig) by selecting Device Drivers > Block Devices > Loopback Device Support. You can inspect the loop device as follows (your initrd file name will vary):
Inspecting the initrd (prior to FC3):
# mkdir temp ; cd temp
# cp /boot/initrd.img.gz .
# gunzip initrd.img.gz
# mount -t ext -o loop initrd.img /mnt/initrd
# ls -la /mnt/initrd
You can now inspect the /mnt/initrd subdirectory for the contents of the initrd. Note that even if your initrd image file does not end with the .gz suffix, it's a compressed file, and you can add the .gz suffix to gunzip it.
Beginning with Fedora Core 3, the default initrd image is a compressed cpio archive file. Instead of mounting the file as a compressed image using the loop device, you can use a cpio archive. To inspect the contents of a cpio archive, use the following commands:
Inspecting the initrd: (RHEL-4, FC-3 or Later)
# mkdir temp ; cd temp
# cp /boot/initrd-2.6.14.2.img initrd-2.6.14.2.img.gz
# gunzip initrd-2.6.14.2.img.gz
# cpio -i --make-directories < initrd-2.6.14.2.img
The result is a small root file system, as shown in Listing 3. The small, but necessary, set of applications are present in the ./bin directory, including nash (not a shell, a script interpreter), insmod for loading kernel modules, and lvm (logical volume manager tools).
Listing 3. Default Linux initrd directory structure
# ls -la
#
drwxr-xr-x 10 root root 4096 May 7 02:48 .
drwxr-x--- 15 root root 4096 May 7 00:54 ..
drwxr-xr-x 2 root root 4096 May 7 02:48 bin
drwxr-xr-x 2 root root 4096 May 7 02:48 dev
drwxr-xr-x 4 root root 4096 May 7 02:48 etc
-rwxr-xr-x 1 root root 812 May 7 02:48 init
-rw-r--r-- 1 root root 1723392 May 7 02:45 initrd-2.6.14.2.img
drwxr-xr-x 2 root root 4096 May 7 02:48 lib
drwxr-xr-x 2 root root 4096 May 7 02:48 loopfs
drwxr-xr-x 2 root root 4096 May 7 02:48 proc
lrwxrwxrwx 1 root root 3 May 7 02:48 sbin -> bin
drwxr-xr-x 2 root root 4096 May 7 02:48 sys
drwxr-xr-x 2 root root 4096 May 7 02:48 sysroot
#
Subscribe to:
Posts (Atom)