Creating partitions and filesystems
A block device is an abstraction layer for any storage device that can be formatted in
fixed-size blocks; individual blocks may be accessed independently of access to
other blocks. Such access is often called random access.
Use the ls -l command to display /dev
entries. The first character on each output line is b for a block device, such as
floppy, CD drive, IDE hard drive, or SCSI hard drive; and c for a character device,
such as a tape drive or terminal.
The fdisk command with the -l option is used to list partitions.
To start fdisk in interactive mode, simply give the name of a disk, such as /dev/hda
or /dev/sdb, as a parameter. Use the p command to display the existing partition on this particular disk. using the n command to create a new partition.
Neither LILO nor GRUB uses the bootable flag. If either of these is installed in the
MBR, then it can boot the Windows/XP partition. You could also install LILO or
GRUB into your /boot partition (/dev/hda2) and mark that partition bootable and
remove the bootable flag from /dev/hda1. Leaving the original MBR can be useful if
the machine is later returned to being a Windows-only machine.
There is no journaling in ext2, and it has largely been replaced by ext3. The ext3 filesystem adds journaling capability to a standard ext2 filesystem
XFS is a filesystem with journaling. It comes with robust features and is optimized for
scalability. XFS aggressively caches in-transit data in RAM, so an uninterruptible
power supply is recommended if you use XFS.
Linux uses the mkfs command to create filesystems and mkswapcommand to make
swap space. The mkfs command is actually a front end to several
filesystem-specific commands such as mkfs.ext3 for ext3 and mkfs.reiserfs
for ReiserFS.
If you wish to add a journal to an existing ext2 system, use the tune2fs command with the -j option.
You can use the reiserfstune command to add a label or display the label on an
existing ReiserFS filesystem.
xfs_admin command with the -L option to add a label to an existing XFS
filesystem. Use the -l option of xfs_admin to display a label. Unlike ext2, ext3 and
ReiserFS, labels are limited to a maximum size of 12 characters.
Many Linux distributions include the cfdisk and sfdisk commands. The cfdisk
command provides a more graphical interface than fdisk, using the ncurses library
functions.
The gparted tool is another graphical partitioning tool, designed for the GNOME
desktop. It uses the GTK+GUI library.
IBM's Journaled File System (JFS), currently used in IBM enterprise servers, is
designed for high-throughput server environments. It is available for Linux and is
included in several distributions. To create JFS filesystems, use the mkfs.jfs
command.
Section 3. Filesystem integrity
a file or directory is contained in a collection of blocks,
and information about the file or directory is contained in an inode.
Both the data blocks and the inode blocks take space on filesystems, so you need to
monitor the space usage to ensure that your filesystems have space for growth.
df displays information about mounted filesystems.
If you want specific information on inode usage, use the -i option on the df
command. You can exclude certain filesystem types using the -x option or restrict
information to just certain filesystem types using the -t option. Use these multiple
time if necessary.
The du command displays information about the filename (or filenames) given as
parameters. If a directory name is given, then du recurses and calculates sizes for
every file and subdirectory of the given directory. The result can be a lot of output.
Fortunately, you can use the -s option to request just a summary for a directory. If
you use du to get information for multiple directories, then add the -c option to get a
grand total. You can also control output format with the same set of size options (-h,
-H, --si, and so on) that are used for df.
The main tool for checking filesystems is fsck, which, like mkfs, is really a front end
to filesystem checking routines for the various filesystem types.
dumpe2fs
Prints the super block and block group descriptor information for an ext2 or
ext3 filesystem.
debugfs
Is an interactive file system debugger. Use it to examine or change the state of
an ext2 or ext3file system.
Tools for Reiserfs filesystems
reiserfstune
Displays and adjusts parameters on ReiserFS filesystems.
debugreiserfs
Performs similar functions to dumpe2fs and debugfs for ReiserFS filesystems.
Tools for XFS filesystems
xfs_info
Displays XFS filesystem information.
xfs_growfs
Expands an XFS filesystem (assuming another partition is available).
xfs_admin
Changes the parameters of an XFS filesystem.
xfs_repair
Repairs an XFS filesystem when the mount checks are not sufficient to repair
the system.
xfs_db
Examines or debugs an XFS filesystem.
Section 4. Mounting and unmounting filesystems
The basic form of the mount command takes two parameters: the device (or other
resource) containing the filesystem to be mounted, and the mount point.
you can mount a filesystem read-only by specifying -o ro. If the
filesystem is already mounted, add remount
Once
this filesystem is mounted, the initialization process runs mount with the -a option to
automatically mount a set of filesystems. The set is specified in the file /etc/fstab.
Section 5. Disk quotas
The first step to enable quotas is to add the usrquota, or grpquota options to the
filesystem definitions in /etc/fstab, according to whether you want to implement user
quotas, group quotas, or both.
The quotaon
command turns on quota checking. Listing 32 shows an example. The following
options are used on both commands:
-a
For all filesystems in /etc/fstab that are enabled for automount
-u
For user quotas (this is the default)
-g
For group quotas
-v
For verbose output
In order to set a quota for a particular
user, use the edquota command.
Normally warnquota is run periodically as a
cron job. See the man pages for cron and warnquota for more information.
Section 6. File permissions and access control
It is also possible to log in as one
user and become another user using the su or sudo -s commands.
If your prompt does not include your user id, then you
can use the whoami command to check your current effective id.
Similarly, you can find out what groups you are in by using the groups command.
You can find out both user and group information using the id command.
Filesystem object types
Code Object type
- Regular file
d Directory
l Symbolic link
c Character special
device
b Block special
device
p FIFO
s Socket
Use the chmod command with the +x option to add
the execute permissions
For
example, using chmod +rwx would set all the read, write, and execute permissions
for a file.
Numeric permissions
Symbolic Octal
rwx 7
rw- 6
r-x 5
r-- 4
-wx 3
-w- 2
--x 1
--- 0
The remaining access mode bit is called the sticky bit. It is represented symbolically
by t and numerically as a 1 in the high-order octal digit. It is displayed in a long
directory listing in the place of the executable flag for other users (the last character),
with the same meaning for upper and lower case as for suid and sgid. If set for a
directory, it permits only the owning user or the superuser (root) to delete or unlink a
file.
The access modes and permissions provide extensive control over who can do what
with files and directories. However, they do not prevent inadvertent deletion of files
by the root user. There are some additional attributes available on various
filesystems that provide additional capabilities. One of these is the immutable
attribute.If this is set, even root cannot delete the file until the attribute is unset.
To change the group of a file, use the chgrp command with a group name and one
or more filenames.
chgrp has a -R option to
allow changes to be applied recursively to all selected files and subdirectories.
The root user can change the ownership of a file using the chown command.
Section 8. Hard and symbolic links
You can use the ln command to create additional hard links to an existing file (but
not to a directory, even though the system sets up . and .. as hard links). If there are
multiple hard links to an inode, then the inode is deleted only when the link count
goes to zero.
FHS root filesystem
Directory Purpose
bin Essential command binaries
boot Static files of the boot loader
dev Device files
etc Host-specific system configuration
lib Essential shared libraries and kernel modules
media Mount point for removable media
mnt Mount point for mounting a filesystem temporarily
opt Add-on application software packages
sbin Essential system binaries
srv Data for services provided by this system
tmp Temporary files
usr Secondary hierarchy
var Variable data
home User home directories (optional)
lib
(optional)
root Home directory for the root user (optional)
You can use the which command to search your path and find out which command
will be executed (if any) when you type a command. Listing 66 shows an example of
finding the fdisk command.
There are some commands that the which command will not find, such as shell
builtins. The type builtin will tell you how a given command string will be evaluated
for execution.
If you want to find more information than just the location of a program, then you can
use the whereis command.
The find command is the Swiss army knife of file searching tools on Linux systems.
To find files by permission, you can use the -perm test along with symbolic
expressions similar to those used with the chmod or umask commands. You can
search for exact permissions, but it is often more useful to prefix the permission
expression with a hyphen to indicate that you want files with those permissions set,
but you don't care about other permissions.
The find command searches all the directories you specify, every time you run it.
To speed things up, you can use another command, locate, which uses a
database of stored path information rather than searching the filesystem every time.
On modern Linux systems, this command is usually
replaced by the slocate command, which stores permissions as well as paths and
thus prevents users from prying into directories that they could not otherwise see.