Linux_磁盘分区
一、磁盘分区:
1、分区之前,需要先给虚拟机添加一块磁盘,以便于我们做后续的实验vmware虚拟机,请按如下进行操作
- 1.找到对应虚拟主机点击右键,选择设置
- 2.在硬件向导里面点击添加按钮,在硬件类型中选中“硬盘”,点击下一步
- 3.磁盘类型选择默认,然后创建新虚拟磁盘,调整大小(不要勾选立即分配空间)
- 4.最后点击下一步,完成即可
2、分区工具有fdisk和gdisk,当硬盘小于2T的时候我们应该用fdisk来分区,而当硬盘大于2T的时候则应用gdisk来进行分区
二、使用fidsk创建分区
1、fdisk命令
- fdisk常用选项:
- -l 列出素所有分区表
- -u 与"-l"搭配使用,显示分区数目
2、fdisk菜单操作说明:
Command (m for help): m //输入m列出常用的命令 Command action a toggle a bootable flag //切换分区启动标记 b edit bsd disklabel //编辑sdb磁盘标签 c toggle the dos compatibility flag //切换dos兼容模式 d delete a partition //删除分区 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 7 //创建新的Sun磁盘标签 t change a partition's system id //修改分区ID,可以通过1查看id u change disp lay/entry units //修改容量单位,磁柱或扇区 V verify the partition table //检验分区表 w write table to disk and exit //保存退出 x extra functionality (experts only) //拓展功能
3、实例:
- 使用fdisk创建一个主分区(+5G)
[root@localhost ~]# fdisk /dev/nvme0n2 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. Device does not contain a recognized partition table. Created a new DOS disklabel with disk identifier 0xc3bdb27b. 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): 1 //分区编号,默认是1 First sector (2048-104857599, default 2048): //起始大小,默认大小是2048M Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): +5G //该分区的大小是5G Created a new partition 1 of type 'Linux' and of size 5 GiB. Command (m for help): p //列出分区信息 Disk /dev/nvme0n2: 50 GiB, 53687091200 bytes, 104857600 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: 0xc3bdb27b Device Boot Start End Sectors Size Id Type /dev/nvme0n2p1 2048 10487807 10485760 5G 83 Linux Command (m for help): w //保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. //加载分区信息 [root@localhost ~]# partprobe Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. //查看分区信息 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 7.3G 0 rom /mnt nvme0n1 259:0 0 120G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 119G 0 part ├─rhel-root 253:0 0 50G 0 lvm / ├─rhel-swap 253:1 0 2G 0 lvm [SWAP] └─rhel-home 253:2 0 67G 0 lvm /home nvme0n2 259:3 0 50G 0 disk └─nvme0n2p1 259:5 0 5G 0 part
- 使用fdisk创建一个扩展分区(剩余空间全部)
[root@localhost ~]# fdisk /dev/nvme0n2 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 (1 primary, 0 extended, 3 free) e extended (container for logical partitions) Select (default p): e //新建一个额外的分区(扩展分区) Partition number (2-4, default 2): 2 //分区编号,默认是2 First sector (10487808-104857599, default 10487808): //该分区起始大小 Last sector, +sectors or +size{K,M,G,T,P} (10487808-104857599, default 104857599): //结束大小;扩展分区分配剩余所有的空间大小 Created a new partition 2 of type 'Extended' and of size 45 GiB. Command (m for help): p //列出分区信息 Disk /dev/nvme0n2: 50 GiB, 53687091200 bytes, 104857600 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: 0x8f03f9d8 Device Boot Start End Sectors Size Id Type /dev/nvme0n2p1 2048 10487807 10485760 5G 83 Linux /dev/nvme0n2p2 10487808 104857599 94369792 45G 5 Extended Command (m for help): w //保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# partprobe Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. //查看分区信息 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 7.3G 0 rom /mnt nvme0n1 259:0 0 120G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 119G 0 part ├─rhel-root 253:0 0 50G 0 lvm / ├─rhel-swap 253:1 0 2G 0 lvm [SWAP] └─rhel-home 253:2 0 67G 0 lvm /home nvme0n2 259:3 0 50G 0 disk ├─nvme0n2p1 259:4 0 5G 0 part └─nvme0n2p2 259:5 0 1K 0 part
- 使用fdisk在扩展分区里面创建逻辑分区(+10G)
[root@localhost ~]# fdisk /dev/nvme0n2 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 //新建一个分区 All space for primary partitions is in use. Adding logical partition 5 //默认的第一个逻辑分区编号是从5开始 First sector (10489856-104857599, default 10489856): //该分区的起始大小 Last sector, +sectors or +size{K,M,G,T,P} (10489856-104857599, default 104857599): +10G //分区结束大小10G Created a new partition 5 of type 'Linux' and of size 10 GiB. Command (m for help): p //查看分区列表信息 Disk /dev/nvme0n2: 50 GiB, 53687091200 bytes, 104857600 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: 0x8f03f9d8 Device Boot Start End Sectors Size Id Type /dev/nvme0n2p1 2048 10487807 10485760 5G 83 Linux /dev/nvme0n2p2 10487808 104857599 94369792 45G 5 Extended /dev/nvme0n2p5 10489856 31461375 20971520 10G 83 Linux Command (m for help): w //保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. //重新读取分区表 [root@localhost ~]# partprobe Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only. //查看分区信息 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 7.3G 0 rom /mnt nvme0n1 259:0 0 120G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 119G 0 part ├─rhel-root 253:0 0 50G 0 lvm / ├─rhel-swap 253:1 0 2G 0 lvm [SWAP] └─rhel-home 253:2 0 67G 0 lvm /home nvme0n2 259:3 0 50G 0 disk ├─nvme0n2p1 259:7 0 5G 0 part ├─nvme0n2p2 259:8 0 1K 0 part └─nvme0n2p5 259:9 0 10G 0 part
三、磁盘分区格式化
1、使用mkfs(make filesystem)命令格式化磁盘,创建文件系统:
- mkfs(make filesystem)常用的选项有:
- -b //设定数据区块占用空间大小, 目前支持1024、2048、4096 bytes每个块大小
- -t //用来指定什么类型的文件系统,可以是ext3,ext4, xfs
- -i //设定inode的大小
- -N //设定inode数量,防止Inode 数量不够导致磁盘不足
- -L //'LABEL': 设定卷标
-
mkfs示例:
- mkfs.ext4 /dev/DEVICENAME
- mkfs -t ext4 /dev/DEVICENAME
-
mke2fs:ext系列文件系统专用管理工具:
- -t {ext2|ext3|ext4}
- -b {1024|2048|4096}
- -L 'LABEL':设定卷标
- -j: 相当于 -t ext3
2、格式化文件系统类型:
- 当前系统支持的文件系统:cat /etc/filesystems
- Linux文件系统: ext2, ext3, ext4, xfs, swap, iso9660
- ext4: 默认文件系统
- swap: 交换分区,相当于虚拟内存(当内存耗尽前才会使用)
- 光盘:iso9660
- Windows:FAT32, NTFS
- Unix: FFS, UFS, JFS2
- 网络文件系统:NFS, CIFS
3、使用blkid查看格式化后设备的属性信息
- blkid:块设备属性信息查看
- blkid [OPTION]... [DEVICE]
- -U UUID: 根据指定的UUID来查找对应的设备
- -L LABEL:根据指定的LABEL来查找对应的设备
- blkid [OPTION]... [DEVICE]
- e2label:管理ext系列文件系统的LABEL
- e2label DEVICE [LABEL]
4、使用mkfs格式化主分区:
//查看以及创建的主分区 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 7.3G 0 rom /mnt nvme0n1 259:0 0 120G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 119G 0 part ├─rhel-root 253:0 0 50G 0 lvm / ├─rhel-swap 253:1 0 2G 0 lvm [SWAP] └─rhel-home 253:2 0 67G 0 lvm /home nvme0n2 259:3 0 50G 0 disk └─nvme0n2p1 259:4 0 5G 0 part //以文件名格式化主分区 [root@localhost ~]# mkfs.ext4 /dev/nvme0n2p1 mke2fs 1.44.6 (5-Mar-2019) Creating filesystem with 1310720 4k blocks and 327680 inodes Filesystem UUID: 47dcacad-d395-4cdf-806a-28f7cd126a2b Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done //查看已经格式化的主分区的属性 [root@localhost ~]# blkid /dev/nvme0n2p1 /dev/nvme0n2p1: UUID="47dcacad-d395-4cdf-806a-28f7cd126a2b" TYPE="ext4" PARTUUID="8f03f9d8-01"
- 使用mkfs -t格式化主分区
//查看分区信息 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 7.3G 0 rom /mnt nvme0n1 259:0 0 120G 0 disk ├─nvme0n1p1 259:1 0 1G 0 part /boot └─nvme0n1p2 259:2 0 119G 0 part ├─rhel-root 253:0 0 50G 0 lvm / ├─rhel-swap 253:1 0 2G 0 lvm [SWAP] └─rhel-home 253:2 0 67G 0 lvm /home nvme0n2 259:3 0 50G 0 disk └─nvme0n2p1 259:5 0 5G 0 part //格式化主分区 [root@localhost ~]# mkfs -t ext4 /dev/nvme0n2p1 mke2fs 1.44.6 (5-Mar-2019) Creating filesystem with 1310720 4k blocks and 327680 inodes Filesystem UUID: a9622acc-4419-4416-9d16-ed7fa1a2b476 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done //查看格式化后主分区的属性 [root@localhost ~]# blkid /dev/nvme0n2p1 /dev/nvme0n2p1: UUID="a9622acc-4419-4416-9d16-ed7fa1a2b476" TYPE="ext4" PARTUUID="8f03f9d8-01"