Loading

磁盘分区

分区方案

  • MBR

    • 分区类型:主分区,扩展分区和逻辑分区

    • 限制:最都有4个主分区,磁盘最大为2TB

  • GPT

    • GPT使用全局唯一标识符(GUID)来识别磁盘和分区

    • GPT提供分区表备份功能,主GPT位于磁盘头部,备份的GPT位于磁盘尾部

    • 限制:最多有128个分区,磁盘最大为8ZiB

分区工具

parted

  1. 创建MBR分区表

    [root@localhost ~]# parted /dev/sda		#进入交互式分区
    GNU Parted 3.2
    Using /dev/sda
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) mklabel    #设置分区表类型                                  
    New disk label type? msdos   #msdos表示MBR                               
    Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue?
    Yes/No? Yes                                                               
    (parted) print    #打印分区表信息                                    
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sda: 10.7GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start  End  Size  Type  File system  Flags
    

    parted命令更改会立即生效

    mklabel命令会擦除现有的分区表,修改之后,之前的所有数据无法访问

  2. parted工具可以直接在后面提供子命令,不需要进入交互界面

    [root@localhost ~]# parted /dev/sda mklabel msdos 
    Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue?
    Yes/No? Yes                                                               
    Information: You may need to update /etc/fstab.
    
    [root@localhost ~]# parted /dev/sda print                                 
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sda: 10.7GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start  End  Size  Type  File system  Flags
    
  3. 创建MBR分区

    [root@localhost ~]# parted /dev/sda
    GNU Parted 3.2
    Using /dev/sda
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) mkpart	    #添加新分区                                            
    Partition type?  primary/extended? primary    #选择分区类型为主分区           
    File system type?  [ext2]? xfs    #文件系统类型                                 
    Start? 1M       #起始位置                                                      
    End? 1024M      #结束位置                                                      
    (parted) p      #打印分区信息                                                  
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sda: 10.7GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags: 
    
    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  1024MB  1023MB  primary  xfs          lba
    

    parted工具并不能直接提供分区的大小,分区大小size=end-start

    分区的单位可以指定s(扇区),MB,GB和TB,默认是MB

  4. 无交互式创建MBR分区

    [root@localhost ~]# parted /dev/sda mkpart primary xfs 1G 2G
    Information: You may need to update /etc/fstab.
    
  5. 无交互式创建GPT分区

    [root@localhost ~]# parted /dev/sda mklabel gpt                           
    Warning: The existing disk label on /dev/sda will be destroyed and all data on this disk will be lost. Do you want to continue?
    Yes/No? Yes                                                               
    Information: You may need to update /etc/fstab.
    
    [root@localhost ~]# parted /dev/sda mkpart part1 xfs 1M 1024M
    Information: You may need to update /etc/fstab.
    

    对于gpt分区,每个分区必须指定一个名称,例如part1

  6. 删除分区

    [root@localhost ~]# parted /dev/sda
    GNU Parted 3.2
    Using /dev/sda
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sda: 10.7GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start   End     Size    File system  Name   Flags
     1      1049kB  1024MB  1023MB               part1
    
    (parted) rm 1      #删除Number为1的分区                                     
    (parted) p                                                                
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sda: 10.7GB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags: 
    
    Number  Start  End  Size  File system  Name  Flags
    
    (parted) 
    

fdisk

fdisk应用于MBR分区表

  • fdisk -l 查看磁盘和分区信息
  • fdisk /dev/sda 对指定设备进行分区
    • n:创建新的分区
    • d:删除分区
    • p:打印分区表
    • t:修改分区id
    • w:保存分区
    • q:不保存退出
[root@localhost ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n		#新建分区
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p		#选择分区类型
Partition number (1-4, default 1): 	#选择驱动号
First sector (2048-20971519, default 2048): 	#选择开始扇区位置
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +2G
# 选择结束位置,也可以使用加号指定大小
Created a new partition 1 of type 'Linux' and of size 2 GiB.

Command (m for help): p		#打印分区表
Disk /dev/sda: 10 GiB, 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
Disklabel type: dos
Disk identifier: 0x0bfbff5c

Device     Boot Start     End Sectors Size Id Type
/dev/sda1        2048 4196351 4194304   2G 83 Linux

Command (m for help): t	#修改分区id
Selected partition 1
Hex code (type L to list all codes): 8e		#8e代表LVM类型
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sda: 10 GiB, 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
Disklabel type: dos
Disk identifier: 0x0bfbff5c

Device     Boot Start     End Sectors Size Id Type
/dev/sda1        2048 4196351 4194304   2G 8e Linux LVM

Command (m for help): w		#保存退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# 

gdisk

gdisk应用于gpt分区表,使用方式fdisk一致

格式化文件系统

格式化就是在创建文件系统

[root@localhost ~]# mkfs.xfs /dev/sda1
meta-data=/dev/sda1              isize=512    agcount=4, agsize=131072 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=1
data     =                       bsize=4096   blocks=524288, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# 

挂载文件系统

[root@localhost ~]# mount /dev/sda1 /data

常用选项:

  • -t:指定文件系统类型
  • -r:只读挂载
  • -w:读写挂载
  • -a:自动挂载所有支持自动挂载的设备
  • -L:用卷标指定挂在设备
  • -U:用UUID指定挂在设备
  • -o [options]:指定挂载选项
    • sync/async:同步/异步模式
    • atime/noatime:是否更新atime,包含目录和文件
    • auto/noauto:是否支持自动挂载
    • exec/noexec:是否支持文件系统上的可执行文件运行
    • dev/nodev:是否支持在此文件系统上使用设备文件
    • suid/nosuid:是否支持suid权限
    • remount:重新挂载
    • ro/rw:只读或者读写挂载
    • user/nouser:是否允许普通用户挂载此设备
    • defaults:默认挂载选项,是rw,suid,dev,exec,auto,nouser,async的组合

永久挂载

[root@localhost ~]# cat /etc/fstab | grep /dev/sda
/dev/sda1	/data	xfs		defaults	 0 			0
要挂载的设备	挂在点	文件系统类型	挂载选项	转存频率	自检顺序
  • 要挂载的设备:设备文件,或UUID
  • 挂载选项:
  • 转存频率:默认为0,不备份
  • 自检顺序:用fsck按顺序检查文件系统,对于ext4文件系统,该值为1,其他ext,该值为2;对于xfs文件系统,该值为0,因为xfs不用fsck检查。

卸载命令

#卸载设备文件或者挂载点
[root@localhost ~]# umount /dev/sda1	
[root@localhost ~]# umount /data

查看正在访问文件系统的进程

[root@localhost ~]# fuser -v /data
                     USER        PID ACCESS COMMAND
/data:               root     kernel mount /data
                     root       4367 ..c.. bash
                     root       4392 ..c.. vim

配置交换分区

交换分区保存内存中暂时不活动的数据

  1. 使用分区工具创建swap分区

    [root@localhost ~]# parted /dev/sda mkpart primary linux-swap 1M 1G
    Information: You may need to update /etc/fstab.
    
  2. 格式化swap分区

    [root@localhost ~]# mkswap /dev/sda1
    mkswap: /dev/sda1: warning: wiping old xfs signature.
    Setting up swapspace version 1, size = 953 MiB (999288832 bytes)
    no label, UUID=77740781-4dcd-4ddd-a4a0-e9617bae4ab9
    
  3. 挂载swap分区

    [root@localhost ~]# swapon /dev/sda1 
    [root@localhost ~]# swapon -s
    Filename				Type		Size	Used	Priority
    /dev/dm-1                              	partition	2097148	0	-2
    /dev/sda1                              	partition	975868	0	-3
    
  • swapon -a 挂载/etc/fstab中所有未挂载的swap分区
  • swapon -s 查看所有已挂载的swap分区
  • swapon -p 指定优先级
  • swapon /dev/sda1 挂载swap分区
  • swapoff /dev/sda1 卸载swap分区

其他磁盘管理工具

  • blkid打印文件系统属性信息

    [root@localhost ~]# blkid
    /dev/nvme0n1: PTUUID="a79dfc8b" PTTYPE="dos"
    /dev/nvme0n1p1: UUID="4b04a473-38a8-4832-bc4c-52c12ac65058" TYPE="xfs" PARTUUID="a79dfc8b-01"
    
  • lsblk列出块设备信息

    [root@localhost ~]# lsblk
    NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
    sda             8:0    0   10G  0 disk 
    └─sda1          8:1    0  953M  0 part [SWAP]
    sr0            11:0    1 1024M  0 rom  
    nvme0n1       259:0    0   20G  0 disk 
    ├─nvme0n1p1   259:1    0  500M  0 part /boot
    └─nvme0n1p2   259:2    0 19.5G  0 part 
      ├─rhel-root 253:0    0 17.5G  0 lvm  /
      └─rhel-swap 253:1    0    2G  0 lvm  [SWAP]
    [root@localhost ~]# lsblk --fs
    NAME          FSTYPE      LABEL UUID                                   MOUNTPOINT
    sda                                                                    
    └─sda1        swap              77740781-4dcd-4ddd-a4a0-e9617bae4ab9   [SWAP]
    sr0                                                                    
    nvme0n1                                                                
    ├─nvme0n1p1   xfs               4b04a473-38a8-4832-bc4c-52c12ac65058   /boot
    └─nvme0n1p2   LVM2_member       0xeyHg-Jamf-KyBX-TQ5W-l8SP-Wd05-wSyB9N 
      ├─rhel-root xfs               cbc0d96e-0099-41c3-bb90-3c475f590c5c   /
      └─rhel-swap swap              339c094b-b14e-47f9-bec3-951eb5aafb6d   [SWAP]
    
posted @ 2020-12-16 00:14  北兢王  阅读(255)  评论(0)    收藏  举报