Linux Study Notes (14) file system management and linux Study Notes
1. Review partition and System (1) Partition types
Linux partitions generally include primary partitions, extended partitions, and logical partitions. Where:
A maximum of four primary partitions can be created;
There can be only one extended partition, or one of the primary partitions, that is, the primary partition and the extended partition can have up to four. However, extended partitions cannot store data or format data. They must be divided into logical partitions before they can be used;
Logical partitions are divided by extended partitions. For an IDE hard disk, Linux supports a maximum of 59 logical partitions. For a SCSI hard disk, Linux supports a maximum of 11 logical partitions.
(2) File System
Common Linux file systems include ext2, ext3, and ext4.
Ext2 is an upgraded version of the ext file system. By default, the system before RedHat Linux7.2 is an ext2 file system. Released in 1993, it supports up to 16 TB of partitions and up to 2 TB of files.
Ext3 is an upgraded version of the ext2 file system. The biggest difference is that the log function is added to provide the reliability of the system file system when the system suddenly stops. Supports up to 16 TB of partitions and up to 2 TB of files.
Ext4 is an upgraded version of ext3. Ext4 has been significantly improved in terms of performance, scalability, and reliability. The changes in ext4 are backward compatible with ext3 and support up to 1 EB file system and 16 TB files, wireless number of sub-directories, Extents continuous data block concept, multi-block allocation, delayed allocation, and persistent pre-allocation, fast fsck, log verification, no log mode, online fragment, iNode enhancement, and barrier enabled by default.
2 common file system commands (1) df, du, fsck and dumpe2fs commands
1) df is a File System View command in the format:
Df [Option] [mount point]
The-a option displays information about all file systems, including special file systems such as/proc and/syfs, the "-h" option indicates that the used unit shows the capacity such as KB, MB, and GB. The "-T" option indicates the file type of the file system, and the "-m" option indicates the size displayed in MB, the-k option indicates the display capacity in KB. The default value is KB.
For example, view the local file system:
[root@localhost ~]# df -ThFilesystem Type Size Used Avail Use% Mounted on/dev/sda5 ext4 14G 2.0G 11G 16% /tmpfs tmpfs 504M 0 504M 0% /dev/shm/dev/sda1 ext4 291M 32M 244M 12% /boot/dev/sda2 ext4 4.0G 136M 3.7G 4% /home
2) The du command is used to measure the directory or file size. The format is:
Du [Option] [directory or file name]
The "-a" option indicates that the disk usage of each sub-file is displayed. By default, only the disk statistics of sub-directories are collected. The "-h" option indicates that the disk usage is displayed in a regular unit, the-s option indicates the total usage, rather than the usage of sub-directories and sub-files.
Note: The ls command only counts the space occupied by the first level sub-directories and files in the directory.
For example, use the ls command and the du command to calculate the size of the/etc directory:
[Root @ localhost ~] # Du-sh/etc/27 M/etc/[root @ localhost ~] # Ls-ld/etc/drwxr-xr-x. 103 root 12288 January 31 05:00/etc/
3) Difference between df and du commands
When using df commands, consider not only the space occupied by files, but also the space occupied by commands or programs. The most common is that the file has been deleted, but the program has not released space.
The du command is file-oriented and only calculates the space occupied by files or directories.
For example, use df and du to display the size of the root directory:
[Root @ localhost ~] # Df-h/Filesystem Size Used Avail Use % Mounted on/dev/sda5 14G 2.0G 11G 16%/[root @ localhost ~] # Du-sh/du: inaccessible "/proc/1597/task/1597/fd/4": No file or directory du: inaccessible "/proc/1597/task/1597/fdinfo/4": No file or directory du: inaccessible "/proc/1597/fd/4 ": no file or directory du: cannot access "/proc/1597/fdinfo/4": No file or directory 1.9 GB/
4) fsck is a file system repair command in the format:
Fsck [Option] partition Device File Name
Among them, the-a option indicates that different user prompts are displayed and the file system is automatically repaired. The-y option indicates that the file system is automatically repaired, which is consistent with the-a option. However, some file systems only support-y.
5) The dumpe2fs command is used to display the disk status in the following format:
Dumpe2fs partition Device File Name
For example, to view the disk status of/dev/sda1, use the-h option to display only the super block information:
[root@localhost ~]# dumpe2fs -h /dev/sda1dumpe2fs 1.41.12 (17-May-2010)Filesystem volume name: <none>Last mounted on: /bootFilesystem UUID: 0b5697a3-9081-4b75-bf4c-71b4891d5e86Filesystem magic number: 0xEF53Filesystem revision #: 1 (dynamic)Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isizeFilesystem flags: signed_directory_hashDefault mount options: user_xattr aclFilesystem state: clean…………
(2) mount command
1) query and automatic mounting
The mount command is used to query the mounted devices in the system, such as the mounted devices:
[root@localhost ~]# mount/dev/sda5 on / type ext4 (rw)proc on /proc type proc (rw)sysfs on /sys type sysfs (rw)devpts on /dev/pts type devpts (rw,gid=5,mode=620)tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")/dev/sda1 on /boot type ext4 (rw)/dev/sda2 on /home type ext4 (rw)none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
Add the-l option to display the volume label name. If the-a option is added, the mounting is automatically performed based on the configuration file/etc/fstab.
2) mount command format:
Mount [-t file system] [-L volume name] [-o Special option] device file name mount point
The-t option specifies the Mount type by adding the file system type, which can be ext3, ext4 (for Linux), iso9660 (CD), and fat (win16), vfat (win32) and other file systems;
-L indicates the volume label name, indicating that the partition of the specified volume label is mounted instead of the file name of the installation device;
The-o option is a special option. You can specify additional options for mounting. Common options include
Parameters |
Description |
Atime/noatime |
Update access time/not updated. Whether to update the file access time when accessing the partition file. The default value is update. |
Async/sync |
Asynchronous/Synchronous. The default value is asynchronous. |
Auto/noauto |
Automatic/manual: whether the/etc/fstab file is automatically mounted when the mount-a command is executed. The default value is automatic. |
Ults |
Defines the default value, which is equivalent to the seven options rw, suid, dwv, exec, auto, nouser, and async. |
Exec/noexec |
Execute/do not execute. Set whether to allow execution of executable files in the file system. The default value is exec. |
Remount |
Remount a mounted file system, which is generally used to specify the modification of a special township |
Rw/ro |
Read/write-only: whether the file system has read/write permissions when mounting. The default value is rw. |
Suid/nosuid |
Has/does not have SUID permission, and sets whether the file system has SUID and SGID permissions. By default |
User/nouser |
Allows/does not allow normal users to mount, and sets whether the file system allows normal users to mount partitions. By default, only root users can mount partitions. |
Usrquota |
Writing indicates that the file system supports the user's disk quota. Not supported by default |
Kgquota |
Writing indicates that the file system supports the disk quota, which is not supported by default. |
For example, re-mount the/home partition and run the shell script helloworld. sh with the noexec permission:
[Root @ localhost ~] # Mount-o remount, noexec/home [root @ localhost ~] # Cd/home/[root @ localhost home] # vi helloword. sh [root @ localhost home] # chmod 755 helloword. sh [root @ localhost home] #. /helloword. sh-bash :. /helloword. sh: insufficient permissions [root @ localhost home] # Total ll usage 20-rwxr-xr-x. 1 root 31 Aug 17 06:12 helloword. sh [root @ localhost home] # cp helloword. sh/root/[root @ localhost home] #/root/helloword. shhello world! [Root @ localhost home] # mount-o remount, exec/home
It can be seen that the shell script cannot be executed after the noexec permission is set under/home, And/root can be executed in Other Default partitions. Note that the modification should be made back. Otherwise, the system startup will be affected.
(3) attaching a CD and a USB flash disk
1) attach a cd
First, select the attached CD image in vmwarevm settings, and then make sure that the "device status" is "connected", as shown in:
Next, create a cdrom mount point in the/mnt directory, mount the disc, and display the content of the disc. The procedure is as follows:
[Root @ localhost ~] # Mkdir/mnt/cdrom/# create a mount point [root @ localhost ~] # Mount-t iso9660/dev/cdrom/mnt/cdrom/# mount the disk: block device/dev/sr0 is write-protected, mounting read-only [root @ localhost ~] # Ls/mnt/cdrom/CentOS_BuildTag Packages RPM-GPG-KEY-CentOS-Security-6EULA RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-Testing-6GPL RPM-GPG-KEY-CentOS-6isolinux repodata TRANS. TBLimages RPM-GPG-KEY-CentOS-Debug-6 [root @ localhost ~] # Umount/mnt/cdrom/# unmount mount point
Note: The mount point unmount command umount is in the format of umount device file name or mount point.
2) mount a USB flash drive
Place the cursor in the vmwarevm and insert the USB flash disk. In this way, the USB flash drive will be recognized by Linux instead of Windows.
First, run the fdisk command to view the Device File Name of the USB flash drive. The specific command is fdisk-l.
Mount the usb flash drive: mount-t vfat/dev/sdb1/mnt/usb
Note: The USB flash drive is a FAT16 file system. The format of the mounted file system in Linux is fat. If the format is FAT32, the format of the mounted file system in Linux is vfat. Linux does not support the NTFS file system by default.
(4) NTFS-Supported file systems
A third-party software ntfs-3g can be used to mount NTFS file systems in Linux. It is http://www.tuxera.com/community/ntfs-3g-download /.
The installation and compilation process is as follows:
① Decompression: tar-xzvf ntfs-3g-ntfsprogs-2014.2.15.tar.gz
② Into the compressed package: cd ntfs-3g-ntfsprogs-2014.2.15
③ The compiler is ready. You do not need to specify the installation directory. The installation will be installed to the default location:./confgure ④ compile: make
⑤ Compile and install: make install
After installation, insert the mobile hard disk (the same as the insert method of the USB flash disk) and mount it:
Mount-t ntfs-3g partition device file name mount point
3 fdisk partitioning (1) fdisk command partitioning process
① Use VMware to create a hard disk:
In "Virtual Machine Settings", select "Hard Disk", click "add", and then select default to add a new hard disk. Note: Add a hard disk when the system is not started.
② Open the VM and run the command fdisk-l to view the newly added Hard Disk:
[root@localhost ~]# fdisk -lDisk /dev/sdb: 10.7 GB, 10737418240 bytes255 heads, 63 sectors/track, 1305 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x00000000
③ Use the fdisk/dev/sdb command to partition the new hard disk. The fdisk interaction commands are described as follows:
Command |
Description |
A |
Set bootable flag |
B |
Edit bsd disk tags |
C |
Set the DOS operating system compatibility tag |
D |
Delete a partition |
L |
Display known file system types. 82 is a swap partition and 83 is a Linux partition. |
M |
Show Help Menu |
N |
Create Partition |
O |
Create a blank DOS partition table |
P |
Display partition list |
Q |
Do not save and exit |
S |
Create a blank SUN disk tag |
T |
Change the system ID of a partition |
U |
Change the display record Unit |
V |
Verify the Partition Table |
W |
Save and exit |
X |
Additional functions (expert only) |
Create a new/dev/sdb1 primary partition,/dev/sbd2 extended partition, And/dev/sdb5 logical partition.
# Create a primary partition and allocate 2 GB of hard disk space. Command (m for help): nCommand action e extended p primary partition (1-4) pPartition number (1-4 ): 1 First cylinder (1-1305, default 1): Using default value 1 Last cylinder, + cylinders or + size {K, M, G} (1-1305, default 1305 ): + 2G # create an extended partition and allocate the remaining hard disk space. Command (m for help): nCommand action e extended p primary partition (1-4) ePartition number (1-4): 2 First cylinder (263-1305, default 263): Usi Ng default value 263 Last cylinder, + cylinders or + size {K, M, G} (263-1305, default 1305): Using default value 1305 # create a logical partition, the disk space is the disk space of the extended partition. Command (m for help): nCommand action l logical (5 or over) p primary partition (1-4) lFirst cylinder (263-1305, default 263): Using default value 263 Last cylinder, + cylinders or + size {K, M, G} (263-1305, default 1305 ): using default value 1305 # Use the w Command to save and exit Command (m Help): wThe partition table has been altered! Calling ioctl () to re-read partition table. Syncing disks.
④ After the partition is created, run the partprobe command to read the partition table information again, and a normal alarm is displayed.
[Root @ localhost ~] # PartprobeWarning: WARNING: the kernel failed to re-read the partition table on/dev/sda (the device or resource is busy ). as a result, it may not reflect all of your changes until after reboot. warning: you cannot open/dev/sr0 in read/write mode (read-only file system ). /Dev/sr0 is enabled in read-only mode. Warning: you cannot open/dev/sr0 in read/write mode (read-only file system ). /Dev/sr0 is enabled in read-only mode. Error: the Partition Table-/dev/sr0 is invalid and recursive partitions appear.
⑤ Format the partition and write it into the file system. mkfs-t ext4/dev/sdb1 and/dev/sdb5.
[Root @ localhost ~] # Mkfs-t ext4/dev/sdb1mke2fs 1.41.12 (17-May-2010) File System tag = Operating System: Linux block size = 4096 (log = 2) part size = 4096 (log = 2) Stride = 0 blocks, Stripe width = 0 blocks131648 inodes, 526120 blocks26306 blocks (5.00%) reserved for the super user first data block = 0 Maximum filesystem blocks = 54106521617 block groups32768 blocks per group, 32768 fragments per group7744 inodes per groupSuperblock backups stored on blocks: 32768,983 04, 163840,229 376, 294912 Writing to inode table: Creating journal (16384 blocks): Writing superblocks and filesystem accounting information: This filesystem will be automatically checked every 32 mounts or180 days, whichever comes first. use tune2fs-c or-I to override.
⑥ Create a partition mount point and mount it. Create the disk1 and disk5 directories in the root partition as the mount points for/devsdb1 and/dev/sdb5 respectively.
[root@localhost ~]# mkdir /disk1 /disk5[root@localhost ~]# mount /dev/sdb1 /disk1[root@localhost ~]# mount /dev/sdb5 /disk5
7. Run the df or mount command to view the mounting information.
[root@localhost ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/sda5 14G 2.0G 11G 16% /tmpfs 504M 0 504M 0% /dev/shm/dev/sda1 291M 32M 244M 12% /boot/dev/sda2 4.0G 136M 3.7G 4% /home/dev/sdb1 2.0G 68M 1.9G 4% /disk1/dev/sdb5 7.9G 146M 7.4G 2% /disk5
(2) Automatic partition mounting
The preceding partitions are manually mounted and need to be remounted after the system is restarted. Therefore, you need to set automatic mounting. It mainly sets the/etc/fstab file
UUID=9288c490-0161-471c-b63a-6e56699065d5 / ext4 defaults 1 1UUID=0b5697a3-9081-4b75-bf4c-71b4891d5e86 /boot ext4 defaults 1 2UUID=fe5a8908-97e6-4670-a114-8bcc43af1bc2 /home ext4 defaults 1 2UUID=3ae64e9d-7321-478b-b35f-14df91638287 swap swap defaults 0 0tmpfs /dev/shm tmpfs defaults 0 0devpts /dev/pts devpts gid=5,mode=620 0 0sysfs /sys sysfs defaults 0 0proc /proc proc defaults 0 0
Fstab has six fields:
The first field is the partition device file name or UUID (General unique identifier code of the hard disk );
The second field is the mount point;
The third field is the file system name;
The fourth field is the mount parameter, which has the same meaning as the mount-o option;
The fifth field indicates whether the specified partition is backed up by dump. 0 indicates no backup, 1 indicates daily backup, and 2 indicates irregular backup. Note: Each partition directory has a backup directory lost + found;
The sixth field indicates whether the specified partition is detected by fsck. 0 indicates that the partition is not detected, and other digits indicate the detection priority.
Therefore, write the new partition/dev/sdb1 and/dev/sdb5 to fstab for automatic mounting:
/dev/sdb1 /disk1 ext4 defaults 1 2/dev/sdb5 /disk5 ext4 defaults 1 2
(3) fstab file repair
If the content entered in the/etc/fstab file is incorrect, the system reports an error upon restart and needs to be repaired.
For example, when the/dev/sdb5 partition is automatically mounted, enter/dev/sdb by mistake, that is
/Dev/sdb/disk5 ext4 defaults 1 2
When the system restarts, the following error occurs:
In this case, enter the password to re-Modify the/etc/fstab file. Because the file is a read-only file, you need to re-mount the root partition to read/write permissions, that is:
Mount-o remount, rw/
Then modify the entered fstab file and restart it.