【linux 学习】5.文件系统管理

文件系统管理

目录

回顾分区与文件系统

  1. 分区类型

    【linux 学习】5.文件系统管理

    • 划分eg1:

      【linux 学习】5.文件系统管理

    • 划分eg2:

      【linux 学习】5.文件系统管理

      即便主分区+扩展分区<4,逻辑分区也只能从sd?5开始


设备文件名命名规则:

设备 设备文件名
IDE硬盘 /dev/hd[a-d]
SCSI/SATA/USB硬盘 /dev/sd[a-p]
u盘 /dev/sd[a-p]
软驱 /dev/fd[0-1]
打印机 25针:/dev/lp[0-2]
USB:/dev/usb/ lp[0-15]
鼠标 USB:/dev/usb/mouse[0-15]
PS2: /dev/psaux
CD-ROM/DVD /dev/cdrom
当前鼠标 /dev/mouse
磁带机 IDE:/dev/ht0
SCSI:/dev/st0

硬盘设备命名规则:

设备文件 对应设备
sda a 表示第一个出现的硬盘,代表一个硬盘
sda1 第一个硬盘中的第一个分区
sda2 第一个硬盘中的第二个分区
sda3 第一个硬盘中的第三分区
设备文件 对应设备
sdb b 表示第二个出现的硬盘,代表一个硬盘
sdb1 第二个硬盘中的第一个分区
sdb2 第二个硬盘中的第二个分区

  1. 文件系统 ext->ext4版本发展
ext
ext2 1. ext2升级版,1993年发布
2. red hat linux7.2以前默认文件系统ext2
3. 最大支持16TB分区,2TB文件
ext3 1. 与ext2最大区别:带日志功能,系统突然停止时可提高文件系统可靠性
2. 最大支持16TB分区,2TB文件
ext4 1. 在性能,伸缩性,可靠性方面有重大改进
2. 向下兼容ext3,无限数量子目录,extents连续数据块,多块/延迟/持久预分配,快速fsck,日志校验,无日志模式,在线碎片整理,inode增强,默认启用barrier...
3. 最大1EB文件系统分区,16TB文件(1EB=1024PB=1024*1024TB)
4.默认文件系统entos6.3

文件系统常用命令 -df,du,fsck,dumpe2fs

  1. 文件系统查看 df

    df [选项]	[挂载点]
    	-a	显示所有文件系统信息,包括特殊文件系统(eg./proc, /sysfs等)
    	-h	人性化显示,KB,MB,GB
    	-T	显示文件系统类型
    	-m	以MB为单位显示容量
    	-k	  KB(默认)
    
    [jen@localhost ~]$ df
    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/sda2       14114696 4098780   9275872  31% /
    devtmpfs          482308       0    482308   0% /dev
    tmpfs             497948       0    497948   0% /dev/shm
    tmpfs             497948   14972    482976   4% /run
    tmpfs             497948       0    497948   0% /sys/fs/cgroup
    /dev/sda3        3997376   84000   3687280   3% /home
    /dev/sda1         194235  134718     45181  75% /boot
    tmpfs              99592      28     99564   1% /run/user/1000
    /dev/sr0         4480476 4480476         0 100% /run/media/jen/CentOS 7 x86_64
    [jen@localhost ~]$ df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda2        14G  4.0G  8.9G  31% /
    devtmpfs        472M     0  472M   0% /dev
    tmpfs           487M     0  487M   0% /dev/shm
    tmpfs           487M   15M  472M   4% /run
    tmpfs           487M     0  487M   0% /sys/fs/cgroup
    /dev/sda3       3.9G   83M  3.6G   3% /home
    /dev/sda1       190M  132M   45M  75% /boot
    tmpfs            98M   28K   98M   1% /run/user/1000
    /dev/sr0        4.3G  4.3G     0 100% /run/media/jen/CentOS 7 x86_64
    [jen@localhost ~]$ df -a
    Filesystem     1K-blocks    Used Available Use% Mounted on
    rootfs                 -       -         -    - /
    sysfs                  0       0         0    - /sys
    proc                   0       0         0    - /proc
    devtmpfs          482308       0    482308   0% /dev
    securityfs             0       0         0    - /sys/kernel/security
    tmpfs             497948       0    497948   0% /dev/shm
    devpts                 0       0         0    - /dev/pts
    tmpfs             497948   14972    482976   4% /run
    tmpfs             497948       0    497948   0% /sys/fs/cgroup
    cgroup                 0       0         0    - /sys/fs/cgroup/systemd
    pstore                 0       0         0    - /sys/fs/pstore
    ...
    /dev/sda2       14114696 4098780   9275872  31% /
    selinuxfs              0       0         0    - /sys/fs/selinux
    mqueue                 0       0         0    - /dev/mqueue
    hugetlbfs              0       0         0    - /dev/hugepages
    systemd-1              0       0         0    - /proc/sys/fs/binfmt_misc
    debugfs                0       0         0    - /sys/kernel/debug
    /dev/sda3        3997376   84000   3687280   3% /home
    /dev/sda1         194235  134718     45181  75% /boot
    sunrpc                 0       0         0    - /var/lib/nfs/rpc_pipefs
    tmpfs              99592      28     99564   1% /run/user/1000
    gvfsd-fuse             0       0         0    - /run/user/1000/gvfs
    fusectl                0       0         0    - /sys/fs/fuse/connections
    /dev/sr0         4480476 4480476         0 100% /run/media/jen/CentOS 7 x86_64
    
  2. 统计目录或文件大小du

    ll -h 目录命令统计一级文件名+子文件名占用空间大小,不统计数据,与du有所区别

    du [选项]	[目录/文件名]
    		-a	显示每个子文件磁盘占用量,默认只统计子目录
    		-h	人性化显示占用量
    		-s	统计总占用量,不列出子文件/目录占用量
    
    [jen@localhost ~]$ du -ha
    4.0K    ./Pictures
    ...
    4.0K    ./.bash_profile
    4.0K    ./.config/pulse/cookie
    4.0K    ./.config/pulse/9f58fea569b74406b723fae5281cf442-default-sink
    12K     ./.config/pulse/9f58fea569b74406b723fae5281cf442-stream-volumes.tdb
    68K     ./.config/pulse
    4.0K    ./.config/user-dirs.locale
    .......
    100K    ./.cache/ibus
    0       ./.cache/gdm/session.log.old
    0       ./.cache/gdm/session.log
    4.0K    ./.cache/gdm
    ...
    352K    ./.cache/tracker/ontologies.gvdb
    1.6M    ./.cache/tracker/meta.db
    ........
    44K     ./.local/share/flatpak/repo
    52K     ./.local/share/flatpak
    4.0K    ./.local/share/sounds
    ...
    192K    ./.local/share/tracker
    1.6M    ./.local/share
    1.6M    ./.local
    4.0K    ./Documents
    66M     .
    

    du -sh在统计前需扫描目录,属于高负载命令(大规模数据读写/扫描)

    [root@localhost jen]# du -sh /
    du: cannot access ‘/proc/19431/task/19431/fd/3’: No such file or directory
    du: cannot access ‘/proc/19431/task/19431/fdinfo/3’: No such file or directory
    du: cannot access ‘/proc/19431/fd/4’: No such file or directory
    du: cannot access ‘/proc/19431/fdinfo/4’: No such file or directory
    du: cannot access ‘/run/user/1000/gvfs’: Permission denied
    8.4G    /
    [root@localhost jen]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda2        14G  4.0G  8.9G  31% /
    devtmpfs        472M     0  472M   0% /dev
    tmpfs           487M     0  487M   0% /dev/shm
    tmpfs           487M   15M  472M   4% /run
    tmpfs           487M     0  487M   0% /sys/fs/cgroup
    /dev/sda3       3.9G   83M  3.6G   3% /home
    /dev/sda1       190M  132M   45M  75% /boot
    tmpfs            98M   28K   98M   1% /run/user/1000
    /dev/sr0        4.3G  4.3G     0 100% /run/media/jen/CentOS 7 x86_64
    [root@localhost jen]# 
    

    Q:df与du检测出的/占用空间大小不同?

    【linux 学习】5.文件系统管理S:

    S:du与df区别:

    • df:面向文件系统,考虑文件占用的空间+系统/进程/命令/程序占用空间(eg.文件删除但程序没有释放空间),多用于统计剩余空间
    • du:面向文件,只计算文件/目录占用空间,多用于统计文件占用大小
  3. 文件系统修复命令fsck

    fsck [选项]	分区设备文件名
    		-a:不同显示用户提示,自动修复文件系统
    		-y:自动修复。与-a作用一致,只不过某些文件系统支支持-y
    

    重启自动进行,用户一般不设置

  4. 检测磁盘状态命令dumpe2fs

    dumpe2fs	分区设备文件名
    [root@localhost jen]# dumpe2fs /dev/sda1
    dumpe2fs 1.42.9 (28-Dec-2013)
    Filesystem volume name:   <none>	卷标
    Last mounted on:          /boot		挂载点
    Filesystem UUID:          ca096b0c-2338-499f-8c65-e47083b3a329	UUID
    Filesystem magic number:  0xEF53
    Filesystem revision #:    1 (dynamic)
    Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
    Filesystem flags:         signed_directory_hash 
    Default mount options:    user_xattr acl
    Filesystem state:         clean
    Errors behavior:          Continue
    Filesystem OS type:       Linux
    Inode count:              51200
    Block count:              204800
    Reserved block count:     10240
    Free blocks:              59517
    Free inodes:              50852
    First block:              1
    Block size:               1024		分区大小k
    Fragment size:            1024
    Group descriptor size:    64
    Reserved GDT blocks:      256
    Blocks per group:         8192
    Fragments per group:      8192
    Inodes per group:         2048
    Inode blocks per group:   256
    Flex block group size:    16
    Filesystem created:       Sat Sep 26 05:29:43 2020
    Last mount time:          Wed Sep 30 16:11:27 2020
    Last write time:          Wed Sep 30 16:11:27 2020
    Mount count:              5
    Maximum mount count:      -1
    Last checked:             Sat Sep 26 05:29:43 2020
    Check interval:           0 (<none>)
    Lifetime writes:          227 MB
    Reserved blocks uid:      0 (user root)
    Reserved blocks gid:      0 (group root)
    First inode:              11
    Inode size:               128
    Journal inode:            8
    Default directory hash:   half_md4
    Directory Hash Seed:      99fed84e-c38b-4e25-b90c-0fe643934898
    Journal backup:           inode blocks
    Journal features:         journal_64bit
    Journal size:             4096k
    Journal length:           4096
    Journal sequence:         0x00000152
    Journal start:            1
    
    数据块组
    Group 0: (Blocks 1-8192) [ITABLE_ZEROED]
      Checksum 0x59ac, unused inodes 1700
      Primary superblock at 1, Group descriptors at 2-3
      Reserved GDT blocks at 4-259
      Block bitmap at 260 (+259), Inode bitmap at 276 (+275)
      Inode table at 292-547 (+291)
      3770 free blocks, 1701 free inodes, 11 directories, 1700 unused inodes
      Free blocks: 4423-8192
      Free inodes: 348-2048
    Group 1: (Blocks 8193-16384) [ITABLE_ZEROED]
      Checksum 0x295e, unused inodes 2047
      Backup superblock at 8193, Group descriptors at 8194-8195
      Reserved GDT blocks at 8196-8451
      Block bitmap at 261 (bg #0 + 260), Inode bitmap at 277 (bg #0 + 276)
      Inode table at 548-803 (bg #0 + 547)
      340 free blocks, 2047 free inodes, 1 directories, 2047 unused inodes
      Free blocks: 12285-12288, 15537-15872
      Free inodes: 2050-4096
    ...
    Group 4: (Blocks 32769-40960) [INODE_UNINIT, ITABLE_ZEROED]
      Checksum 0x6947, unused inodes 2048
      Block bitmap at 264 (bg #0 + 263), Inode bitmap at 280 (bg #0 + 279)
      Inode table at 1316-1571 (bg #0 + 1315)
      4667 free blocks, 2048 free inodes, 0 directories, 2048 unused inodes
      Free blocks: 32769-36480, 36508-36512, 36537-36544, 36562-36576, 36596-36608, 36659-36672, 36731-36736, 36825-36832, 36852-36864, 36899-36928, 36959-36960, 36991-36992, 37032-37056, 37082-37088, 37106-37120, 37234-37248, 37266-37280, 37306-37312, 37335-37344, 37372-37376, 37416-37440, 37478-37504, 37602-37632, 37777-37792, 37823-37824, 37865-37888, 37995-38016, 38120-38144, 38289-38304, 38328-38336, 38381-38400, 38536-38560, 38579-38592, 38644-38656, 38765-38784, 38888-38912, 39070-39072, 39104, 39139-39168, 39283-39296, 39414-39424, 39590-39616, 39835-39840, 39859-39872, 39909-39936, 40002-40064, 40172-40192, 40326-40448, 40573-40576, 40680-40704, 40800-40832, 40938-40960
      Free inodes: 8193-10240
    Group 5: (Blocks 40961-49152) [INODE_UNINIT, ITABLE_ZEROED]
      Checksum 0x2788, unused inodes 2048
      Backup superblock at 40961, Group descriptors at 40962-40963
      Reserved GDT blocks at 40964-41219
      Block bitmap at 265 (bg #0 + 264), Inode bitmap at 281 (bg #0 + 280)
      Inode table at 1572-1827 (bg #0 + 1571)
      2998 free blocks, 2048 free inodes, 0 directories, 2048 unused inodes
      Free blocks: 41220-41248, 41275-41344, 41449-41472, 41625-41728, 41833-43008, 45510-47104
      Free inodes: 10241-12288
    ...
    

挂载命令,光盘/u盘mount

  1. 查询与自动挂载

    mount [-l]		查询系统中已挂载设备,-l显示卷标名
    			-a			依据配置文件/etc/fstab内容自动挂载
    
    [root@localhost jen]# mount
    sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)#一些进程/程序挂载位置
    proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
    devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=482308k,nr_inodes=120577,mode=755)
    securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
    tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel)
    devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
    tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,mode=755)
    ...
    cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,seclabel,perf_event)
    configfs on /sys/kernel/config type configfs (rw,relatime)
    /dev/sda2 on / type ext4 (rw,relatime,seclabel,data=ordered)		#硬盘挂载点	/
    selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
    mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
    hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
    systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=35,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=20605)
    debugfs on /sys/kernel/debug type debugfs (rw,relatime)
    /dev/sda3 on /home type ext4 (rw,relatime,seclabel,data=ordered)
    /dev/sda1 on /boot type ext4 (rw,relatime,seclabel,data=ordered)
    sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
    tmpfs on /run/user/1000 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=99592k,mode=700,uid=1000,gid=1000)
    gvfsd-fuse on /run/user/1000/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=1000,group_id=1000)
    fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
    /dev/sr0 on /run/media/jen/CentOS 7 x86_64 type iso9660 	(ro,nosuid,nodev,relatime,uid=1000,gid=1000,iocharset=utf8,mode=0400,dmode=0500,uhelper=udisks2)			#光盘挂载
    binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)
    
  2. 挂载命令

    mount [-t 文件系统] [-L 卷标名]	\ [-o 特殊选项]  设备文件名 挂载点
    			-t 文件系统		加入文件系统类型指定挂载类型,eg.ext3,ext4,ios9660
    			-L 卷标名		挂载指定卷标的分区,而非安装设备文件名挂载
    			-o 特殊选项		指定挂载的额外选项,多个选项,分割
    			eg: -o remount,rw 挂载点
    					-o exec 挂载点
    			
    
    • []中内容非必须

    • 特殊选项:

      【linux 学习】5.文件系统管理
    [root@localhost Templates]# vi hello.sh
    [root@localhost Templates]# ls
    hello.sh
    [root@localhost Templates]# chmod 755 hello.sh 
    [root@localhost Templates]# ./hello.sh 
    Mr li is the hot man!
    [root@localhost Templates]# mount -o remount,noexec /home
    [root@localhost Templates]# 
    [root@localhost Templates]# ls
    hello.sh
    [root@localhost Templates]# ./hello.sh
    bash: ./hello.sh: Permission denied
    [root@localhost Templates]# 
    [root@localhost Templates]# mount -o remount,exec /home  
    [root@localhost Templates]# ./hello.sh                 
    Mr li is the hot man!
    [root@localhost Templates]# 
    
    1. 挂载光盘

      光盘,u盘,移动硬盘不会自动挂载,因为不能保证每次开机都连接到设备。需要手动挂载。

      现在较新的linux系统中光驱设备可自动挂载(在/media/cdrom下)

      mount查看可见挂载信息,通过挂载点可查看光盘内容

      [root@localhost dev]#mount
      .........
      /dev/sr0 on /run/media/jen/CentOS 7 x86_64 type iso9660 (ro,nosuid,nodev,relatime,uid=1000,gid=1000,iocharset=utf8,mode=0400,dmode=0500,uhelper=udisks2)
      
      [root@localhost jen]# cd /run/media/jen
      [root@localhost jen]# ls
      CentOS 7 x86_64
      [root@localhost jen]# cd CentOS\ 7\ x86_64/
      [root@localhost CentOS 7 x86_64]# ls
      CentOS_BuildTag  EFI  EULA  GPL  images  isolinux  LiveOS  Packages  repodata  RPM-GPG-KEY-CentOS-7  RPM-GPG-KEY-CentOS-Testing-7  TRANS.TBL
      [root@localhost CentOS 7 x86_64]# 
      

      若没有自动挂载可在Workstation-虚拟机-设置中勾选已连接(不选相当于未上电),选择光盘文件保存

      【linux 学习】5.文件系统管理

      终端中新建挂载点挂载。(iso9660 是光盘的默认文件系统。由于linux系统可以自动识别光盘的文件系统,故“-t iso9660”也可以省略不写)

      mkdir /mnt/cdrom		#建立挂载点
      mount -t iso9660 /dev/cdrom /mnt/cdrom/		#挂载光盘
      mount /dev/sr0 /mnt/cdrom		#设备文件名/dev/sr0与/dev/cdrom(软链接)等同
      
  3. 卸载命令

    umount 设备文件名/挂载点	(一旦挂载,二者建立连接,断开哪一边效果等同)
    umount /mnt/cdrom
    
  4. 挂载u盘

    fdisk -l	#查看设备文件名
    mount -t vfat /dev/sdb1 /mnt/usb
    
    
    • 挂载u盘时不能用远程工具,需用linux虚拟机,因为本机和虚拟机识别u盘存在竞争关系
    • u盘文件类型:linux默认不支持NTFS文件系统。

    支持NTF文件系统

    linux系统自动识别加载驱动,常见驱动都包含在内核中 ,但默认不支持NTFS

    解决方法:

    1. 重新编译内核,手动加载驱动(比较麻烦,推荐2)

    2. 插件ntfs-3g

      wget  https://tuxera.com/opensource/ntfs-3g_ntfsprogs-2017.3.23.tgz	#官方安装包下载 
      tar -zxvf ntfs-3g_ntfsprogs-2017.3.23.tgz		#解压缩
      cd ntfs-3g_ntfsprogs-2017.3.23			#编译安装
      ./configure
      make 
      make install
      

      如报错需要gcc支持,安装gcc即可yum install gcc

      mount -t ntfs-3g 分区设备文件名 挂载点
      

fdisk分区

分区过程

  1. 添加新硬盘

    虚拟机断电,在设置中添加即可(此处添加硬盘类型为SCSI)

  2. 查看分区情况

    [root@localhost jen]# fdisk -l			查看分区情况
    Disk /dev/sda: 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: 0x000750f0
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048      411647      204800   83  Linux
    /dev/sda2          411648    29358079    14473216   83  Linux
    /dev/sda3        29358080    37746687     4194304   83  Linux
    /dev/sda4        37746688    41943039     2098176    5  Extended
    /dev/sda5        37750784    41943039     2096128   82  Linux swap / Solaris
    
    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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
    
  3. 分区

    fdisk 待分区设备文件名
    
    fdisk交互指令说明:
    Command (m for help):
    	a			设置可引导标记
    	b			编辑bsd磁盘标签
    	c			设置dos操作系统兼容标志
    	d			删除一个分区
    	l			显示已知文件系统类型。82-linux swap分区,83-linux分区
    	m			帮助菜单
    	n			建立新分区
    	o			建立空白dos分区表
    	p			显示分区列表
    	q			不保存退出
    	s			建立空白sun磁盘标签
    	t			改变一个分区的系统id
    	u			改变显示记录单位
    	v			验证分区表
    	w			保存退出
    	x			附加功能(仅专家)
    基础操作:n,d,p,l,w,q
    
    [root@localhost jen]# fdisk /dev/sdb
    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 0xaa6240ab.
    Command (m for help): l			#显示已知文件系统类型
     0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
     1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
     2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
     3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
     4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
     5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
     6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
     7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
     8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
     9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
     a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
     b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
     c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
     e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
     f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
    10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
    11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
    12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
    14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
    16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
    17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
    18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
    1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
    1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
    1e  Hidden W95 FAT1 80  Old Minix 
    
    Command (m for help): p				#查看分区列表,目前为空/
    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xaa6240ab
    
    Device Boot      Start         End      Blocks   Id  System
    
    Command (m for help): n				#新建分区
    Partition type:
    p   primary (0 primary, 0 extended, 4 free)
    e   extended
    Select (default p): p				#选择主分区
    Partition number (1-4, default 1): 1		#序号,最好按顺序进行
    First sector (2048-20971519, default 2048): 		#起始扇区
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +2G		#结束
    Partition 1 of type Linux and of size 2 GiB is set
    
    Command (m for help): p			#查看
    
    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xaa6240ab
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): e										#新建扩展分区
    Partition number (2-4, default 2): 2
    First sector (4196352-20971519, default 4196352): 
    Using default value 4196352
    Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): 
    Using default value 20971519
    Partition 2 of type Extended and of size 8 GiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xaa6240ab
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux
    /dev/sdb2         4196352    20971519     8387584    5  Extended
    
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5)
    Select (default p): l						#新建逻辑分区
    Adding logical partition 5
    First sector (4198400-20971519, default 4198400): 
    Using default value 4198400
    Last sector, +sectors or +size{K,M,G} (4198400-20971519, default 20971519): +2G
    Partition 5 of type Linux and of size 2 GiB is set
    
    Command (m for help): p								#最后分区情况
    
    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xaa6240ab
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux
    /dev/sdb2         4196352    20971519     8387584    5  Extended
    /dev/sdb5         4198400     8392703     2097152   83  Linux
    
    Command (m for help): w				#保存退出
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
    
    
  4. 生效需重启系统或者重新读取分区表信息

    [root@localhost jen]# partprobe			#重新读取分区表信息
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    
  5. 格式化

    分区将“大柜子”分割成“小柜子”,格式化在“小柜子”中打入“隔断”

    [root@localhost jen]# mkfs -t ext4 /dev/sdb1			#格式化分区
    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
    131072 inodes, 524288 blocks
    26214 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=536870912
    16 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks: 
      32768, 98304, 163840, 229376, 294912
    Allocating group tables: done                            
    Writing inode tables: done                            
    Creating journal (16384 blocks): done
    Writing superblocks and filesystem accounting information: done 
    
  6. 挂载分区

    [root@localhost jen]# mkdir /disk1				#挂载分区
    [root@localhost jen]# mkdir /disk5
    [root@localhost jen]# mount /dev/sdb1 /disk1
    [root@localhost jen]# mount /dev/sdb5 /disk5
    [root@localhost jen]# mount
    ...
    /dev/sdb1 on /disk1 type ext4 (rw,relatime,seclabel,data=ordered)
    /dev/sdb5 on /disk5 type ext4 (rw,relatime,seclabel,data=ordered)
    

    fdisk只能看到分区是否被正常分配,不能看到挂载。mount或df可查看挂载情况

    [root@localhost jen]# fdisk -l		
    
    Disk /dev/sda: 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: 0x000750f0
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048      411647      204800   83  Linux
    /dev/sda2          411648    29358079    14473216   83  Linux
    /dev/sda3        29358080    37746687     4194304   83  Linux
    /dev/sda4        37746688    41943039     2098176    5  Extended
    /dev/sda5        37750784    41943039     2096128   82  Linux swap / Solaris
    
    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xaa6240ab
    
    Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048     4196351     2097152   83  Linux
    /dev/sdb2         4196352    20971519     8387584    5  Extended
    /dev/sdb5         4198400     8392703     2097152   83  Linux
    

    问题:手动挂载重启分区消失,永久需要写入文件/etc/fstab

自动挂载与fstab文件修复

/etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Sep 26 05:29:50 2020
#
# 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=f2ab1199-e774-4530-a05f-ed6c1b5394e5 /               ext4    defaults        1 1
UUID=ca096b0c-2338-499f-8c65-e47083b3a329 /boot           ext4    defaults        1 2
UUID=73a56e89-67f3-4035-ac4c-03e04ee5ec7e /home           ext4    defaults        1 2
UUID=6dd39111-1b8f-431f-b435-12c4646e1f5c swap            swap    defaults        0 0

格式:分区设备文件名/UUID(硬盘通用唯一识别码) 挂载点 文件系统名称 挂载参数/特殊权限 指定分区dump备份[0不1每天备份2不定期备份] 是否被fsck检测[0不检测其他num检测优先级]

  • UUID查看

    [root@localhost jen]# dumpe2fs /dev/sdb1
    dumpe2fs 1.42.9 (28-Dec-2013)
    Filesystem volume name:   <none>
    Last mounted on:          <not available>
    Filesystem UUID:          9fdb8aa3-c588-4aaf-a1d7-9f4af03a8d64
    Filesystem magic number:  0xEF53
    Filesystem revision #:    1 (dynamic)
    ...
    
  • 自动备份 挂载点下lost+found

    [root@localhost jen]# ls /boot
    config-3.10.0-957.el7.x86_64
    efi
    grub
    grub2
    initramfs-0-rescue-9f58fea569b74406b723fae5281cf442.img
    initramfs-3.10.0-957.el7.x86_64.img
    lost+found
    symvers-3.10.0-957.el7.x86_64.gz
    System.map-3.10.0-957.el7.x86_64
    vmlinuz-0-rescue-9f58fea569b74406b723fae5281cf442
    vmlinuz-3.10.0-957.el7.x86_64
    [root@localhost jen]# ls /home
    jen  lost+found  st  u1  u2
    [root@localhost jen]# 
    
  • 自动挂载修改/etc/fstab文件eg:

/dev/sdb1       /disk1  ext4    defaults        1 2
  • 文件写错或删除/disk 会使系统崩溃
	[root@localhost jen]# vim /etc/fstab
[root@localhost jen]# mount -a	#依据配置文件/etc/fstab内容自动挂载分区,可在修改文件后执行进行检验是否成功修改
[root@localhost jen]# 
  • 修复:

    重启后报错,输入root密码,vi修改fstab文件

    如文件只读,不可保存修改则重新挂载根目录/,加入读写权限

    mount -o remount,rw /
    

    局限性:只能修复文件报错;根分区错误,无法修改

上一篇:SHA算法原理与常用实现


下一篇:公有云技术