RHEL5.4在线更改磁盘分区大小

这里所说的磁盘分区指的是非系统分区、非LVM分区,下面将进行缩容和扩容

 

当前分区结构:

[root@vm3 ~]# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 1239 9952236 83 Linux
/dev/sda2 1240 1304 522112+ 82 Linux swap / Solaris

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 1305 10482381 83 Linux

文件系统使用率

[root@vm3 ~]# df
文件系统 1K-块 已用可用已用%挂载点
/dev/sda1 9640260 5056192 408646056%/
tmpfs 383616 03836160%/dev/shm
/dev/sdb1 10317828 2182424792576022%/data

 

一、缩容

现在想缩小sdb1的容量,看到sdb1的可用空间为7925760K,我们分2G出来

 

1、卸载已经挂载的分区

[root@vm3 ~]# umount /data
umount: /data: device is busy
umount: /data: device is busy
[root@vm3 ~]# fuser -vm /data

USER PID ACCESS COMMAND
/data: mysql 5522 F.c.. mysqld

[root@vm3 ~]# kill 5522
[root@vm3 ~]# umount /data

2、作文件系统检测

[root@vm3 ~]# e2fsck -f /dev/sdb1
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 197/1310720 files (22.3% non-contiguous), 586743/2620595 blocks

3、调整文件系统大小

[root@vm3 ~]# resize2fs /dev/sdb1 8317828k
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/sdb1 to 2079457 (4k) blocks.
The filesystem on /dev/sdb1 is now 2079457 blocks long.

这里设置的大小是在原来总大小的基础上减掉了2000k,即10317828-2000000=8317828

4、调整分区大小

如何确定分区的大小呢?看上面红色的字,说明已经重设文件系统大小为2079457 (4k) blocks,即2079457*4k=8317828k

[root@vm3 ~]# fdisk /dev/sdb <<EOF
> d
> n
> p
> 1
>
> +8317828
K
> w
> EOF

注意,这里是大写K,1k=1000bytes,1K=1024bytes

5、再运行文件系统检测

[root@vm3 ~]# e2fsck -f /dev/sdb1
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 197/1048576 files (22.3% non-contiguous), 578519/2079457 blocks

ok,没问题,文件系统检测通过。如果前面用小写k,就会出错,如下:

[root@vm3 ~]# e2fsck -f /dev/sdb1
e2fsck 1.39 (29-May-2006)
The
filesystem size (according to the superblock) is 2079457 blocks
The physical size of the device is 2032214 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>? yes

意思是说分区大小小于文件系统大小

6、挂载分区

[root@vm3 ~]# mount /dev/sdb1 /data
[root@vm3 ~]# ls /data
db1 db2 log lost+found mongodb mysql

ok,数据还在,调整成功!

7、为剩余空间创建分区

[root@vm3 ~]# fdisk /dev/sdb <<EOF
> n
> p
> 2
>
>
> w
> EOF

8、格式化分区、挂载

[root@vm3 ~]# mkfs.ext3 /dev/sdb2
[root@vm3 ~]# mount /dev/sdb2 /mnt
[root@vm3 ~]# ls /mnt
lost+found

9、看文件系统使用率

[root@vm3 ~]# df
文件系统 1K-块已用可用已用%挂载点
/dev/sda1 96402605055024408762856%/
tmpfs 38361603836160%/dev/shm
/dev/sdb1 8186172 2182420 600375227%/data
/dev/sdb2 21188646877619424564%/mnt

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/20205.html