08 - Storage in Linux

Storage Basics

Block Devices

  • A block device is a type of file found under /dev/ directory.

  • It represents a piece of hardware that can store data.

  • It is called as block storage, because data is read or written to it in blocks or chunks of space.

  • To list information about Block Devices use below command:

       $ lsblk
    
      NAME            MAJ:MIN    RM    SIZE    RO    TYPE    MOUNTPOINT
      sda               8:0       0    119.2G   0    disk                         # physical disk
      |--- sda1         8:1       0    100M     0    part    /boot/efi            # partition1
      |--- sda2         8:2       0    72.5G    0    part    /media/MM/Data       # partition2
      |--- sda3         8:3       0    46.6G    0    part                         # partition3
    
  • There is also alternative to this command:

      $ ls -l /dev/ | grep "^b"        # b represnts block device (first character of a output)
    
      brw-rw----    1    root    disk    8,    0 Mar 19 17:43 sda
      brw-rw----    1    root    disk    8,    1 Mar 19 17:43 sda1
      brw-rw----    1    root    disk    8,    2 Mar 19 17:43 sda2
      brw-rw----    1    root    disk    8,    3 Mar 19 17:43 sda3
    
  • Each block devices has a major and minor number:

    • Major number is used to identify the type of block device.

    • Minor Number is use to distinguish individual, physical or logical devices.

Major numberDevice Type
1RAM
3HARD DISK or CD ROM
6PARALLEL PRINTERS
8SCSI DISK

Disk Partitions

  • The entire disk can be broken down into smaller segments of usable space called partitions.

  • The information about partitions is saved in a partition table.

  • fdisk -l: To print, create and delete the partitions in partition table.

      [~]$ sudo fdisk -l /dev/sda
    

Partition Types

  • There are 3 types of disk partitions:

    1. PRIMARY: Use to boot an operating system and there can be 4 primary partitions.

    2. EXTENDED: Can host logical partitions but cannot be used on its own.

    3. LOGICAL: Created within an extended partition.

Partition Scheme - MBR

  • How a disk is partitioned is defined by a partitioning scheme also know as partition table.

  • MBR (Master Boot Record):

    • MBR is a type of partitioning scheme.

    • There can be 4 primary partitions in MBR.

    • The maximum size per disk is 2TB.

    • If we want more than 4 partitions per disk, we would need to create the fourth partition as an extended partition and carve out logical partitions within it.

  • GPT (GUID Partition Table):

    • GPT is another type of partitioning scheme that is created to address the limitations in MBR.

    • Theoretically, GPT can have an unlimited number of partitions per disk and usually only limited by the restrictions imposed by the OS itself.

      • Ex: RHEL only allows 128 partitions per disk.
    • The disk size limitation of 2TB does not exist with GPT.

Creating Partitions

  • gdisk <device-path>: To create partitions on the disk. It is an improved version of the fdisk that works with the GTP partition table.

    • Running gdisk /dev/sdb will take you into a menu-driven interface.

    • Once in, use the question mark (?) character to print all available options.

    • Next, type n key to create a new partition.

    • Let’s say we want to create a partition number 1 with the size of 20GB.

    • Next, it will ask for a hexcode for the partition type:

      • tick to the default of ‘Linux filesystem’ having code 8300.

      • Can type L key to see all possible value.

    • After providing all the necessary information, use W command to write the partition table.

    • This will create a new partition called /dev/sdb1.

    $ gdisk /dev/sdb                                        # command

    GPT fdisk (gdisk) version 1.0.1                         # output

    Partition table scan:
      MBR: protective
      BSD: not present
      APM: not present
      GPT: present
    Found valid GPT with protective MBR; using GPT.

    Command (? for help): ?
    b back up GPT data to a file
    c change a partition's name
    d delete a partition
    i show detailed information on a partition
    l list known partition types
    n add a new partition
    o create a new empty GUID partition table (GPT)
    p print the partition table
    q quit without saving changes
    r recovery and transformation options (experts only)
    s sort partitions
    t change a partition's type code
    v verify disk
    w write table to disk and exit
    x extra functionality (experts only)
    ? print this menu

    Command (? for help): n
    Partition number (1-128, default 1): 1
    First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: 2048
    Information: Moved requested sector from 34 to 2048 in
    order to align on 2048-sector boundaries.
    Use 'l' on the experts' menu to adjust alignment
    Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: 41943006
    Current type is 'Linux filesystem'
    Hex code or GUID (L to show codes, Enter = 8300):
    Changed type of partition to 'Linux filesystem'
    Command (? for help): w
    Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
    PARTITIONS!!
    Do you want to proceed? (Y/N): Y
    OK; writing new GUID partition table (GPT) to /dev/vdb.
    The operation has completed successfully.
  • lsblk or fdisk -l: To check the status of new partition.

      $ sudo fdisk -l /dev/sdb                                        # command
    
      Disk /dev/sdb: 20 GiB, 128035676160 bytes, 250069680 sectors    # output
      Units: sectors of 1 * 512 = 512 bytes
      Sector size (logical/physical): 512 bytes / 512 bytes
      I/O size (minimum/optimal): 512 bytes / 512 bytes
      Disklabel type: gpt
      Disk identifier: 7CABF26E-9723-4406-ZEA1-C2B9B6270A23
      Device Start End Sectors Size Type
      /dev/sdb1 2048 41943006 204800 20GB Linux filesystem
    

File Systems in Linux

  • Partitioning alone does not make a disk usable in the OS.

  • The disk and the partitions are seen as a raw disk by the Linux kernel.

  • To write to a disk or partition, we must first create a filesystem.

  • The filesystem defines how data is stored on a disk.

  • After creating a filesystem, we must mount it to a directory, and that’s when we can read or write data to it.

  • Few most commonly used filesystems are the extended filesystems series from EXT2 to EXT4.

Difference between EXT2, EXT3 and EXT4

  • Both EXT2 and EXT3 allow a maximum file size of 2TB and a maximum volume size of 4TB.

  • The difference between the two is that in EXT2, in case of an unclean shutdown such as one caused by a power outage, it can take quite some time for the system to boot back up.

  • EXT3 file system, however, did not have this drawback. It implemented additional features that allowed quicker startup after an ungraceful shutdown.

  • EXT4 further improves EXT3 filesystem and is one of the most common general purpose filesystem used today. It can support 16TB of maximum file size and up to 1 Exabyte of volume size and also backward compatible.

  • An EXT4 filesystem can be mounted as an EXT3 or EXT2 filesystem and similarly, EXT3 can be mounted as EXT2.

Working with EXT4

  • mkfs.ext4 <disk>: To create a file system we will make use of /dev/sdb disk, run below command

      $ mkfs.ext4 /dev/sdb1        # create filesystem
    
  • Now create a directory to mount the filesystem use below commands:

      $ mkdir /mnt/ext4;             # create directory
    
      $ mount /dev/sdb1 /mnt/ext4    # mount the created directory into the filesystem
    
  • To verify if the filesystem is mounted:

      $ mount | grep /dev/sdb1
      OR
      $ df -hP | grep /dev/sdb1
    

FSTAB

  • Add an entry into /etc/fstab for the filesystem to be available after reboot.

      # /etc/fstab: static file system information.
      #
      # Use 'blkid' to print the universally unique identifier for a
      # device; this may be used with UUID= as a more robust way to name devices
      # that works even if disks are added and removed. See fstab(5).
      # 
      # <file system>  <mount point>  <type>  <options>                          <dump>  <pass>
      /dev/sda1        /              ext4    defaults,relatime,errors=panic 0    1       ~
    
      echo "/dev/sdb1 /mnt/ext4 ext4 rw 0 0" >> /etc/fstab
    
    • fstab file attributes:

      fstab


External Storage: DAS, NAS and SAN

  • Commonly used external storage are (stores data in the form of block):

    1. DAS: Direct Attached Storage, external storage is attached directly to the host system that requires the space.

    2. NAS: Network Attached Storage quite similar to NFS server.

    3. SAN: Storage Area Network, this technology uses a fiber channel for providing high-speed storage.


NFS

  • NFS stands for Network File System.

  • NFS does not store data in blocks. Instead, it saves data in form of files. It works on service-client model.

  • NFS works on a server-client model.

  • Example:

    • Let’s say we have a directory /software/repos exists on the NFS server.

    • This directory is shared over the network using an NFS to the clients (employees laptops).

    • The data we see on our local system (employee laptop) may not physically reside on any of the attached disks. However, once mounted, it can be used as any other filesystem in the OS.

    • The term for directory sharing in NFS is called exporting.

  • NFS server maintains an export configuration file at /etc/exports that defines the clients which should be able to access the directories on the server. /etc/exports looks like this:

      $ /etc/exports
      /software/repos 10.61.35.201 10.61.35.202 10.61.35.203
    

  • exportfs -a: To exports all the mounts defined in /etc/exports:

      $ exportfs -a
    
  • exportfs -o: To manually export a directory

      $ exportfs -o 10.61.35.201:/software/repos
    
  • Once exported, we should be able to mount it on a local directory such as /mnt/software/repos using the mount command on the client side (employee laptop).

  • The network share should now be mounted on the client.


LVM

  • LVM stands for Logical Volume Manager.

  • LVM allows grouping of multiple physical volumes, which are hard disks or partitions into a volume group.

  • Volume groups can be carve out logical volumes.

  • LVM allows the logical volume to be resized dynamically as long as there is sufficient space in the volume group.

Working with LVM

  • To make use of LVM, install the package LVM .

      $ apt-get install lvm2
    
  • Use pvcreate command to create a Physical Volume.

      $ pvcreate /dev/sdb                                    # command
    
      Physical volume "/dev/sdb" successfully created        # output
    
  • Use vgcreate command to create a Volume Group.

      $ vgcreate caleston_vg /dev/sdb                        # command
    
      Volume group "caleston_vg" successfully created        # output
    
  • Use pvdisplay command to list all the PVs their names, size and the Volume group it is part of.

      $ pvdisplay                                        # command
    
      --- Physical volume ---                            # output
        PV Name /dev/sdb
        VG Name caleston_vg
        PV Size 20.00 GiB / not usable 3.00 MiB
        Allocatable yes
        PE Size 4.00 MiB
        Total PE 5119
        Free PE 5119
        Allocated PE 0
        PV UUID iDCXIN-En2h-5ilJ-Yjqv-GcsR-gDfV-zaf66E
    
  • Use vgdisplay to see more details of the VG.

      $ vgdisplay                             # command
    
      --- Volume group ---                    # output
        VG Name caleston_vg
        System ID
        Format lvm2
        Metadata Areas 1
        Metadata Sequence No 1
        VG Access read/write
        VG Status resizable
        MAX LV 0
        Cur LV 0
        Open LV 0
        Max PV 0
        Cur PV 1
        Act PV 1
        VG Size 20.00 GiB
        PE Size 4.00 MiB
        Total PE 5119
        Alloc PE / Size 0 / 0
        Free PE / Size 5119 / 20.00 GiB
        VG UUID VzmIAn-9cEl5bA-lVtm-wHKX-KQaObR
    
  • To create the Logical Volumes, you can use lvcreate command

      $ lvcreate –L 1G –n vol1 caleston_vg    # command
    
      Logical volume "vol1" created.          # output
    
  • To display the Logical Volumes, you can use lvdisplay command

      $ lvdisplay                                            # command
    
      --- Logical volume ---                                 # output
        LV Path /dev/caleston_vg/vol1
        LV Name vol1
        VG Name caleston_vg
        LV UUID LueYC3-VWpE31-UaYk-wjIR-FjAOyL
        LV Write Access read/write
        LV Creation host, time master, 2020-03-31 06:26:14
        LV Status available
        # open 0
        LV Size 1.00 GiB
        Current LE 256
        Segments 1
        Allocation inherit
        Read ahead sectors auto
        - currently set to 256
        Block device 252:0
    
  • To list the volume, you can use lvs command

      $ lvs                                    # command
    
       LV VG Attr LSize Pool                   # output
       vol1 caleston_vg -wi-a----- 1.00g
    
  • Now to create an filesystem you can use mkfs command

      $ mkfs.ext4 /dev/caleston_vg/vol1
    
  • To mount the filesystem use mount command

      $ mount –t ext4 /dev/caleston_vg/vol1 /mnt/vol1
    
  • Now logical volume is now available for use. Lets resize the filesystem on vol1 while it is mounted. Check the free space available.

      $ vgs                                            # command
    
      VG #PV #LV #SN Attr VSize VFree                  # output
      caleston_vg 1 1 0 wz--n- 20.00g 19.00g
    
      $ lvresize -L +1G -n /dev/caleston_vg/vol1        # command
    
      Logical volume vol1 successfully resized.         # output
    
      $ df –hP /mnt/vol1                                            # command
    
      Filesystem Size Used Avail Use% Mounted on                    # output
      /dev/mapper/caleston_vg-vol1 976M 1.3M 908M 1% /mnt/vol1
    
  • Now to resize the file system use resize2fs command.

      $ resize2fs /dev/caleston_vg/vol1                                # command
    
      resize2fs 1.42.13 (17-May-2015)                                  # output
      Filesystem at /dev/mapper/caleston_vg-vol1 is mounted on
      /mnt/vol1; on-line resizing required
      old_desc_blocks = 1, new_desc_blocks = 1
      The filesystem on //dev/mapper/caleston_vg-vol1 is now 524288
      (4k) blocks long.
    
  • Now run df -hp command to verify the size of the mounted filesystem

      $ df –hP /mnt/vol1                                                # command
    
      Filesystem Size Used Avail Use% Mounted on                        # output
      /dev/mapper/caleston_vg-vol1 2.0G 1.6M 1.9G 1% /mnt/vol1