centos 磁盘操作(新建,删除)

Author Avatar
doubledna 3 月 25 日

一. 新建磁盘分区
1.[root@localhost ~]# fdisk -l 查看磁盘分区

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x00005cb3

Device Boot Start End Blocks Id System
/dev/sda1 2048 6143 2048 83 Linux
/dev/sda2 * 6144 2103295 1048576 83 Linux
/dev/sda3 2103296 37525503 17711104 8e Linux LVM

Disk /dev/mapper/centos-root: 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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 /dev/mapper/centos-home: 5242 MB, 5242880000 bytes, 10240000 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
可以看出 /dev/sda 磁盘有剩余空间。

创建磁盘分区
2.[root@localhost ~]# fdisk /dev/sda 使用:fdisk 磁盘文件
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): n 新建分区
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e 新建拓展分区
Selected partition 4
First sector (37525504-41943039, default 37525504): 直接选择默认起始柱面
Using default value 37525504
Last sector, +sectors or +size{K,M,G} (37525504-41943039, default 41943039): 直接选择默认结束柱面
Using default value 41943039
Partition 4 of type Extended and of size 2.1 GiB is set

Command (m for help): w 保存结果
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.
[root@localhost ~]#

3. 创建逻辑分区:
[root@localhost ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x00005cb3

Device Boot Start End Blocks Id System
/dev/sda1 2048 6143 2048 83 Linux
/dev/sda2 * 6144 2103295 1048576 83 Linux
/dev/sda3 2103296 37525503 17711104 8e Linux LVM
/dev/sda4 37525504 41943039 2208768 5 Extended
可以发现 sda4 的文件系统类型是扩展分区, 扩展分区是无法使用的, 必须在扩展分区的基础上创建逻辑分区
输入 n 创建逻辑分区, 因为这里只剩下刚才创建的逻辑分区需要创建, 所以不需要选择输入开始和结束柱面即可

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x00005cb3

Device Boot Start End Blocks Id System
/dev/sda1 2048 6143 2048 83 Linux
/dev/sda2 * 6144 2103295 1048576 83 Linux
/dev/sda3 2103296 37525503 17711104 8e Linux LVM
/dev/sda4 37525504 41943039 2208768 5 Extended

Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (37527552-41943039, default 37527552):
Using default value 37527552
Last sector, +sectors or +size{K,M,G} (37527552-41943039, default 41943039):
Using default value 41943039
Partition 5 of type Linux and of size 2.1 GiB is set

Command (m for help): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x00005cb3

Device Boot Start End Blocks Id System
/dev/sda1 2048 6143 2048 83 Linux
/dev/sda2 * 6144 2103295 1048576 83 Linux
/dev/sda3 2103296 37525503 17711104 8e Linux LVM
/dev/sda4 37525504 41943039 2208768 5 Extended
/dev/sda5 37527552 41943039 2207744 83 Linux 可以看到创建了 sda5 这个逻辑分区

Command (m for help): w
The partition table has been altered!
4 格式化逻辑分区
如果是使用虚拟机, 需要重启才可以对分区进行格式化, 否则会提示找不到分区文件。
mkfs-t ext4 /dev/sda5
5. 挂载分区
先创建分区挂载的文件夹
mkdir /newpar
将 sda5 挂载到 /newpar 文件下
mount /dev/sda5 /newpar
6.# df -Ph 查看分区情况

二,删除分区
[root@localhost ~]# df -Ph
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 10G 979M 9.1G 10% /
devtmpfs 484M 0 484M 0% /dev
tmpfs 495M 0 495M 0% /dev/shm
tmpfs 495M 6.9M 488M 2% /run
tmpfs 495M 0 495M 0% /sys/fs/cgroup
/dev/sda2 1014M 125M 890M 13% /boot
/dev/mapper/centos-home 4.9G 33M 4.9G 1% /home
tmpfs 99M 0 99M 0% /run/user/0
/dev/sda5 2.1G 6.4M 1.9G 1% /newpar
[root@localhost ~]# umount /newpar 卸载挂载点
[root@localhost ~]# df -Ph
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 10G 979M 9.1G 10% /
devtmpfs 484M 0 484M 0% /dev
tmpfs 495M 0 495M 0% /dev/shm
tmpfs 495M 6.9M 488M 2% /run
tmpfs 495M 0 495M 0% /sys/fs/cgroup
/dev/sda2 1014M 125M 890M 13% /boot
/dev/mapper/centos-home 4.9G 33M 4.9G 1% /home
tmpfs 99M 0 99M 0% /run/user/0
[root@localhost ~]# fdisk /dev/sda
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): p

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x00005cb3

Device Boot Start End Blocks Id System
/dev/sda1 2048 6143 2048 83 Linux
/dev/sda2 * 6144 2103295 1048576 83 Linux
/dev/sda3 2103296 37525503 17711104 8e Linux LVM
/dev/sda4 37525504 41943039 2208768 5 Extended
/dev/sda5 37527552 41943039 2207744 83 Linux

Command (m for help): d 删除逻辑分区 /dev/sda5
Partition number (1-5, default 5): 5
Partition 5 is deleted

Command (m for help): p

Device Boot Start End Blocks Id System
/dev/sda1 2048 6143 2048 83 Linux
/dev/sda2 * 6144 2103295 1048576 83 Linux
/dev/sda3 2103296 37525503 17711104 8e Linux LVM
/dev/sda4 37525504 41943039 2208768 5 Extended

Command (m for help): d 删除拓展分区 /dev/sda4
Partition number (1-4, default 4): 4
Partition 4 is deleted

Command (m for help): w 保存操作

三 给根目录扩容: