# LPI Linux Certification/LPIC1 Exam 101/Linux Filesystems

* LPIC1 Exam 101
* Detailed_Objectives
* Hardware & Architecture
* Linux Installation & Package Management
* GNU & Unix Commands
* Linux Filesystems
* The X Window System
* Exercises results

## Section Overview

* 1.104.1 Create partitions and filesystems
* 1.104.2 Maintain the integrity of filesystems
* 1.104.3 Control mounting and unmounting filesystems
* 1.104.4 Managing disk quota
* 1.104.5 Use file permissions to control access to files
* 1.104.6 Manage file ownership
* 1.104.7 Create and change hard and symbolic links
* 1.104.8 Find system files and place files in the correct location
* Linux filesystems
* Create partitions and filesystems
* Maintain the integrity of filesystems
* Control mounting and unmounting filesystems
* Managing disk quota
* Use file permissions to control access to files
* Manage file ownership
* Create and change hard and symbolic links

## Create partitions and filesystems
### Overview

Description: Candidates should be able to configure disk partitions and then create filesystems on media such as hard disks. This objective includes using various mkfs commands to set up partitions to various filesystems, including ext2, ext3, reiserfs, vfat, and xfs.
Key files terms and utilities include: fdisk mkfs

### Partitions

Media can be divided into partitions. Partitions are usually created at installation time but can also be created with the fdisk program or other utilities. This will divide the media in partitions and different filesystems can be built and different operating systems can be installed.
IDE is recognized as follows:
* Primary Master:
       /dev/hda : Full disk
       /dev/hda1: First partition
       /dev/hda2: Second partition
* Primary Slave: /dev/hdb
* Secondary Master: /dev/hdc
* Secondary Slave: /dev/hdd
SCSI is recognized as follows:
* ID1:
       /dev/sda: Full disk
       /dev/sda1: First partition
* ID2: /dev/sdb
USB and FireWire disks are recognized as SCSI disks.
Once it is partitioned it is possible to build a filesystem on every partition.

### Filesystems

Filesystems exist to allow you to store, retrieve and manipulate data on a media. Filesystems maintain an internal data structure (meta-data) that keeps all you data organized and accessible. The structure of this meta-data gives the characteristic of the filesystem. A filesystem is accessed by a driver through the organized meta-data structure. When Linux boots it reads in /etc/fstab all the filesystems that needs to be mounted and checks if they are in an usable state.
When a power failure occurs Linux won't be able to unmout the filesystem properly and some data in the cache won't be synchronized on the media. The meta-data may be corrupted.
Once you reboot the system, it will detect this and do a fsck to the full meta-data structure for a consistency check. This can take a very long time. Few minutes to few hours in proportion of the media size. Journaling a filesystem is adding a new data structure called a journal. This journal is on-disk and before each modification to the meta-data is made by the driver it is first written into the journal. Before each meta-data modification the journal maintains a log of the next operation.
Now, when a power failure occurs there is only the need to check the journal. A journaling filesystem recovery is very fast. It just needs to go through the logs and fix the latest operation. Journaling filesystem recovery can take only few seconds.
On a cluster systems, journaling allows to quickly recover a shared partition of a node that went down.

### Linux filesystems

* ext2: Old very stable Linux filesystem. Efficient for files larger than ~2-3K.
* ext3: Journaling extension for ext2. Possible to move a filesystem back and forth between ext2 and ext3.
* Reiserfs: Journaling filesystem. 8-15 time faster than ext2 when manipulating small files.
* XFS: A powerful journaling filesystem with possible quota and ACL
* Msdos: MS-Windows FAT filesystem type. (mainly used for floppy)
* Vfat: MS-Windows FAT filesystem type. (mainly used for large hdd partitions)
* NTFS (ReadOnly but loop files): MS-Windows journaling filesystem
* SMBFS: A filesystem to mount windows or samba sharing from Linux
* NFS: Network File System

### Filesystem tree

A Linux file system has one top directory called root (/) where all sub directories of the entire system is stored. The sub directories can be another partition, a remote directory, or a remote partition accessible through the network with NFS protocol.

### Create filesystems

To create a file system on a partition, use mkfs.
Common options:
    -t: fstype: File system type.
    -c : Check the device for bad blocks before building the filesystem.
The full partition will be erased and organized to the type of filesystem requested. There is no undo command. The fstype possible are: msdos, ext2, ext3, reiserfs,minix,xfs
The blocksize allows to customize the block size for your filesystem.
Examples:

#### Create extended filesystems

To create an extended (ext2, ext3) filesystem on an partition, use mke2fs.
Common options:
    -b: Specify the block sizefstype: File system type.
    -c : Check the device for bad blocks before building the filesystem.
    -j: Create the filesystem with an ext3 journal.
    -L: Set the volume label for the filesystem.
With mke2fs it is possible to store the super-block the journal information on another device. Examples:

### Monitoring disk usage

To print the disk usage, use du.
Common options:
    -a: All files not just directories
    -b: Print size in bytes
    -c: Total
    -h: Human readable format. (1K, 20M,...)
Examples:

### Filesystem disk space

A filesystem is composed of a meta-data structure plus a list of blocks. To print the filesystem disk space usage, use df.
Common options:
    -a: All included filesystems with 0 blocks.
    -t: Limit listing to a filesystem type.
    -h: Human readable format. (1K, 20M,...)
Examples:

### Exercises

* Exercises results.

## Maintain the integrity of filesystems
### Overview

Description: Candidates should be able to verify the integrity of filesystems, monitor free space and inodes, and repair simple filesystem problems. This objective includes the commands required to maintain a standard filesystem, as well as the extra data associated with a journaling filesystem.
Key files terms and utilities include: du df fsck e2fsck mke2fs debugfs dumpe2fs tune2fs

### Checking filesystems

To check filesystems consistency, use fsck.
Common options:
    -A: Go through the /etc/fstab file and try to check all file systems. Typically used at boot time from a script.
    -t fslist: Specify the type of file system to be checked. With -A, only filesystems that match fslist are checked
    -C: Display completion/progression bar.
Common fsck-options:
    -a: Automatically repair.
    -r: Interactively repair.
Examples:

#### Checking extended filesystems

To check extended filesystems consistency, use e2fsck.
Common options:
    -b: Use an alternate super block filename.
    -c: This option makes badblocks program to run and marks all the bad blocks.
    -f: Force checking even if the filesystem seems clean.
    -a or -p: Automatically repair.
    -y: non-interactive mode
Examples:

### Debugging extended filesystems

The debugfs program is an interactive file system debugger. It can be used to examine and change the state of an ext2 file system.
Common commands:
    Help: Give an help on all the command available.
    Stats: Give a statistics of the media.
    Stat file: Give a statistic of the file on the media.
    logdump: Dump the journal's media into a file
Example:

### Dumping extended filesystems info

To print the super block and blocks group information of an extended filesystem, use dumpe2fs.
Common options:
    -b: print the bad blocks of the filesystem.
    -h: Display only the superblock information.
Example:

### Tuning extended filesystems

To tune an extended filesystem, use tune2fs.
Common options:
    -i#: Interval between filesystem checks [d|m|w].
    -l: List the contents of the filesystem superblock.
    -L: Set the volume label of the filesystem.
Examples:

### Exercises

* Exercises results
* Build an ext2 file system, with a block size of 2048 bytes, on a floppy.
* Change the label of the floppy to BACKUP.
* Try to add a journal on the floppy media.
* Use debugfs to validate your floppy file system information, and check when it was last accessed.
* Use watch to monitor the size when you copy a big file.
* Create a shell script to list all files on the floppy bigger than 100 Kb.
* Display file system usage for all MSDOS file systems.
* Which directory MUST exist in / to qualify this OS as Linux?
* What is the file system usage of /proc?

## Attach and detach filesystems

* Control mounting and unmounting filesystems

### Overview

Description: Candidates should be able to configure the mounting of a filesystem. This objective includes the ability to manually mount and unmount filesystems, configure filesystem mounting on bootup, and configure user mountable removable filesystems such as tape drives, floppies, and CDs.
Key files terms and utilities include: /etc/fstab mount umount

### Attach a filesystem

The mount command serves to attach the file system found on some device to the big file tree.
If the device or directory is listed in /etc/fstab you can use the following:
Normally only root has the privilege to mount devices unless it is specified in the /etc/fstab file. Examples:

### Detach a filesystem

To detach a filesystem from the file tree, use umount.
A busy filesystem cannot be unmounted.
* Open files
* Working directory of a process.
Examples:

### File system information

The file /etc/fstab contains all the file systems and related information that will be used when doing a mount -a. (Boot time)
The file /etc/mtab is maintained by the kernel and keeps track on what is or isn't mounted. The /etc/fstab format is:
Common options:
    ro: read only
    noauto: Don't mount automatically
    exec: Can execute binary on the filesystem
    suid: Allow to setuser bit
    user: Allow a user to mount/unmount it
    unhide: hidden file visible
    async: All operations will be done asynchronously
    default: rw, suid, dev, exec, auto, nouser, and async

### Exercises

* Exercises results
* Create a line in /etc/fstab that allows any user to access the floppy disk. Check that you can mount the floppy and can create a file with touch.
* Do the following manipulation:
** Create an ext2 file system on the floppy.
** Mount the floppy.
** Copy all the files /etc/*.conf into the floppy.
** Unmount it. What's happening?
** Mount it back and check that all the files are there.
** Issue the following command:
** Tar cvf /dev/fd0 /etc/*.conf
** Try to mount it back. What's happening?
** Use tar to view the contents of the floppy..

## Managing disk quota
### Overview

Description: Candidates should be able to manage disk quotas for users. This objective includes setting up a disk quota for a filesystem, editing, checking, and generating user quota reports.
Key files terms and utilities include: quota edquota repquota quotaon
* Managing quotas for filesystems
* Printing quota reports
* Managing disk quota

### Quotas

On a system, root can manage the usage of disk space per user and per filsystems. The two limits that can be setup are: The soft limit (soft =) specifies the maximum amount of disk usage a quota user is allowed to have. The hard limit (hard =) specifies the absolute limit on the disk usage a quota user can't go beyond it. There is also the possibility to setup a grace period that will enforce the soft limit only after an amount of time specified.

### Setting up quotas for users

1) The keyword usrquota or/and grpquota must be added in file /etc/fstab for the partition interested.
2) Add in each root filesystems the file user.quota or/and group.quota.
Only root can do the quota administration and once the empty files have been created some disk quota can be set such as:
* Soft limitation on number of files and inodes.
* Hard limitation on number of files and inodes if the grace time is set.

### Setting up quotas for users

3) Check the setting
4) Enable quota on the disk
5) Customize the disk quota limits:

### List quotas

To list quotas for a user or group, use quota.
Common options:
    -u:Default, print user quotas.
    -g:Print group quotas for the group of which the user is a member.
    -q: Print a more terse message, containing only information on filesystems where usage is over quota.
Example:

#### Display a quota report

To display a quota report, use repquota.
Common options:
    -a: Report on all filesystems indicated in /etc/mtab to be read-write with quotas.
    -g: Report for group.
Example:

### Exercises

* Exercises results
* Setup a soft limitation for any users that have their home directory in /home to 500M.
* Change the grace time to 0.
* Log as the user and check if the limitation works..

## File Permissions and Security
### Overview

Unix File Security File and Directory Permissions Default Permissions Changing File Permissions Changing File Owner and Group More Privileges Unix file security The biggest security problem may be you. You own any directory or files you created. You are responsible for accessibility of your files. You decide who can access which files and directories.
In your home directory you will be able to grant different levels of permission to yourself, users in your group and the all other users.

### File and Directory Permissions

The permission of a file or of a directory can be viewed with ls -l.

#### File permissions

Examples of file permissions:
This means read,write, and execution permissions for user toto, read and write permissions for members of group users. No permissions for others. (0760)
This means read and write permissions for user root, read permissions for members of group root and all others. (0644)
Examples of directory permissions:
This means read,write, and execution permissions for user root, read and execution permissions for members of group root and others. (0755)
This means read, write, and execution permissions for user toto, read and execution permissions for members of group users and others. (0755)

### Default permissions

The default permissions when creating a file are 0666 and when creating a directory are 0777. Most of the systems overwrite this at boot time with the program umask. Generally the mask value is 022. It means the write for group and other will be blocked. To check or change the mask value, do:
Examples for file:
Examples for directory:

### Changing file permissions

To change permissions on a file or directory, use chmod. To overwrite the existing permissions, do:
To change add or cancel some permissions without overwriting all the existing permissions, do:
To change in recursive mode, use the -R option.

### Exercises

* Exercises results.

## Manage file ownership
### Overview

Description: Candidates should be able to control user and group ownership of files. This objective includes the ability to change the user and group owner of a file as well as the default group owner for new files.
Key files terms and utilities include: chmod chown chgrp

### Changing file owner and group

To change the owner of a file or directory, use chown.
To change the group of a file or directory, use chgrp.
The programs gpasswd and yast2 allow you to administrate groups.
gpasswd [-A user,...] [-M user,...] group
    -A: Add users with group administrator privileges.
    -M: Add members in group.
Group administrators can add or delete members of the group
Group administrators can set or remove the password for the group.

### More privileges

It is possible to give more privileges to a user when it executes a particular script or program by setting the uid or gid bit of the file.
If the bit is set, the process will inherit the permissions of the owner of the file not the permissions of the user. To set the effective uid or gid, use chmod.
Example of such program is /bin/passwd.
The sticky bit can also be set and can make the program text segment resident in RAM. chmod 1640 [file] (1) The file program stays in RAM.

### Exercises

* Exercises results
1) Write the command line by using letters with chmod to set the following permissions:
2) Write the command line by using octal numbers with chmod to set the following permissions:
3) With the following umask values what would be the files and directories creation permissions?
4) Create two user accounts
Logging in id: tst1, group users, with bash shell, home directory /home/tst1
Logging in id: tst2, group public, with bash shell, home directory /home/tst2
For the two accounts set a password.
Logging in as tst1 and copy /bin/ls into tst1 home directory as myls. Change the owner of myls to tst1 and the permissions to 0710. What does this permission value mean?
Logging in as tst2 and try to use /home/tst1/myls to list your current directory. Does it work ?
Create in /etc/group and /etc/gshadow a new group labo with tst1 and tst2. Change the owner group of myls to labo.
Try again from tst2 account to execute /home/tst1/myls to list your current directory. Does it work?.

## Hard and symbolic links
### Overview

Description: Candidates should be able to create and manage hard and symbolic links to a file. This objective includes the ability to create and identify links, copy files through links, and use linked files to support system administration tasks.
Key files terms and utilities include: ln

### Links

Use link when: You want to create a pathname to a file. Set a shorter or fixed pathname to a file.
To link one file to another, use ln:
Common options:
    -f force: clobber existing link
    -s symbolic link
The default links are hard links (ln without an option). On Windows they're called shortcuts. A hard link can only be created to an existing file on the same physical device, after creation no visible association can be displayed between a link name and a file name. A symbolic link can be created on a file that doesn’t exist yet, the association between the link name and the file name can be viewed with the ls command.

### Linking to a file

The symbolic and hard link can be displayed with ls -l. Symbolic link are indicated with an arrow: link_name->real_filename.
Hard links are indicated with the number of links counter (3-1=2 in this case).
When removing a link name, use rm. Only the link will be removed not the linked file.

### Exercises

* Exercises results
* Create a directory etc and bin in your home directory.
* Copy all the files in recursive mode from /etc to your etc directory and do the same for /bin to bin.
* In your local etc directory rename all files *.conf by *.conf.bak
* Create in your home directory a symbolic link call dir that points to your local bin/ls. Check if dir do execute ls.
* Remove the dir link. Is bin/ls still there?.

## Find system files and place files in the correct location
### Exercices

* Exercises results