天天看点

Linux根目录空间100%该如何扩展

Linux的根分区如果已经使用率达到100%,导致服务不能正常进行,我们可以这样扩展根目录的大小(注意:下面所有操作需在root用户下完成)。

1. 新加一块硬盘(这里使用的是sde), 先创建物理卷

注意:扩展的分区必须要是linux lvm卷,是否是lvm卷是由磁盘第一次分区时候选择的分区格式决定的。

[root@bigdata105 ~]# fdisk /dev/sde

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x6133f941.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: The size of this disk is 3.0 TB (3000592982016 bytes).

DOS partition table format can not be used on drives for volumes

larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID

partition table format (GPT).

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): n (命令 n 用于添加新分区)

Command action

e extended

p primary partition (1-4)

p (选择创建主分区)

Partition number (1-4): 1

First cylinder (1-364801, default 1): (回车,默认将所有空间分配给第一个主分区)

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349):

Using default value 267349

Command (m for help): t(修改磁盘格式)

Selected partition 1(指定分区号)

Hex code (type L to list codes): 8e (8e代表LVM)

Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w (保存所有并退出)

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

2. 查看当前磁盘情况

[root@bigdata105 ~]# fdisk -l

Disk /dev/sde: 3000.6 GB, 3000592982016 bytes

255 heads, 63 sectors/track, 364801 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: 0x6133f941

Device Boot Start End Blocks Id System

/dev/sde1 1 267349 2147480811 8e Linux LVM

可以看到/dev/sde1已支持LVM。

3. 将新创建的主分区创建为物理卷

[root@bigdata105 ~]# pvcreate /dev/sde1

Physical volume "/dev/sde1" successfully created

4. df -h命令查看根目录所属的卷组和逻辑卷

[root@bigdata105 ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/vg_i8350r-lv_root 542G 318G 197G 62% /

tmpfs 127G 254M 126G 1% /dev/shm

/dev/sdd1 485M 40M 420M 9% /boot

/dev/mapper/vg_i8350r-lv_home 221G 58G 152G 28% /home

这里根目录所属的卷组是vg_i8350r,逻辑卷是/dev/mapper/vg_i8350r-lv_root

5. 将刚创建的物理卷/dev/sde1添加到卷组中

[root@bigdata105 ~]# vgextend vg_i8350r /dev/sde1

Found duplicate PV i35fwCm5JjA6P77IVdaXDDYQBg50o5hm: using /dev/sdd2 not /dev/sdc2

Volume group "vg_i8350r" successfully extended

6. 将卷组的200GB扩展给根所在的逻辑卷

[root@bigdata105 ~]# lvextend -L +200G /dev/mapper/vg_i8350r-lv_root

Extending logical volume lv_root to 750.00 GiB

Logical volume lv_root successfully resized

7. 激活修改的配置

[root@bigdata105 ~]# resize2fs /dev/mapper/vg_i8350r-lv_root

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/mapper/vg_i8350r-lv_root is mounted on /; on-line resizing required

old desc_blocks = 35, new_desc_blocks = 47

Performing an on-line resize of /dev/mapper/vg_i8350r-lv_root to 196608000 (4k) blocks.

The filesystem on /dev/mapper/vg_i8350r-lv_root is now 196608000 blocks long.

8. 查看扩展后系统情况

/dev/mapper/vg_i8350r-lv_root 739G 318G 383G 46% /

扩展前系统情况

根目录已经成功扩展200G空间。

9. 其他

如果扩展的根目录下装有其它服务,如数据库,则扩展操作完成后需要重启数据库。