一、目的
在使用CentOS6.3版本linux系统的时候,发现根目录(/)的空间不是很充足,而其他目录空间有很大的空闲,所以本文主要是针对现在已有的空间进行调整。首先,先来查看一下系统的空间分配情况:
[root@CentOS-78 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
50G 14G 34G 30% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 37M 423M 8% /boot
/dev/mapper/vg_centos-lv_home
404G 670M 382G 1% /home
下面的详细步骤部分将从vg_centos-lv_home分区下取出100G的空间添加到/vg_centos-lv_root分区上去。
二、详细步骤
1、卸载vg_centos-lv_home分区
[root@CentOS-78 /]# umount /home
此时在用df命令查看将会发现/home目录已经看不见了,如下所示:
[root@CentOS-78 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
50G 14G 34G 30% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 37M 423M 8% /boot
2、从新设定vg_home-lv_home的大小
[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G
resize2fs 1.41.12 (17-May-2010)
Please run ‘e2fsck -f /dev/mapper/vg_centos-lv_home‘ first.
这一步设定vg_home-lv_home大小没有成功,系统提示我们先运行下面的命令,操作如下:
[root@CentOS-78 /]# e2fsck -f /dev/mapper/vg_centos-lv_home
e2fsck 1.41.12 (17-May-2010)
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/mapper/vg_centos-lv_home: 1386/26836992 files (0.9% non-contiguous), 1855856/107344896 blocks
从新设定vg_home-lv_home的大小:
[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_home 282G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/vg_centos-lv_home to 73924608 (4k) blocks.
Begin pass 2 (max = 43)
Relocating blocks XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 3276)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 266)
Updating inode references XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/vg_centos-lv_home is now 73924608 blocks long.
检查是否成功:
[root@CentOS-78 /]# mount /home
[root@CentOS-78 /]#
[root@CentOS-78 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
50G 14G 34G 30% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 37M 423M 8% /boot
/dev/mapper/vg_centos-lv_home
278G 663M 263G 1% /home
[root@CentOS-78 /]#
3、将lv_home逻辑分区减小到指定大小
[root@CentOS-78 /]# lvreduce -L 282G /dev/mapper/vg_centos-lv_home
WARNING: Reducing active and open logical volume to 282.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
Reducing logical volume lv_home to 282.00 GiB
Logical volume lv_home successfully resized
[root@CentOS-78 /]#
我认为这个命令是将已经减少的100G空间拿到系统公共的区域,可以被其他分区加载和利用。
查询卷组的信息:
[root@CentOS-78 /]# vgdisplay
--- Volume group ---
VG Name vg_centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 465.27 GiB
PE Size 4.00 MiB
Total PE 119109
Alloc PE / Size 86472 / 337.78 GiB
Free PE / Size 32637 / 127.49 GiB
VG UUID 1k4ooN-RFV9-uyf1-uMYf-aERG-YaGs-ZNoSD6
Free PE / Size指定的应该是现在可在分配的空间。
4、增加vg_centos-lv_root分区的大小
将可用的空间添加到vg_centos-lv_root分区上面:
[root@CentOS-78 /]# lvextend -L +127.40G /dev/mapper/vg_centos-lv_root
Rounding up size to full physical extent 127.40 GiB
Extending logical volume lv_root to 177.40 GiB
Logical volume lv_root successfully resized
[root@CentOS-78 /]#
从新设定该分区的大小:
[root@CentOS-78 /]# resize2fs -p /dev/mapper/vg_centos-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_centos-lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 12
Performing an on-line resize of /dev/mapper/vg_centos-lv_root to 46504960 (4k) blocks.
The filesystem on /dev/mapper/vg_centos-lv_root is now 46504960 blocks long.
5、再次查看分区大小
[root@CentOS-78 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos-lv_root
175G 14G 153G 9% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 37M 423M 8% /boot
/dev/mapper/vg_centos-lv_home
278G 663M 263G 1% /home
我们发现vg_centos-lv_root分区的空间已经增加了125G,之所以比lv_home减少的空间要多25G主要是由于我们把系统所有的可用的空间都加在了lv_root分区。
三、所遇到的问题
1、在卸载/home目录的时候失败
可先执行如下fuser命令,然后再umount即可:
[root@CentOS-78 /]# fuser -m /home
[root@CentOS-78 /]#
2、设定完lv_home的大小,再次mount该分区时,发现用df命令无法看到给分区,此时只要在mount一次即可
3、在设定lv_root的大小时,不要把Free PE / Size的空间全部都用上,这很可能会出现Free PE空间不足的现象,建议保留一点Free PE的空间。
在Fedora13-x86_64 扩展 VolGroup-lv_root 分区.
在使用Fedora13-x86_64过程中,发现系统分配给root用户lvm份额太少.
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
50G 11G 37G 23% /
tmpfs 878M 100K 878M 1% /dev/shm
/dev/sda1 485M 28M 432M 7% /boot
/dev/mapper/VolGroup-lv_home
406G 411M 385G 1% /home
/dev/sdc 124M 107M 18M 86% /media/16F0-286F
[root@localhost ~]#
总共才50G,决定给它增加份额,现做好重要文件备份,以防万一.
思路:
先减掉VolGroup-lv_home的份额,再把减掉份额增加到VolGroup-lv_root.
[root@localhost ~]# pvscan
PV /dev/sda2 VG VolGroup lvm2 [465.25 GiB / 0 free]
Total: 1 [465.25 GiB] / in use: 1 [465.25 GiB] / in no VG: 0 [0 ]
[root@localhost ~]# resize2fs -p /dev/mapper/VolGroup-lv_home 206G
resize2fs 1.41.10 (10-Feb-2009)
Filesystem at /dev/mapper/VolGroup-lv_home is mounted on /home; on-line resizing required
On-line shrinking from 107954176 to 54001664 not supported.
[root@localhost ~]# umount /home
[root@localhost ~]# resize2fs -p /dev/mapper/VolGroup-lv_home 206G
resize2fs 1.41.10 (10-Feb-2009)
Please run ‘e2fsck -f /dev/mapper/VolGroup-lv_home‘ first.
[root@localhost ~]# e2fsck -f /dev/mapper/VolGroup-lv_home
e2fsck 1.41.10 (10-Feb-2009)
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/mapper/VolGroup-lv_home: 1534/26992640 files (1.2% non-contiguous), 1799101/107954176 blocks
[root@localhost ~]# resize2fs -p /dev/mapper/VolGroup-lv_home 206G
resize2fs 1.41.10 (10-Feb-2009)
Resizing the filesystem on /dev/mapper/VolGroup-lv_home to 54001664 (4k) blocks.
Begin pass 3 (max = 3295)
Scanning inode table XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/VolGroup-lv_home is now 54001664 blocks long.
[root@localhost ~]# mount /home
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
50G 11G 37G 23% /
tmpfs 878M 100K 878M 1% /dev/shm
/dev/sda1 485M 28M 432M 7% /boot
/dev/sdc 124M 107M 18M 86% /media/16F0-286F
/dev/mapper/VolGroup-lv_home
203G 400M 193G 1% /home
[root@localhost ~]# lvreduce -L 206G /dev/mapper/VolGroup-lv_home
WARNING: Reducing active and open logical volume to 206.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_home? [y/n]: y
Reducing logical volume lv_home to 206.00 GiB
Logical volume lv_home successfully resized
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
50G 11G 37G 23% /
tmpfs 878M 100K 878M 1% /dev/shm
/dev/sda1 485M 28M 432M 7% /boot
/dev/sdc 124M 107M 18M 86% /media/16F0-286F
/dev/mapper/VolGroup-lv_home
203G 400M 193G 1% /home
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 5
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 465.25 GiB
PE Size 32.00 MiB
Total PE 14888
Alloc PE / Size 8302 / 259.44 GiB
Free PE / Size 6586 / 205.81 GiB
VG UUID ymBJK0-SNdq-DWHg-MlPu-31sG-ZpO1-kCrUmu
[root@localhost ~]# man lvextend
[root@localhost ~]# lvextend -L +205.81G /dev/mapper/VolGroup-lv_root
Rounding up size to full physical extent 205.81 GiB
Extending logical volume lv_root to 255.81 GiB
Logical volume lv_root successfully resized
[root@localhost ~]# resize2fs -p /dev/mapper/VolGroup-lv_root
resize2fs 1.41.10 (10-Feb-2009)
Filesystem at /dev/mapper/VolGroup-lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 16
Performing an on-line resize of /dev/mapper/VolGroup-lv_root to 67059712 (4k) blocks.
The filesystem on /dev/mapper/VolGroup-lv_root is now 67059712 blocks long.
让我们看一下,
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
252G 11G 229G 5% /
tmpfs 878M 100K 878M 1% /dev/shm
/dev/sda1 485M 28M 432M 7% /boot
/dev/sdc 124M 107M 18M 86% /media/16F0-286F
/dev/mapper/VolGroup-lv_home
203G 400M 193G 1% /home
[root@localhost ~]#
已经可以了.
linux增加根目录的磁盘空间(LVM)
第一步:添加磁盘
第二步:使用Linux下的fdisk工具进行分区
用root用户登录:
打印当前的磁盘分区表,这时我们可以看到磁盘的总量的确增加到30GB 了,但是分区只有以前的那几个原有的分区
[root@localhost ~]# fdisk -l
键入命令: fdisk /dev/sda “sda 就是经过扩容的硬盘,为 SCSI 硬盘, IDE 类型硬盘对应为 hda ,是对该硬盘进行操作 ”
选择主分区吧,则键入 p
此时, fdisk 会让你选择主分区的编号,如果已经有了主分区 sda1 , sda2 ,那么编号就选3 ,即要创建的该分区为 sda3.
键入: 3
此时, fdisk 又会让你选择该分区的开始值这个就是分区的 Start 值( start cylinder );这里最好直接按回车, 如果您输入了一个非默认的数字,可能会造成空间浪费;
对于分区的 End 值(end cylinder),同样直接按回车。这时候会显示出你新建分区的柱面范围和空间大小。
此时键入: w 表示" 保存所有并退出,分区划分完毕 "
[root@localhost ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 3916.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1045-3916, default 1045):
Using default value 1045
Last cylinder or +size or +sizeM or +sizeK (1045-3916, default 3916):
Using default value 3916
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.
Syncing disks.
我们的新建分区/dev/sda3,却不是LVM的。所以,接下来使用fdisk将其改成LVM的。
[root@CNGI-SIP6-BUPT ~]# fdisk /dev/sda
Command (m for help): m
Command (m for help): t //改变分区系统id
Partition number (1-4): 3 //指定分区号
Hex code (type L to list codes): 8e //指定要改成的id号,8e代表LVM。
Command (m for help): w
[root@localhost ~]# fdisk /dev/sda
The number of cylinders for this disk is set to 3916.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition‘s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)
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.
Syncing disks.
重新登录后:
[root@localhost ~]# reboot
Broadcast message from root (pts/1) (Thu Sep 22 09:54:45 2011):
The system is going down for reboot NOW!
[root@localhost ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 1044 8281507+ 8e Linux LVM
/dev/sda3 1045 3916 23069340 8e Linux LVM
Disk /dev/sdb: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 783 6289416 83 Linux
第三步、格式化该新添加的分区
格式化磁盘
[root@localhost ~]# mkfs -t ext3 /dev/sda3
mke2fs 1.35 (28-Feb-2004)
max_blocks 4294967295, rsv_groups = 0, rsv_gdb = 1024
max_blocks 4294967295, rsv_groups = 0, rsv_gdb = 1024
warning: 167 blocks unused.
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2889216 inodes, 5767168 blocks
288366 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=8388608
176 block groups
32768 blocks per group, 32768 fragments per group
16416 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Writing inode tables: done
inode.i_blocks = 90120, i_size = 4243456
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
第四步、扩充根分区
[root@localhost ~]# lvs
LV VG Attr LSize Origin Snap% Move Copy%
LogVol00 VolGroup00 -wi-ao 5.91G
LogVol01 VolGroup00 -wi-ao 1.94G
[root@localhost ~]# pvcreate /dev/sd
sda sda1 sda2 sda3 sdb sdb1
[root@localhost ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
[root@localhost ~]# df -h
Filesystem 容量 已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
5.9G 5.5G 120M 98% /
/dev/sda1 99M 9.0M 85M 10% /boot
none 506M 0 506M 0% /dev/shm
/dev/sdb1 6.0G 347M 5.3G 7% /disk2
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 7.88 GB
PE Size 32.00 MB
Total PE 252
Alloc PE / Size 251 / 7.84 GB
Free PE / Size 1 / 32.00 MB
VG UUID HvKxmm-WazL-TjFN-yXNH-ui8U-pcl9-Pk6psB
[root@localhost ~]# vgextend VolGroup00 /dev/sda3
Volume group "VolGroup00" successfully extended
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 29.88 GB
PE Size 32.00 MB
Total PE 956
Alloc PE / Size 251 / 7.84 GB
Free PE / Size 705 / 22.03 GB
VG UUID HvKxmm-WazL-TjFN-yXNH-ui8U-pcl9-Pk6psB
#不能把 Free PE / Size 705 / 22.03 GB的空间全部加上,应该下雨22.03
[root@localhost ~]# lvextend -L+22.03G /dev/VolGroup00/LogVol00 /dev/sda3
Rounding up size to full physical extent 22.03 GB
Extending logical volume LogVol00 to 27.94 GB
Insufficient allocatable logical extents (893) for logical volume LogVol00: 894 required
[root@localhost ~]# lvextend -L+21.03G /dev/VolGroup00/LogVol00 /dev/sda3
Rounding up size to full physical extent 21.03 GB
Extending logical volume LogVol00 to 26.94 GB
Logical volume LogVol00 successfully resized
[root@localhost ~]# cat /etc/fstab
# This file is edited by fstab-sync - see ‘man fstab-sync‘ for details
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/sdb1 /disk2 ext3 defaults 1 1
/dev/hdc /media/cdrom auto pamconsole,exec,noauto,managed 0 0
/dev/fd0 /media/floppy auto pamconsole,exec,noauto,managed 0 0
[root@localhost ~]# e2fsck -a /dev/VolGroup00/LogVol00
/dev/VolGroup00/LogVol00 is mounted.
WARNING!!! Running e2fsck on a mounted filesystem may cause
SEVERE filesystem damage.
Do you really want to continue (y/n)? yes
/dev/VolGroup00/LogVol00: recovering journal
/dev/VolGroup00/LogVol00: Clearing orphaned inode 596983 (uid=501, gid=501, mode=0100660, size=0)
/dev/VolGroup00/LogVol00: clean, 144094/774144 files, 1440278/1548288 blocks
#redhat4 没有resize2fs命令
[root@localhost ~]# resize2fs
-bash: resize2fs: command not found
#redhat4下用ext2online
[root@localhost ~]# ext2online /dev/VolGroup00/LogVol00
ext2online v1.1.18 - 2001/03/18 for EXT2FS 0.5b
[root@localhost ~]# df -h
Filesystem 容量 已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
27G 5.5G 20G 22% /
/dev/sda1 99M 9.0M 85M 10% /boot
none 506M 0 506M 0% /dev/shm
/dev/sdb1 6.0G 347M 5.3G 7% /disk2