Centos磁盘管理

1、实验环境:

[root@Script-test ~]# cat /etc/redhat-release 
CentOS Linux release 7.7.1908 (Core)

在这里插入图片描述
掌握命令:df、du、fdisk、parted、vgextend
注:df和du的计算方式不一样,du是计算文件内容大小,df计算的空间大小,包括系统产生的垃圾文件、临时文件等。

2、MBR分区

在这里插入图片描述

创建磁盘分区

创建主分区
[root@Script-test ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。

更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

命令(输入 m 获取帮助):m    #查看帮助命令
命令操作
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition   #删除分区
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition    #添加一个新分区
   o   create a new empty DOS partition table
   p   print the partition table  #打印分区表
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id   #更改分区的系统id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit  #将表写入磁盘并退出
   x   extra functionality (experts only)

命令(输入 m 获取帮助):n  
Partition type:
   p   primary (0 primary, 0 extended, 4 free)      #主分区
   e   extended                                     #扩展分区
Select (default p): p                               #创建主分区
Partition number (1-4, default 1): 1                #选择分区号,范围为1~4(主分区和扩展分区加起来最多只能建立 4 个,而扩展分区最多只能建立 1 个),这里选择1
First sector (2048-2147483647, default 2048): 2048  #分区的起始扇面,默认从2048开始  
Last sector, +sectors or +size{K,M,G} (2048-2147483647, default 2147483647): +1G           #指定该磁盘分区的大小,默认占用整个磁盘;这里用 +size{K,M,G} 命令指定 1G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): p                           #打印当前磁盘的分区列表

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x9bd3dede

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
创建扩展分区
命令(输入 m 获取帮助):n   
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e                     #创建扩展分区
分区号 (2-4,默认 2):
起始 扇区 (2099200-10485759,默认为 2099200):
将使用默认值 2099200
Last 扇区, +扇区 or +size{K,M,G} (2099200-10485759,默认为 10485759):
将使用默认值 10485759
分区 2 已设置为 Extended 类型,大小设为 4 GiB

命令(输入 m 获取帮助):p        #打印分区表

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x9bd3dede

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200    10485759     4193280    5  Extended
创建逻辑分区
命令(输入 m 获取帮助):n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l              #创建逻辑分区
添加逻辑分区 5
起始 扇区 (2101248-10485759,默认为 2101248):
将使用默认值 2101248
Last 扇区, +扇区 or +size{K,M,G} (2101248-10485759,默认为 10485759):
将使用默认值 10485759
分区 5 已设置为 Linux 类型,大小设为 4 GiB

命令(输入 m 获取帮助):p

磁盘 /dev/sdb:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x9bd3dede

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200    10485759     4193280    5  Extended    #扩展分区
/dev/sdb5         2101248    10485759     4192256   83  Linux       #逻辑分区
将分区信息写入磁盘
命令(输入 m 获取帮助):w          #将分区写入磁盘
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。

疑问:为什么要创建扩展分区呢?
答:由于主分区和扩展分区加起来最多只能创建4个。但扩展分区中可以创建更多的逻辑分区,譬如上述/dev/sdb5(逻辑分区)位于/dev/sdb2 (扩展分区)底下,如果删除扩展分区即逻辑分区也会一并删除。

格式化分区

查看磁盘文件类型:blkid /dev/sdb(数字)

[root@Script-test ~]# mkfs -t ext3 /dev/sdb1        #ext3为文件类型,可自定义选择
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
第一个数据块=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: 完成                            
正在写入inode表: 完成                            
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成 

挂载磁盘分区

[root@Script-test mnt]# mkdir test_sdb1     #创建挂载点
[root@Script-test mnt]# ls
test_sdb1
[root@Script-test mnt]# pwd                 #一般情况下将磁盘挂载在/mnt目录下
/mnt

[root@Script-test ~]# mount /dev/sdb1 /mnt/test_sdb1/     #将/dev/sdb1挂载
[root@Script-test ~]# df -T -h |grep /dev/sdb1            #查看挂载情况
/dev/sdb1               ext3      2.0G  3.1M  1.9G    1% /mnt/test_sdb1

 vim /etc/fstab                              #设置开机自动挂载
  2 #
  3 # /etc/fstab
  4 # Created by anaconda on Fri Feb 11 15:11:24 2022
  5 #
  6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
  7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
  8 #
  9 /dev/mapper/centos-root /                       xfs     defaults        0 0
 10 UUID=ebbbd049-9931-452a-86d8-c09910705d05 /boot                   xfs     defaults        0 0
 11 /dev/mapper/centos-swap swap                    swap    defaults        0 0
 12 /dev/sdb1 /mnt/test_sdb1 etx3  defaults 1 2       #将分区设为开机自动挂载
 13 #  /dev/sdb1 选择一个分区;xfs 是当前分区的格式; defaults = rw, suid, dev, exec, auto, nouser, and async; 1 需要 dump;0 不需要 dump; 2 开机时检查顺序,非 boot 文    件系统为 1,其它文件系统都为 2                               

删除磁盘分区

[root@Script-test ~]# 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.

Command (m for help): d                                         # 删除分区
Partition number (1-6, default 6): 6                            #选择待删除的分区号
Partition 6 is deleted

Command (m for help): w                                         #保存设置
The partition table has been altered!

Calling ioctl() to re-read partition table.

参考链接:https://www.cnblogs.com/SchrodingerDoggy/p/13746237.html

3、GPT分区

[root@Script-test ~]# parted
GNU Parted 3.1
使用 /dev/sda                  #默认进去第一块硬盘
Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) help
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  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
  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
  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) select /dev/sdb                                                  
使用 /dev/sdb
指定文件格式
mklabel msdos     ##指定mbr格式

mklabel  gpt      ##指定gpt,本次实验使用GPT格式
检查文件格式
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt       #GPT格式
Disk Flags: 

Number  Start  End  Size  File system  Name  标志

parted创建分区有两种方法:1、交互模式(类似fdisk一样)2、命令行模式(一行命令创建完成)

创建分区

交互模式:

(parted) mkpart
分区名称?  []? test                                                      
文件系统类型?  [ext2]? ext3                                              
起始点? 1                  #起始点如果输入0会有警告,说没有对齐。 起始点和结束点默认单位是:MB                                                  
结束点? 3000               #切换单位。unit GB                                              
(parted) print                                                            
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  标志
 1      1049kB  3000MB  2999MB  ext3         test

命令行模式:

(parted) mkpart minglinghang 3000 3500    # 一条命令创建
(parted) print                                                            
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name          标志
 1      1049kB  3000MB  2999MB  ext3         test
 2      3000MB  3500MB  500MB                minglinghang

删除分区
(parted) print 
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name          标志
 1      1049kB  3000MB  2999MB  ext3         test
 2      3000MB  3500MB  500MB                minglinghang

(parted) rm 2                       #rm num  即删除                                        
(parted) print                                                            
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 5369MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  标志
 1      1049kB  3000MB  2999MB  ext3         test

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值