Linux Disk and File System Management

Posted by deejay on Wed, 08 May 2019 14:39:03 +0200

fdisk

Commonly used disk partitioning tools, limited by the mbr partition table, can only partition disks smaller than 2TB. If you use fdisk to partition disks larger than 2TB, although partitioning is possible, it can only recognize 2T of space. Generally, the parted partitioning tool is used

- l # Display all disk partition information

[root@puppet105 ~]# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors    # Size of disk/dev/sda
Units = sectors of 1 * 512 = 512 bytes                  # 512 bytes per cylinder
Sector size (logical/physical): 512 bytes / 512 bytes        # Bytes per sector
I/O size (minimum/optimal): 512 bytes / 512 bytes          #Bytes read and written at a time
Disk label type: dos
Disk identifier: 0x000a9f20

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 4196351 1048576 82 Linux swap / Solaris /dev/sda3 4196352 125829119 60816384 8e Linux LVM Disk /dev/mapper/centos_centos7-root: 62.3 GB, 62272831488 bytes, 121626624 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes

Device #Partition Name

Boot #boot partition, indicated by *boot partition

Start #indicates the starting cylinder

End #indicates the end of the cylinder

Blocks # Number of Blocks

#Partition Type ID

System #Partition Type

Disk Partitioning Steps

# Add a 20G disk
[root@centos7 ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

#  View pre-partition device status

[root@centos7 ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb

# Begin partitioning
[root@centos7 ~]# fdisk /dev/sdb        # Followed directly by the device file, or the -cu parameter
Welcome to fdisk (util
-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xfe05c2f7. Command (m for help): m             # Input m is the help menu Command action a toggle a bootable flag           # Set boot sector b edit bsd disklabel              # Edit bsd Volume Label c toggle the dos compatibility flag      # Set dos compatible sector d delete a partition                # Delete a partition g create a new empty GPT partition table    # Create a blank gpt partition table G create an IRIX (SGI) partition table      # Create IRIX partition table l list known partition types            # View partition type corresponding list number m print this menu                  # Print Help Menu n add a new partition                # Add a new partition o create a new empty DOS partition table     # Create a new DOS partition table p print the partition table            # Print partition table q quit without saving changes        # Exit without saving s create a new empty Sun disklabel      # Create a new empty sun volume label t change a partition's system id        # Change partition system ID u change display/entry units          # Change display units v verify the partition table          # Verify partition table w write table to disk and exit          # Write disk list and exit x extra functionality (experts only)      # Additional features Command (m for help): n                # Create a new partition Partition type: p primary (0 primary, 0 extended, 4 free)  # Primary partition, only four can be created, fifth must be an extended partition e extended                      # Extended partition Select (default p): p                  # Create primary partition identity Partition number (1-4, default 1): 1          #number(1-4) First sector (2048-41943039, default 2048):     # Start position, direct return, default 1 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G # Set primary partition size Partition 1 of type Linux and of size 5 GiB is set Command (m for help): n              # Create a new partition Partition type: p primary (1 primary, 0 extended, 3 free) e extended                    # Create an extended partition Select (default p): e Partition number (2-4, default 2): 2        # Number 2 First sector (10487808-41943039, default 10487808): Using default value 10487808 Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G  # Also 5G capacity Partition 2 of type Extended and of size 5 GiB is set Command (m for help): n              # Create a new partition Partition type: p primary (1 primary, 1 extended, 2 free)  # Now that the extended partition is created, only the primary and logical partitions are left l logical (numbered from 5) Select (default p): l                # Create Logical Partitions Adding logical partition 5              # Logical partition number must start at 5, default is 5 First sector (10489856-20973567, default 10489856): Using default value 10489856 Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +2G  # Capacity given to 2G Partition 5 of type Linux and of size 2 GiB is set Command (m for help): p                # Print the list of partitions created Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xfe05c2f7 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux /dev/sdb2 10487808 20973567 5242880 5 Extended /dev/sdb5 10489856 14684159 2097152 83 Linux Command (m for help): w                # Preservation The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@centos7 ~]# ls /dev/sdb*            # View device status /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb5 [root@centos7 ~]# partprobe /dev/sdb        # Notify that the kernel partition table has changed [root@centos7 ~]# mkfs.ext4 /dev/sdb1        #Format File System mke2fs 1.42.9 (28-Dec-2013)   Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@centos7 ~]# tune2fs -c 0 -i 0 /dev/sdb1    # This command can automatically check rice disk overload tune2fs 1.42.9 (28-Dec-2013) Setting maximal mount count to 1 [root@centos7 ~]# df -h            # Formatted disks need to be mounted for use. The created disks are not shown here Filesystem Size Used Avail Use% Mounted on /dev/sda3 98G 1.7G 97G 2% / devtmpfs 901M 0 901M 0% /dev tmpfs 911M 0 911M 0% /dev/shm tmpfs 911M 9.5M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 [root@centos7 ~]# mount /dev/sdb1 /mnt    # Mount Disk Partition [root@centos7 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 98G 1.7G 97G 2% / devtmpfs 901M 0 901M 0% /dev tmpfs 911M 0 911M 0% /dev/shm tmpfs 911M 9.5M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb1 4.8G 20M 4.6G 1% /mnt      # The disk is mounted    [root@centos7 ~]# vi /etc/fstab      # There are two ways to start up, the first one and the other echo "mount/dev/sdb1/mnt" >/etc/rc.local # # /etc/fstab # Created by anaconda on Fri Oct 26 21:32:51 2018 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=fc662da3-4f5b-480b-bae9-cc9c8698742a / xfs defaults 0 0 UUID=e1b67cc9-08ff-47e7-b1e9-3ce2d9ee5eb6 /boot xfs defaults 0 0 UUID=47b26d55-1602-48e1-adba-91fe4f420b03 swap swap defaults 0 0 /dev/sdb1 /mnt ext4 defaults 0 0

#Partitioning about LVM, blog posts will be written separately

fdisk non-interactive partitioning (mass creation)

[root@centos7 ~]# fdisk /dev/sdb<<EOF
> n
> p
> 3
> 
> +2G
> p
> w
> EOF
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): Partition number (3,4, default 3): First sector (20973568-41943039, default 20973568): Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): Partition 3 of type Linux and of size 2 GiB is set

Command (m for help): 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xfe05c2f7

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux
/dev/sdb2        10487808    20973567     5242880    5  Extended
/dev/sdb3        20973568    25167871     2097152   83  Linux
/dev/sdb5        10489856    14684159     2097152   83  Linux

Command (m for help): The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

 

partprobe

Update the hard disk partition table data in the Linux kernel when the hard disk partition changes.No system restart is required to take effect

tnue2fs

Adjust or view the parameters of ex2/ex3/3x4 file system, use less if you can adjust the boot-up self-check cycle of the file system

- c #Set the number of mounts to force self-check, the count will be increased by 1 without mounting once, the number of times will force self-check, set 0 or -1, this function is off

-i #Time interval for mandatory self-check

-l #File system information for investigation

 

parted

Partition commands for disks larger than 2TB require conversion to GPT format

- l #hours need to have disk partition information

* h #View help

[root@centos7 ~]# parted 
GNU Parted 3.1    # version number
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) h           # view help 
align
-check TYPE N  #Check the disk partition, type Divided into min/opt,N Represents the disk partition number
mklabel,mktable LABEL
-TYPE # Create partition table table)     mkpart PART-TYPE [FS-TYPE] START END # Create partitions name NUMBER NAME # Name the partition print [devices|free|list,all|NUMBER] # Show partition table information, abbreviated p quit # Sign out resizepart NUMBER END # Reset partition size rm NUMBER # Delete Number number Partitions select DEVICE #Select the device to edit set NUMBER FLAG STATE # Change partition flag toggle [NUMBER [FLAG]] # Set partition flag unit UNIT   # Set default units copyright information of GNU Parted (parted) quit

 

Section steps, using 20GB for this demonstration

[root@centos7 ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb
[root@centos7 ~]# parted /dev/sdb      # Begin partitioning, interactive
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt              # by sdb Disk Creation GPT Partition table, greater than 2 T This step must be performed. fdisk yes mdos Partition table

(parted) mkpart primary 0 5G    # create primary partition
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore             # Ignore                                            
(parted) p                            #Show partition table                                   
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  5000MB  5000MB               primary    #Once the primary partition is created, it will be automatically recognized by the system.

(parted) mkpart logical 5G 10G                  # Create Logical Partitions
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      17.4kB  5000MB  5000MB               primary
 2      5001MB  10.0GB  5000MB               logical

(parted) quit                                                             
Information: You may need to update /etc/fstab.

[root@centos7 ~]# ls /dev/sd*        # Devices Viewing Partitions
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sdb  /dev/sdb1  /dev/sdb2

# The following steps for formatting partitions, mounting, boot-up automounting are the same as the fdisk partition steps, because parted automatically lets the kernel recognize when creating partitions, so the partprobe command is not used


  # fdisk Non-interactive partitioning(Bulk Creation)

  parted /dev/sdb mklabel gpt yes

  parted /dev/sdb mkpart primary 0 5G

  parted /dev/sdb p

 

mkfs

Create a Linux file system so data can only be stored if the file system is created by formatting

-t #Specify the type of file system to create

- c #Check disk bad blocks when creating file systems

- v #Show Details

  

[root@centos7 ~]# mkfs    # This command is a front-end command that calls mkfs.fstype through the -t parameter so that it can be created directly using mkfs.ext4
mkfs         mkfs.cramfs  mkfs.ext3    mkfs.minix   
mkfs.btrfs   mkfs.ext2    mkfs.ext4    mkfs.xfs  

 

dumpe2fs

Export information about the inside of the ext2/ext3/ext4 file system, e.g., the composition of the file system bao contains information about superblocks, block groups, inode s, bock s, etc.

-b #Print block information reserved in the file system

-h #Enter Reality Super Block Information

- i #Read system information of the file from the specified file system image file

-x #Print information block members in hexadecimal format

resize2fs

Ext2,3,4 filesystems used to expand or shrink unmounted ext2,3,4 filesystems, 2.6 kernel versions or higher, and support online expansion of hung filesystems. Commonly used for partitioning after LVM expansion, fdisk and parted should be planned in advance

-p #Print the progress bar to complete the task

-r #Enforce operation

Topics: Linux Vmware Fragment Anaconda