HUB
Sign inSign up

marlabsdocker/lesson6

By marlabsdocker

•Updated almost 9 years ago

Introduction to Linux. Lesson 6 . File Systems. Part 2.

0

0

marlabsdocker/lesson6 repository overview

++++++++++++++++++++++

⁠Using fsck

⁠++++++++++++++++++++++

The fsck tool (short for File System Check) is used to diagnose and repair file systems that may have become damaged in the course of daily operations. Such repairs might be necessary after a system crash in which the system did not get a chance to fully flush all of its internal buffers to disk. Usually, the system runs the fsck tool automatically during the boot process as it deems necessary (much in the same way Windows runs ScanDisk). If it detects a file system that was not cleanly unmounted, it runs the utility. A file system check will also be run once the system detects that a check has not been performed after a predetermined threshold (e.g., number of mounts or time passed between mounts). Linux makes an impressive effort to automatically repair any problems it runs across and, in most instances, does take care of itself. At this point, you need to run fsck by hand and answer its prompts yourself. If you do find that a file system is not behaving as it should (log messages are an excellent hint of this type of anomaly), you may want to run fsck yourself on a running system. The only downside is that the file system in question must be unmounted in order for this to work. If you choose to take this path, be sure to remount the file system when you are done. The name fsck isn’t the actual title for the ext3 repair tool; it’s actually just a wrapper. The fsck wrapper tries to determine what kind of file system needs to be repaired and then runs the appropriate repair tool, passing any parameters that were passed to fsck. In ext2, the real tool is called fsck.ext2. For the ext3 file system, the real tool is fsck.ext3; for the VFAT file system, the tool is fsck.vfat; and for a ReiserFS file system, the utility is called fsck.reiserfs. For example, when a system crash occurs on an ext2-formatted partition, you may need to call fsck.ext2 directly rather than relying on other applications to call it for you automatically. For example, to run fsck on the /dev/mapper/VolGroup00-LogVol02 file system mounted at the /home directory, you will run the following commands. First, to unmount the file system, type [root@fedora-serverA ~]# umount /home NOTE The preceding step assumes that the /home file system is not currently being used or accessed by any process. Since we know that this particular file system is ext3, we can call the correct utility (fsck.ext3) directly or simply use the fsck utility. Type [root@fedora-serverA ~]# fsck /dev/mapper/VolGroup00-LogVol02 fsck 1.40.8 (12-Jul-2017) e2fsck 1.40.8 (12-Jul-2017) /dev/mapper/VolGroup00-LogVol02: clean, 11/655360 files, 37896/655360 blocks The preceding output shows that the file system is marked clean. To forcefully check the file system and answer yes to all questions in spite of what your operating system (OS) thinks, type [root@fedora-serverA ~]# fsck.ext3 -f -y /dev/mapper/VolGroup00-LogVol02 What If I Still Get Errors? First, relax. The fsck check rarely finds problems that it cannot correct by itself. When it does ask for human intervention, telling fsck to execute its default suggestion is often enough. Very rarely does a single pass of e2fsck not clear up all problems. On the rare occasions when a second run is needed, it should not turn up any more errors. If it does, you are most likely facing a hardware failure. Remember to start with the obvious: Check for reliable power and well-connected cables. Anyone running SCSI systems should verify that they’re using the correct type of terminator, that cables aren’t too long, that SCSI IDs aren’t conflicting, and that cable quality is adequate. (SCSI is especially fussy about the quality of the cables.)

⁠+++++++++++++++++++++

⁠The lost+found Directory

⁠+++++++++++++++++++++

Another rare situation is when fsck finds file segments that it cannot rejoin with the original file. In those cases, it will place the fragment in the partition’s lost+found directory. This directory is located where the partition is mounted, so if /dev/mapper/ VolGroup00-LogVol02 is mounted on /home, for example, then /home/lost+found correlates to the lost+found directory for that particular file system. Anything can go into a lost+found directory—file fragments, directories, and even special files. When normal files wind up there, a file owner should be attached, and you can contact the owner and see if they need the data (typically, they won’t). If you encounter a directory in lost+found, you’ll most likely want to try to restore it from the most recent backups rather than trying to reconstruct it from lost+found. At the very least, lost+found tells you if anything became dislocated. Again, such errors are extraordinarily rare.

⁠+++++++++++++++++++++++

⁠ADDING A NEW DISK

⁠+++++++++++++++++++++++

The process of adding a disk under Linux on the Intel (x 86) platform is relatively easy. Assuming you are adding a disk that is of similar type to your existing disks (for example, adding an IDE disk to a system that already has IDE drives or adding a SCSI disk to a system that already has SCSI drives), the system should automatically detect the new disk at boot time. All that is left is partitioning it and creating a file system on it. If you are adding a new type of disk (like a SCSI disk on a system that only has IDE drives), you may need to ensure that your kernel supports the new hardware. This support can either be built directly into the kernel or be available as a loadable module (driver). Note that the kernels of most Linux distributions come with support for many popular SCSI controllers, but you will occasionally come across troublesome kernel and hardware combinations, especially with the new motherboards that have exotic chipsets. Once the disk is in place, simply boot the system, and you’re ready to go. If you aren’t sure about whether the system can see the new disk, run the dmesg command and see whether the driver loaded and was able to find your disk. For example, [root@fedora-serverA ~]# dmesg | egrep -i "hd|sd|disk"

⁠++++++++++++++++++++++++

⁠Overview of Partitions

⁠++++++++++++++++++++++++

For the sake of clarity, and in case you need to know what a partition is and how it works, let’s do a brief review of this subject. Every disk must be partitioned. Partitions divide the disk into segments, and each segment acts as a complete disk by itself. Once a partition is filled, it cannot automatically overflow onto another partition. Various things can be done with a partitioned disk, such as installing an OS into a single partition that spans the entire disk, installing several different operating systems into their own separate partitions in what is commonly called a “dual-boot” configuration, and using the different partitions to separate and restrict certain system functions into their own work areas. This last reason is especially relevant on a multiuser system, where the content of users’ home directories should not be allowed to overgrow and disrupt important OS functions. Traditional Disk- and Partition-Naming Conventions Under Linux, each disk is given its own device name. The device files are stored under the /dev directory. IDE disks start with the name hdX, where X can range from a through z, with each letter representing a physical device. For example, in an IDE-only system with one hard disk and one CD-ROM, both on the same IDE chain, the hard disk would be /dev/hda and the CD-ROM would be /dev/hdb. Some standard devices are automatically created during system installation, and others are created as they are connected to the system. SCSI disks follow the same basic scheme as IDE, except that instead of starting with hd, they start with sd. Therefore, the first partition on the first SCSI disk would be /dev/ sda1, the second partition on the third SCSI disk would be /dev/sdc2, and so on. NOTE Most newer Linux distributions have replaced the old IDE subsystem with libata. The implication of this is that drive device names that previously started with /dev/hdX are now named /dev/sdX instead. The information provided previously is for legacy systems. When partitions are created, new devices are used. They take the form of /dev/ sdXY, where X is the device letter (as described in the preceding paragraph) and Y is the partition number. Thus, the first partition on the /dev/sda disk is /dev/sda1, the second partition would be /dev/sda2, and so on.

⁠++++++++++++++++++++++++

⁠VOLUME MANAGEMENT

⁠++++++++++++++++++++++++

You may have noticed earlier that we use the terms partition and volume interchangeably in parts of the text. While they are not exactly the same things, the concepts carry over. Volume management is a new approach to dealing with disks and partitions. Instead of viewing a disk or storage entity along partition boundaries, the boundaries are no longer there and everything is now seen as volumes. That made perfect sense, didn’t it? Don’t worry if it didn’t; this is a tricky concept. Let’s try this again with more detail. This new approach to dealing with partitions is called Logical Volume Management (LVM) in Linux. It lends itself to several benefits and removes the restrictions, constraints, and limitations that the concept of partitions imposes. Some of the benefits are ▼ Greater flexibility for disk partitioning ■ Easy online resizing of volumes ■ Easy increases in storage space by simply adding new disks to the storage pool ▲ Use of snapshots Following are some important volume management terms. ▼ Physical Volume (PV) This typically refers to the physical hard disk(s) or other physical storage entity, such as a hardware Redundant Array of Inexpensive Disks (RAID) array or software RAID device(s). There can be only a single storage entity (e.g., one partition) in a PV. ■ Volume Group (VG) Volume groups are used to house one or more physical volumes and logical volumes into a single administrative unit. A volume group is created out of physical volumes. VGs are simply a collection of PVs; however, VGs are not mountable. They are more like virtual raw disks. ■ Logical Volume (LV) This perhaps is the trickiest LVM concept to grasp, because logical volumes (LVs) are the equivalent of disk partitions in a non- LVM world. The LV appears as a standard block device. It is on the LV that we put file systems. It is the LV that gets mounted. It is the LV that gets fsck-ed if necessary. LVs are created out of the space available in VGs. To the administrator, an LV appears as one contiguous partition independent of the actual PVs that make it up. ▲ Extents There are two kinds of extents: physical extents and logical extents. Physical volumes (PVs) are said to be divided into chunks, or units of data, called “physical extents.” And logical volumes (LVs) are said to be divided into chunks, or units of data, called “logical extents.” Creating Partitions and Logical Volumes During the installation process, you probably used a “pretty” tool with a nice graphical user interface (GUI) front-end to create partitions. The GUI tools available across the various Linux distributions vary greatly in looks and usability. One tool that can be used to perform most partitioning tasks, and that has a unified look and feel, regardless of the Linux flavor, is the venerable fdisk utility. Though it’s small and somewhat awkward, it’s a reliable partitioning tool. Furthermore, in the event you need to troubleshoot a system that has gone really wrong, you should be familiar with basic tools such as fdisk. Other powerful command-line utilities for managing partitions are sfdisk, cfdisk, and the much newer parted utility: parted is much more user-friendly and has a lot more built-in functionalities than the other tools have. In fact, a lot of the GUI partitioning tools call the parted program in their back-end. During the installation of the OS as covered in Chapter 2, you were asked to leave some free unpartitioned space. We will now use that free space to demonstrate some LVM concepts by walking through the steps required to create a logical volume. In particular, we will create a logical volume that will house the contents of our current /var directory. Because a separate “/var” volume was not created during the OS installation, the contents of the /var directory are currently stored under the volume that holds the root (“/”) tree. The general idea is that because the /var directory is typically used to hold frequently changing and growing data (such as log files), it is prudent to put its content on its own separate file system. The steps involved with creating a logical volume can be summarized this way:

  1. Initialize a regular partition for use by the LVM system (or simply create a partition of the type Linux LVM (0x8e)).
  2. Create physical volumes from the hard disk partition.
  3. Assign the physical volume(s) to volume group(s).
  4. Finally, create logical volumes within the volume groups, and assign mount points to the logical volumes after formatting. The following illustration shows the relationship between disks, physical volumes (PVs), volume groups (VGs), and logical volumes (LVs) in LVM: PV-1 PV-3 PV-2 Volume Group 1 32GB SCSI disk 160GB IDE disk STEP A: Prepare the storage pool. This pool consists of three different hard disks. STEP B: Create a volume group by assigning disks from your storage pool (Step A) to a volume group. Assuming we use physical extents (PEs) that are 32MB each, then: SCSI disk = PV-1 = 32GB = 1000 PE IDE disk = PV-2 = 160GB = 5000 PE SATA disk = PV-3 = 64GB = 2000 PE STEP C: Create logical volumes (LVs) by using free space from the parent volume group from Step B.
  5. Create an LV called LV-1, format it, and mount its file system on the/home directory. The LV has been created with 3125 logical extents (or 100GB).
  6. Create another LV called LV-2, format it, and mount it at the/var directory; it has been created with 62 logical extents (or 2GB). Therefore, there are a total of 87000 PEs (or ∼224GB) available to the volume group. 64GB SATA disk /home /var Mount point for logical volume /dev/volume-group-1/LV-2 Mount point for logical volume /dev/volume-group-1/LV-1 CAUTION The process of creating partitions is irrevocably destructive to the data already on the disk. Before creating, changing, or removing partitions on any disk, you must be sure of what you are doing and its consequences. The following section will be broken down into several parts: ▼ Creating a partition ■ Creating a physical volume ■ Assigning a physical volume to a volume group ▲ Creating a logical volume The entire process from start to finish may appear a bit lengthy. It is actually a simple process in itself, but we intersperse the steps with some extra steps, along with some notes and explanations.

⁠+++++++++++++++++++++++++++

⁠LVM Command Description

⁠+++++++++++++++++++++++++++

⁠lvcreate

Used for creating a new logical volume in a volume group by allocating logical extents from the free physical extent pool of that volume group.

⁠lvdisplay

Displays the attributes of a logical volume, such as read/write status, size, and snapshot information.

⁠pvcreate

Initializes a physical volume for use with the LVM system.

⁠pvdisplay

Displays the attributes of physical volumes, such as size and PE size.

⁠vgcreate

Used for creating new volume groups from block devices created using the pvcreate command.

⁠vgextend

Used for adding one or more physical volumes to an existing volume group to extend its size.

⁠vgdisplay

Displays the attributes of volume groups.

⁠++++++++++++++++++++

⁠Creating a Partition

⁠++++++++++++++++++++

We will be using the free unpartitioned space on the main system disk, /dev/sda.

  1. Begin by running fdisk with the -l parameter to list the current partition table. Type [root@fedora-serverA ~]# fdisk -l /dev/sda Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x00005158 Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux /dev/sda2 26 1200 9438187+ 8e Linux LVM
  2. Next, we begin the actual repartitioning process using fdisk again. Type [root@fedora-serverA ~]# fdisk /dev/sda The number of cylinders for this disk is set to 1305. ...(OUTPUT TRUNCATED)...
  1. booting and partitioning software from other operating systems (e.g., DOS FDISK, OS/2 FDISK) Command (m for help): You will be presented with a simple fdisk prompt “Command (m for help):”.
  1. Print the partition table again while inside the fdisk program. Type p at the fdisk prompt to print the partition table. Command (m for help): p Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x00005158 Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux /dev/sda2 26 1200 9438187+ 8e Linux LVM A few facts worthy of note regarding this output: ▼ The total disk size is approximately 10.7GB. ■ There are currently two partitions defined on the sample system: /dev/sda1 and /dev/sda2. ■ The /dev/sda1 partition is of the type “Linux” (0x83), and the /dev/sda2 partition is of the type “Linux LVM” (0x8e). ■ From the partitioning scheme we chose during the OS installation, we can deduce that /dev/sda1 houses the /boot file system and /dev/sda2 houses everything else (see the output of the df command for reference). ■ The entire disk spans 1305 cylinders. ▲ The last partition, i.e., /dev/sda2, ends at the 1200-cylinder boundary. Therefore, there is room to create a partition that will occupy the space from cylinder 1201 to the last cylinder on the disk (i.e., 1305).
  2. Type n at the prompt to create a new partition. Command (m for help): n NOTE If you are curious about the other things you can do at the fdisk prompt, type m to display a help menu.
  3. Type p to select a primary partition type. Command action e extended P primary partition (1-4) P
  4. We want to create the third primary partition. Type 3 when prompted for a partition number: Partition number (1-4): 3
  5. The next step is to specify the partition size. First we choose the lower limit. Accept the default value for the first cylinder. Type 1201. First cylinder (1201-1305, default 1201): 1201
  6. Instead of designating a megabyte value for the size of this partition, we enter the last cylinder number, thus taking up the remainder of the disk. Accept the default suggested for the last cylinder. On our sample system, this value is 1305. Type 1305. Last cylinder or +size or +sizeM or +sizeK (1201-1305, default 1305): 1305
  7. By default, fdisk creates ext2-type partitions (i.e., 0x83). But we want to create a partition of type “Linux LVM.” Change the partition type from the default Linux (0x83) to the “Linux LVM” type. To do this, we use the t (change partition type) command. Type t. Command (m for help): t
  8. Enter the partition number whose type you want to change. We want to change the type for the /dev/hda3 partition that was just created, so type 3 when prompted for a partition number. Partition number (1-4): 3
  9. Enter the partition type for “Linux LVM”. Type 8e at the prompt: Hex code (type L to list codes): 8e NOTE You can list the hex codes for the available partition types by typing L.
  10. View the changes you’ve made by viewing the partition table. Type p. Command (m for help): p Disk /dev/sda: 10.7 GB, 10737418240 bytes 255 heads, 63 sectors/track, 1305 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x00005158 Device Boot Start End Blocks Id System /dev/sda1 * 1 25 200781 83 Linux /dev/sda2 26 1200 9438187+ 8e Linux LVM /dev/sda3 1201 1305 843412+ 8e Linux LVM
  11. Once you are satisfied with your changes, commit or write the changes you’ve made to the disk’s partition table using the w (write table to disk) command: Command (m for help): w
  12. Quit the fdisk utility. Type q. Command (m for help): q
  13. When you are back at the shell prompt, reboot the system to allow the Linux kernel to properly recognize the new partition table. Type [root@fedora-serverA ~]# reboot Creating a Physical Volume Next, create the physical volume itself.
  14. After the system comes back up from the reboot, log back in as the superuser.
  15. First let’s view the current physical volumes defined on the system. Type [root@fedora-serverA ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name VolGroup00 PV Size 9.00 GB / not usable 1003.00 KB ...(OUTPUT TRUNCATED)... Take note of the physical volume name field (PV Name).
  16. Use the pvcreate command to initialize the partition we created earlier as a physical volume. Type [root@fedora-serverA ~]# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created
  17. Use the pvdisplay command to view your changes again. Type [root@fedora-serverA ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name VolGroup00 ...(OUTPUT TRUNCATED)... --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 823.64 MB ...(OUTPUT TRUNCATED)... Assigning a Physical Volume to a Volume Group Here we will assign the physical volume created earlier to a volume group (VG).
  18. First use the vgdisplay command to view the current volume groups that might exist on your system. Type [root@fedora-serverA ~]# vgdisplay --- Volume group --- VG Name VolGroup00 System ID Format lvm2 ...(Output truncated)... VG Size 9.00 GB PE Size 32.00 MB Total PE 288 Alloc PE / Size 287 / 8.97 GB Free PE / Size 1 / 32.00 MB VG UUID T4153B-80Zu-KQPs-sWwt-X5sg-0G78-EAiEp0 Chapter 7: File Systems 187 From the preceding output, we can tell that ▼ The volume group name (VG Name) is VolGroup00. ■ The current size of the VG is 9GB (this should increase by the time we are done). ■ The physical extent size is 32MB, and there are a total of 288 PEs. ▲ There is only one physical extent that is free in the VG. It is equivalent to 32MB of space.
  19. Assign the PV to the volume group using the vgextend command. The syntax for the command is vgextend [options] VolumeGroupName PhysicalDevicePath Substituting the correct values in this command, type [root@fedora-serverA ~]# vgextend VolGroup00 /dev/sda3 Volume group "VolGroup00" successfully extended
  20. View your changes with the vgdisplay command. Type [root@fedora-serverA ~]# vgdisplay --- Volume group --- VG Name VolGroup00 ...(Output truncated)... Act PV 2 VG Size 9.78 GB PE Size 32.00 MB Total PE 313 Alloc PE / Size 287 / 8.97 GB Free PE / Size 26 / 832.00 MB Note that the VG Size, Total PE, and Free PE values have dramatically increased. We now have a total of 26 free PEs (or 832MB). Creating a Logical Volume (LV) Now that we have some room in the VG, we can go ahead and create the final logical volume (LV).
  21. First view the current LVs on the system. Type [root@fedora-serverA ~]# lvdisplay | less --- Logical volume --- LV Name /dev/VolGroup00/LogVol00 VG Name VolGroup00 ...(Output truncated)... --- Logical volume --- LV Name /dev/VolGroup00/LogVol02 VG Name VolGroup00 ...(Output truncated)... --- Logical volume --- LV Name /dev/VolGroup00/LogVol03 VG Name VolGroup00 ...(Output truncated)... --- Logical volume --- LV Name /dev/VolGroup00/LogVol01 VG Name VolGroup00 The preceding output shows the current LVs—/dev/VolGroup00/LogVol00, /dev/VolGroup00/LogVol02, /dev/VolGroup00/LogVol03, and so on.
  22. With the background information that we now have, we will create an LV using the same naming convention that is currently used on the system. We will create a fourth LV called “LogVol04.” The full path to the LV will be /dev/ VolGroup00/ LogVol04. Type [root@fedora-serverA ~]# lvcreate -l 26 --name LogVol04 VolGroup00 Logical volume "LogVol04" created NOTE You can actually name your LV any way you want. We named ours LogVol04 for consistency only. We could have replaced LogVol04 with another name, like “my-volume,” if we wanted to. The value for the --name (-n) options determines the name of the LV. The -l option specifies the size in physical extents units (see Step 1 under “Assigning a Physical Volume to a Volume Group”). We could have also specified the size in megabytes by using an option like -L 864M.
  23. View the LV you created. Type [root@fedora-serverA ~]# lvdisplay /dev/VolGroup00/LogVol04 --- Logical volume --- LV Name /dev/VolGroup00/LogVol04 VG Name VolGroup00 ...(Output truncated)... LV Size 832.00 MB Current LE 26 Fedora and RHEL distributions of Linux have a GUI tool that can greatly simplify the entire management of an LVM system. The command system-config-lvm will launch the tool, as shown here: OpenSuSE distribution also has a capable GUI tool for managing disks, partitions, and the LVM. Issue the command yast2 lvm_config to launch the utility, shown here:

Tag Summary

No tags have been pushed to this repository yet.