Linux磁盘分区
时间:2022-04-03 16:10
1、添加10G的硬盘。 实际上在服务器上支持热插拔
2、fdisk -l查看 [root@linux-xl ~]# fdisk -l #如果识别不到新插的硬盘,查看磁盘,虚拟机正常需要重启才能加载新的磁盘。 Disk /dev/sda: 53.7 GB, 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 Disk label type: dos Disk identifier: 0x000cd7c5 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 94373887 46137344 83 Linux /dev/sda3 94373888 102502399 4064256 82 Linux swap / Solaris /dev/sda4 102502400 104857599 1177600 5 Extended /dev/sda5 102504448 104601599 1048576 83 Linux Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors #这里的sdb为新加的磁盘 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是mbr分区模式,最高只支持2TB,最多只有主分区primary加上扩展分区extended为4,想要多个分区需要第四个分区为扩展分区extended,并在扩展分区里面分多个逻辑分区。 扩展分区仅仅只是一个外壳不支持格式化,真正存储的是逻辑分区。 [root@linux-xl ~]# 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 0x189fc246. Command (m for help): n #创建一个新的分区(这里通常使用的是 n:创建一个新的分区;p:罗列出分区;w:保存并退出;d:删除一个分区;q:不保存直接退出) 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 #划分2个G的空间。 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: 0x189fc246 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): #默认回车 Using default response p Partition number (2-4, default 2): #默认回车 First sector (4196352-20971519, default 4196352): #默认回车 Using default value 4196352 Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +2G #新建2G的主分区 Partition 2 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: 0x189fc246 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux /dev/sdb2 4196352 8390655 2097152 83 Linux Command (m for help): n #新建一个分区 Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): #回车默认主分区 Using default response p Partition number (3,4, default 3): #默认回车从3开始 First sector (8390656-20971519, default 8390656): #默认回车 Using default value 8390656 Last sector, +sectors or +size{K,M,G} (8390656-20971519, default 20971519): +1G #划分1个G的主分区 Partition 3 of type Linux and of size 1 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: 0x189fc246 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux /dev/sdb2 4196352 8390655 2097152 83 Linux /dev/sdb3 8390656 10487807 1048576 83 Linux Command (m for help): n #新建分区 Partition type: p primary (3 primary, 0 extended, 1 free) e extended Select (default e): e #划分扩展分区 Selected partition 4 First sector (10487808-20971519, default 10487808): #默认回车 Using default value 10487808 Last sector, +sectors or +size{K,M,G} (10487808-20971519, default 20971519): #默认回车划分剩余空间给扩展分区 Using default value 20971519 Partition 4 of type Extended and of size 5 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: 0x189fc246 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux /dev/sdb2 4196352 8390655 2097152 83 Linux /dev/sdb3 8390656 10487807 1048576 83 Linux /dev/sdb4 10487808 20971519 5241856 5 Extended Command (m for help): n #可以看到提示变了,只能是划分逻辑分区了。 All primary partitions are in use Adding logical partition 5 First sector (10489856-20971519, default 10489856): Using default value 10489856 Last sector, +sectors or +size{K,M,G} (10489856-20971519, default 20971519): +1G #划分1G大小的逻辑分区 Partition 5 of type Linux and of size 1 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: 0x189fc246 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux /dev/sdb2 4196352 8390655 2097152 83 Linux /dev/sdb3 8390656 10487807 1048576 83 Linux /dev/sdb4 10487808 20971519 5241856 5 Extended /dev/sdb5 10489856 12587007 1048576 83 Linux Command (m for help): n #再划分一个逻辑分区 All primary partitions are in use Adding logical partition 6 First sector (12589056-20971519, default 12589056): Using default value 12589056 Last sector, +sectors or +size{K,M,G} (12589056-20971519, default 20971519): +1G #划分1G大小的逻辑分区 Partition 6 of type Linux and of size 1 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: 0x189fc246 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux /dev/sdb2 4196352 8390655 2097152 83 Linux /dev/sdb3 8390656 10487807 1048576 83 Linux /dev/sdb4 10487808 20971519 5241856 5 Extended /dev/sdb5 10489856 12587007 1048576 83 Linux /dev/sdb6 12589056 14686207 1048576 83 Linux Command (m for help): d #删掉一个分区6 Partition number (1-6, default 6): 6 #回车默认是6 Partition 6 is deleted 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: 0x189fc246 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux /dev/sdb2 4196352 8390655 2097152 83 Linux /dev/sdb3 8390656 10487807 1048576 83 Linux /dev/sdb4 10487808 20971519 5241856 5 Extended /dev/sdb5 10489856 12587007 1048576 83 Linux Command (m for help): w #保存并退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. 查看刚划分出来的分区,此时已经成功划分。下一步就是格式化了。 [root@linux-xl ~]# fdisk -l /dev/sdb 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: 0x189fc246 Device Boot Start End Blocks Id System /dev/sdb1 2048 4196351 2097152 83 Linux /dev/sdb2 4196352 8390655 2097152 83 Linux /dev/sdb3 8390656 10487807 1048576 83 Linux /dev/sdb4 10487808 20971519 5241856 5 Extended /dev/sdb5 10489856 12587007 1048576 83 Linux 当我们在创建磁盘分区的时候,有三种分区可选,扩展分区(e),主分区(p),逻辑分区(l)。 我们最多只能创建4个分区(4个中不包括逻辑分区),如果想创建多个分区,可以是3个主分区和1个扩展分区组合。然后在扩展分区中,创建逻辑分区(可以创建无限个逻辑分区); 需要注意的是,扩展分区相当于一个空壳子,是不可以格式化的(不可以使用),只有它的子分区才可以格式化。在fdisk中划分分区的时候,使用d选项可以删除你想要删除的那个分区,需要注意的是,当扩展分区中有子分区(逻辑分区)的时候,删除扩展分区会连同子分区一起删除。