CenterOS磁盘分区调整,删除,合并分区

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CenterOS磁盘分区调整,删除,合并分区相关的知识,希望对你有一定的参考价值。

目的:将/u2和/u3分区删除后,合并为/u4分区。
技术分享图片

1、先取消/u2和/u3分区的挂载
[[email protected] ~]# umount /u2
[[email protected] ~]# umount /u3
[[email protected] ~]# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda5 22G 2.0G 19G 10% /
tmpfs 911M 0 911M 0% /dev/shm
/dev/sda1 194M 32M 153M 18% /boot
/dev/sda3 2.0G 35M 1.8G 2% /u1

2、进入fdisk /dev/sda磁盘删除分区格式
[[email protected] ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
switch off the mode (command ‘c‘) and change display units to
sectors (command ‘u‘).

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007403b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 281 2048000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 281 536 2048000 83 Linux
Partition 3 does not end on cylinder boundary.
/dev/sda4 536 3917 27155456 5 Extended
/dev/sda5 1046 3917 23056384 83 Linux
/dev/sda6 536 736 1610064+ 83 Linux
/dev/sda7 737 1045 2482011 83 Linux #这两个虽然取消挂载,但是分区要进一步删除。

Partition table entries are not in disk order

Command (m for help): d
Partition number (1-7): 7 #这个标号是要删除的磁盘编号

Command (m for help): p #建议删一次分区,用一次p选项,避免删错

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007403b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 281 2048000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 281 536 2048000 83 Linux
Partition 3 does not end on cylinder boundary.
/dev/sda4 536 3917 27155456 5 Extended
/dev/sda5 1046 3917 23056384 83 Linux
/dev/sda6 536 736 1610064+ 83 Linux

Partition table entries are not in disk order

Command (m for help): d
Partition number (1-6): 6 #这个标号是要删除的磁盘编号

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007403b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 281 2048000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 281 536 2048000 83 Linux
Partition 3 does not end on cylinder boundary.
/dev/sda4 536 3917 27155456 5 Extended
/dev/sda5 1046 3917 23056384 83 Linux
#/dev/sda6和/dev/sda7被彻底删除
Command (m for help): w #之前删除分区后,要记得用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)
Syncing disks.
[[email protected] ~]#

3、进入/etc/fstab将/u2和/u3的启动项挂载删除掉,避免启动的时候挂载失败,造成系统无法正常启动。

[[email protected] ~]# cat /etc/fstab
#

/etc/fstab

Created by anaconda on Mon Jan 29 15:17:42 2018

#

Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#
UUID=ec79fc9b-37a3-4569-a6d2-e4f3ddbf9564 / ext4 defaults 1 1
UUID=ddfde3c8-d179-4773-b506-9b8d37c869b8 /boot ext4 defaults 1 2
UUID=2f8686bb-d6e9-4b9d-846a-4459d5359b28 /u1 ext4 defaults 1 2
#UUID=e9f9034f-3d55-4c71-b7fc-ac6811bfc5bc /u2 ext4 defaults 1 2
#UUID=7d32325e-b21c-42b6-9766-20600527113f /u3 ext4 defaults 1 2

UUID=f84bdd88-71b8-4143-aec4-a9ceae8d8a0a swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0

4、出现以上的“WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.”提示是无关紧要的,直接重启服务器就可以正式生效了。
[[email protected] ~]# reboot

Broadcast message from [email protected]
(/dev/pts/0) at 9:17 ...
The system is going down for reboot NOW!

5、重启完成后建立/u4目录
[[email protected] ~]# mkdir -p /u4

6、将之前的/u2和/u3的清空下来的空间用fdisk建立新的分区。
[[email protected] ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
switch off the mode (command ‘c‘) and change display units to
sectors (command ‘u‘).

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007403b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 281 2048000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 281 536 2048000 83 Linux
Partition 3 does not end on cylinder boundary.
/dev/sda4 536 3917 27155456 5 Extended
/dev/sda5 1046 3917 23056384 83 Linux
#可以看出当前分区只到/dev/sda5
Command (m for help): n #新建分区
First cylinder (536-3917, default 536): #由于要合并,直接回车即可,起始为默认
Using default value 536
Last cylinder, +cylinders or +size{K,M,G} (536-1045, default 1045): #由于要合并,直接回车即可,结束位为默认
Using default value 1045

Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0007403b

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 281 2048000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 281 536 2048000 83 Linux
Partition 3 does not end on cylinder boundary.
/dev/sda4 536 3917 27155456 5 Extended
/dev/sda5 1046 3917 23056384 83 Linux
/dev/sda6 536 1045 4092107 83 Linux #可以看到/dev/sda6为新建分区

Partition table entries are not in disk order

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: 设备或资源忙.
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.
#以上这步骤完成后,建议重启不然无法格式化/dev/sda6因为识别不到。

7、格式化新建的/dev/sda6分区
[[email protected] ~]# mkfs.ext4 /dev/sda6
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
256000 inodes, 1023026 blocks
51151 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1048576000
32 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736

正在写入inode表: 完成
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

7、将/dev/sda6分区挂载到u4分区上
[[email protected] ~]# mount /dev/sda6 /u4
[[email protected] ~]# df -h
文件系统 容量 已用 可用 已用%% 挂载点
/dev/sda5 22G 2.0G 19G 10% /
tmpfs 911M 0 911M 0% /dev/shm
/dev/sda1 194M 32M 153M 18% /boot
/dev/sda3 2.0G 35M 1.8G 2% /u1
/dev/sda6 3.9G 72M 3.6G 2% /u4

8、将/u4分区写入到/etc/fstab启动项里
[[email protected] ~]# cd /dev/disk/by-uuid/ #先找出/dev/sda6对应的uuid标示号
[[email protected] by-uuid]# ll
总用量 0
lrwxrwxrwx. 1 root root 10 1月 30 09:42 2f8686bb-d6e9-4b9d-846a-4459d5359b28 -> ../../sda3
lrwxrwxrwx. 1 root root 10 1月 30 09:46 54013a0e-e30a-4e06-b651-e77dd3d0704d -> ../../sda6
lrwxrwxrwx. 1 root root 10 1月 30 09:42 ddfde3c8-d179-4773-b506-9b8d37c869b8 -> ../../sda1
lrwxrwxrwx. 1 root root 10 1月 30 09:42 ec79fc9b-37a3-4569-a6d2-e4f3ddbf9564 -> ../../sda5
lrwxrwxrwx. 1 root root 10 1月 30 2018 f84bdd88-71b8-4143-aec4-a9ceae8d8a0a -> ../../sda2

[[email protected] by-uuid]# vi /etc/fstab
#

/etc/fstab

Created by anaconda on Mon Jan 29 15:17:42 2018

#

Accessible filesystems, by reference, are maintained under ‘/dev/disk‘

See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#
UUID=ec79fc9b-37a3-4569-a6d2-e4f3ddbf9564 / ext4 defaults 1 1
UUID=ddfde3c8-d179-4773-b506-9b8d37c869b8 /boot ext4 defaults 1 2
UUID=2f8686bb-d6e9-4b9d-846a-4459d5359b28 /u1 ext4 defaults 1 2
#UUID=e9f9034f-3d55-4c71-b7fc-ac6811bfc5bc /u2 ext4 defaults 1 2
#UUID=7d32325e-b21c-42b6-9766-20600527113f /u3 ext4 defaults 1 2
UUID=54013a0e-e30a-4e06-b651-e77dd3d0704d /u4 ext4 defaults 1 2
#新增这个条目,UUID抄cd /dev/disk/by-uuid/目录里的。

UUID=f84bdd88-71b8-4143-aec4-a9ceae8d8a0a swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0

至此完成了所有的步骤,重启后也不怕丢失了。

以上是关于CenterOS磁盘分区调整,删除,合并分区的主要内容,如果未能解决你的问题,请参考以下文章

如何重新分配磁盘空间?

怎么修改硬盘分区大小

mac磁盘工具无法调整分区大小也不能增加新分区,该怎么办?

如何调整磁盘大小

aix的磁盘管理

linux 如何合并2个分区