L50.linux命令每日一练 -- 第八章 Linux磁盘与文件系统管理命令 -- tune2fs和parted

8.3 tune2fs:调整ext2/ext3/ext4文件系统参数

8.3.1 命令详解

【命令星级】 ★☆☆☆☆

【功能说明】

​ tune2fs命令可以调整或查看ext2/ext3/ext4文件系统的参数,比如可以调整Linux文件系统开机自检的周期,此参数在工作中极少使用,读者了解即可。

【语法格式】

tune2fs [option]
tune2fs [选项]

​ **说明:**在tune2fs命令及后面的选项里,每个元素之间都至少要有一个空格。

【选项说明】

​ 表8-4针对该命令的参数选项进行了说明。

​ 表8-4 tune2fs命令的参数选项及说明

在这里插入图片描述

8.3.2 使用范例

​ **范例8-5:**查看文件系统挂载信息。

[root@centos7 ~]# tune2fs -l /dev/sdb1 |grep -i Mount	#查看/dev/sdb1设备分区的挂载次数。
Last mounted on:          <not available>
Default mount options:    user_xattr acl
Last mount time:          Mon Oct 26 15:27:07 2020
Mount count:              1	#挂载次数。
Maximum mount count:      1	#强制自检的挂载次数1,-1为关闭自检功能。

​ 范例8-6:设置挂载次数。

[root@centos7 ~]# tune2fs -C 30 /dev/sdb1	#参数-C设置文件系统已经被挂载的次数。
tune2fs 1.42.9 (28-Dec-2013)
Setting current mount count to 30
[root@centos7 ~]# tune2fs -l /dev/sdb1 |grep -i Mount
Last mounted on:          <not available>
Default mount options:    user_xattr acl
Last mount time:          Mon Oct 26 15:27:07 2020
Mount count:              30	#由1变为30。
Maximum mount count:      1

​ **范例8-7:**设置强制自检的挂载次数。

[root@centos7 ~]# tune2fs -c 40 /dev/sdb1	#参数-c设置强制自检的挂载次数。
tune2fs 1.42.9 (28-Dec-2013)
Setting maximal mount count to 40
[root@centos7 ~]# tune2fs -l /dev/sdb1 |grep -i Mount
Last mounted on:          <not available>
Default mount options:    user_xattr acl
Last mount time:          Mon Oct 26 15:27:07 2020
Mount count:              30
Maximum mount count:      40	#由1变为40。
[root@centos7 ~]# tune2fs -c -1 /dev/sdb1	#关闭自动检查等功能。
tune2fs 1.42.9 (28-Dec-2013)
Setting maximal mount count to -1
[root@centos7 ~]# tune2fs -l /dev/sdb1 |grep -i Mount
Last mounted on:          <not available>
Default mount options:    user_xattr acl
Last mount time:          Mon Oct 26 15:27:07 2020
Mount count:              30
Maximum mount count:      -1

​ **范例8-8:**设置强制自检的时间间隔。

[root@centos7 ~]# tune2fs -l /dev/sdb1 |grep -i 	check	#查看检查周期。
Last checked:             Mon Oct 26 15:25:11 2020
Check interval:           0 (<none>)	#系统分区默认不检查。
[root@centos7 ~]# tune2fs -i 10 /dev/sdb1	#参数-i设置每10天检查一次。	
tune2fs 1.42.9 (28-Dec-2013)
Setting interval between checks to 864000 seconds
[root@centos7 ~]# tune2fs -l /dev/sdb1 |grep -i check
Last checked:             Mon Oct 26 15:25:11 2020
Check interval:           864000 (1 week, 3 days)	#这里变成10天,864000秒。
Next check after:         Thu Nov  5 15:25:11 2020
[root@centos7 ~]# tune2fs -i 0 /dev/sdb1	#还原正常状态。
tune2fs 1.42.9 (28-Dec-2013)
Setting interval between checks to 0 seconds
[root@centos7 ~]# tune2fs -l /dev/sdb1 |grep -i check
Last checked:             Mon Oct 26 15:25:11 2020
Check interval:           0 (<none>)

8.4 parted:磁盘分区工具

8.4.1 命令详解

【命令星级】 ★★★★★

【功能说明】

​ 对于小于2TB的磁盘可以用fdisk和parted命令进行分区,这种情况一般采用fdisk命令,但对于大于2TB的磁盘则只能用parted分区,且需要将磁盘转换为GPT格式。

【语法格式】

parted [option] [device]
parted [选项] [设备名]

​ **说明:**在parted命令及后面的选项和设备名里,每个元素之间都至少要有一个空格。

【选项说明】

​ 表8-5针对该命令的参数选项进行了说明。

​ 表8-5 parted命令的参数选项及说明
在这里插入图片描述
【分区命令】

​ 通过parted -h或者直接parted进入交互式模式之后输入h可查看帮助。

[root@centos7 ~]# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
                                                                    (parted) h	#查看帮助。
  align-check TYPE N                        check partition N for
        TYPE(min|opt) alignment	#检查磁盘分区,TYPE分为min/pot两个选择,N为磁盘分区号。
  help [COMMAND]                           print general help, or
        help on COMMAND		#查看帮助,简写h,还可以类似于help check查看其他指令帮助。
  mklabel,mktable LABEL-TYPE               create a new disklabel
        (partition table)	#创建分区表*。
  mkpart PART-TYPE [FS-TYPE] START END     make a partition	#创建带有文件系统的分区*。
  name NUMBER NAME                         name partition NUMBER as
        NAME	#为分区命名。
  print [devices|free|list,all|NUMBER]     display the partition
        table, available devices, free space, all found partitions,
        or a particular partition	#显示分区表信息,简写p*。
  quit                                     exit program		#退出程序。
  rescue START END                         rescue a lost partition
        near START and END	#挽救临近“起始点”、“终止点”的遗失的分区。
  
  resizepart NUMBER END                    resize partition NUMBER	#重设分区大小。
  rm NUMBER                                delete partition NUMBER	#删除编号NUMBER的分区*。
  select DEVICE                            choose the device to edit	#选择要编辑的设备。
  disk_set FLAG STATE                      change the FLAG on
        selected device		
  disk_toggle [FLAG]                       toggle the state of FLAG
        on selected device	
  set NUMBER FLAG STATE                    change the FLAG on
        partition NUMBER	#改变分区的标志。
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG
        on partition NUMBER		#设置分区标志。
  unit UNIT                                set the default unit to
        UNIT		#设置默认单位。
  version                                  display the version
        number and copyright information of GNU Parted		#显示版本号。
                                                                    (parted) quit

​ **特别强调:**带*号的是重点掌握的,其他命令了解即可,或者干脆视而不见,未来需要时再查即可。

8.4.2 使用范例

8.4.2.1 基础范例

​ **范例8-9:**显示分区情况的例子。

[root@centos7 ~]# parted -l	#显示所有磁盘分区的信息。
Model: VMware, VMware Virtual S (scsi)	#磁盘型号,这里采用VMware虚拟化演示。
Disk /dev/sda: 21.5GB	#磁盘大小。
Sector size (logical/physical): 512B/512B	#扇区大小,为msdos,这里适合fdisk分区的类型。
Partition Table: msdos	#分区表类型。
Disk Flags: 

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  1075MB  1074MB  primary  xfs             boot
 2      1075MB  3222MB  2147MB  primary  linux-swap(v1)
 3      3222MB  21.5GB  18.3GB  primary  xfs


Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size   Type      File system  Flags
 1      1049kB  106MB   105MB  primary   ext4
 2      106MB   1074MB  968MB  extended
 5      107MB   526MB   419MB  logical
 6      527MB   1074MB  546MB  logical


Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0
has been opened read-only.
                                                                    Model: NECVMWar VMware IDE CDR10 (scsi)
Disk /dev/sr0: 10.3GB
Sector size (logical/physical): 2048B/2048B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 2      11.7MB  47.4MB  35.7MB  primary


​ 上述内容每列说明具体如下:

​ Number:分区编号。

​ Start:分区开始位置。

​ End:分区结束位置。

​ Size:分区大小。

​ Type:分区类型。

​ primary:为主分区。

​ File system:文件系统,例如ext4、xfs、swap等。

​ Flags:标志位,boot为启动分区。

8.4.2.2 技巧性范例

​ **范例8-10:**在虚拟机(VMware Workstation Pro)中模拟2TB以上的磁盘分区。

​ 步骤1:在虚拟机中添加一块1GB的硬盘,如图8-2所示。
在这里插入图片描述
​ 图8-2 在虚拟机中添加1GB的新硬盘

​ 步骤2:交互式分区。

[root@centos7 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
                                                     (parted) mklabel gpt	#为sdb磁盘创建GPT分区表,大于2TB的磁盘必须执行这一步。
Warning: The existing disk label on /dev/sdb will be destroyed and
all data on this disk will be lost. Do you want to continue?
                                                                                                          Yes/No? yes	#是否继续,yes。
                                                     (parted) mkpart primary 0 500	#创建主分区,大小为500MB。
Warning: The resulting partition is not properly aligned for best
performance.
                                                                                                         Ignore/Cancel? Ignore	#输入Ignore忽略警告。
                                                     (parted) p	#显示分区表信息。
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name     Flags
 1      17.4kB  500MB  500MB               primary
#第一个主分区已创建完毕。
                                                     (parted) mkpart logical 501 1000	#创建逻辑分区,大小为500MB。
                                                     (parted) p	#显示分区表信息。
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size   File system  Name     Flags
 1      17.4kB  500MB   500MB               primary
 2      501MB   1000MB  499MB               logical
#第一个逻辑分区已创建完毕。
                                                     (parted) quit	#退出。
Information: You may need to update /etc/fstab.

                                                     [root@centos7 ~]# ls /dev/sdb*		#查看已分的分区,parted分区是即时生效,没有保存的步骤。
/dev/sdb  /dev/sdb1  /dev/sdb2

​ 后续格式化分区、分区挂载、开机自动挂载等步骤和fdisk分区实践的范例8-2完全相同,读者可前往参考。

​ **范例8-11:**非交互式分区(批量分区)案例。

[root@centos7 ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
                                                                    (parted) p	#显示分区表信息。
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size   File system  Name     Flags
 1      17.4kB  500MB   500MB               primary
 2      501MB   1000MB  499MB               logical

                                                     (parted) rm 1	#删除第1个分区。
                                                     (parted) rm 2	#删除第2个分区。
                                                     (parted) p		#显示分区表信息。
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

                                                     (parted) q	#退出。
Information: You may need to update /etc/fstab.

[root@centos7 ~]# ls /dev/sdb	#查看设备信息。
/dev/sdb                                             

​ 步骤2:非交互式创建分区命令。

[root@centos7 ~]# parted /dev/sdb mklabel gpt Yes	#非交互创建分区的实质上就是将在交互窗口执行的命令作为参数。
Warning: The existing disk label on /dev/sdb will be destroyed and
all data on this disk will be lost. Do you want to continue?
Information: You may need to update /etc/fstab.

[root@centos7 ~]# parted /dev/sdb mkpart primary 0 500 Ignore	#将交互执行的命令直接放在parted /dev/sdb后面就实现非交互分区了。
Warning: The resulting partition is not properly aligned for best
performance.
Information: You may need to update /etc/fstab.

[root@centos7 ~]# parted /dev/sdb mkpart primary 500 1000
Information: You may need to update /etc/fstab.

[root@centos7 ~]# parted /dev/sdb p	#查看分区结果。
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size   File system  Name     Flags
 1      17.4kB  500MB   500MB               primary	#分好的第一个500MB的分区。
 2      500MB   1000MB  500MB               primary
#分好的第二个500MB的分区。

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/125687448