有时候会遇到这样的情况:刚开始给VMWare分配了一个虚拟硬盘,安装Linux后,随着工作中使用得越来越多,虚拟硬盘容量逐渐变得不够用,需要扩充空间。下面说明如何在VMWare中增加Linux的文件系统空间。
我的VMWare中安装的是Fedora 9,不过其它Linux应该大同小异。
首先在VMWare中调整虚拟硬盘的大小(在“Virtual Machine Settings”的Hardware选项卡里选择“Hard Disk(SCSI)”,在右侧的Utilities里选择Expand进行扩展),也可以直接Add一块新的虚拟硬盘。
启动Linux,键入df -h查看磁盘信息:
-
[root@localhost
~]# df -h -
Filesystem
Size Used Avail Use% Mounted on -
/dev/mapper/VolGroup00-LogVol00
-
29G 26G 2.3G 92% / -
/dev/sda1
190M 13M 168M 7% /boot -
tmpfs
506M 48K 506M 1% /dev/shm -
gvfs-fuse-daemon
29G 26G 2.3G 92% /root/.gvfs
可以看到新增加的硬盘容量并没有体现在Linux中。使用fdisk -l查看分区表信息。我的信息是:
-
[root@localhost
~]# fdisk -l -
-
Disk
/dev/sda: 42.9 GB, 42949672960 bytes -
255
heads, 63 sectors/track, 5221 cylinders -
Units
= cylinders of 16065 * 512 = 8225280 bytes -
Disk
identifier: 0x000f1526 -
-
Device Boot Start End Blocks Id System -
/dev/sda1
* 1 25 200781 83 Linux -
/dev/sda2
26 3916 31254457+ 8e Linux LVM
注意Disk /Dev/sda有40多G空间(刚刚通过VMWare扩展的),但两个Device /dev/sda1和/dev/sda2加起来只有30多G,有10G空间没有被使用。
从调整分区表入手,调整分区表用fdisk。这个过程需要人机交互,我在需要输入的地方加注释来说明
-
[root@localhost
~]# fdisk /dev/sda -
-
The
number of cylinders for this disk is set to 5221. -
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 //选择n表示新建分区 -
Command
action -
e extended -
p primary partition (1-4) -
e
//选择e表示建立扩展分区 -
Partition
number (1-4): 3 //选择3是因为1和2都被占了(/dev/dsa1和/dev/dsa2) -
First
cylinder (3917-5221, default 3917): //此分区起始柱面,直接按回车,使用默认值 -
Using
default value 3917 -
Last
cylinder or +size or +sizeM or +sizeK (3917-5221, default 5221): //此分区大小,直接按回车,使用默认值(表明包含所有剩余空间) -
Using
default value 5221 -
-
Command
(m for help): n //在扩展分区基础上新建逻辑分区 -
Command
action -
l logical (5 or over) -
p primary partition (1-4) -
l
//选择l表示新建逻辑分区 -
First
cylinder (3917-5221, default 3917): //直接按回车 -
Using
default value 3917 -
Last
cylinder or +size or +sizeM or +sizeK (3917-5221, default 5221): //直接按回车 -
Using
default value 5221 -
-
Command
(m for help): p //选择p查看新的分区表,发现已经新建成功 -
-
Disk
/dev/sda: 42.9 GB, 42949672960 bytes -
255
heads, 63 sectors/track, 5221 cylinders -
Units
= cylinders of 16065 * 512 = 8225280 bytes -
Disk
identifier: 0x000f1526 -
-
Device Boot Start End Blocks Id System -
/dev/sda1
* 1 25 200781 83 Linux -
/dev/sda2
26 3916 31254457+ 8e Linux LVM -
/dev/sda3
3917 5221 10482412+ 5 Extended -
/dev/sda5
3917 5221 10482381 83 Linux -
-
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: Device or resource busy. -
The
kernel still uses the old table. -
The
new table will be used at the next reboot. -
Syncing
disks.
可以看到新增了/dev/sda3扩展分区和/dev/sda5逻辑分区。如果是新增了一块硬盘,也可以直接新建一个主分区,步骤更简单些。
键入partprobe命令,这个命令用于在硬盘分区发生改变时,更新Linux内核中读取的硬盘分区表数据。
-
[root@localhost
~]# partprobe
我们使用ext3格式化/dev/sda5这个新的分区
-
[root@localhost
~]# mkfs -t ext3 /dev/sda5 -
mke2fs
1.40.8 (13-Mar-2008) -
Warning:
256-byte inodes not usable on older systems -
Filesystem
label= -
OS
type: Linux -
Block
size=4096 (log=2) -
Fragment
size=4096 (log=2) -
655360
inodes, 2620595 blocks -
131029
blocks (5.00%) reserved for the super user -
First
data block=0 -
Maximum
filesystem blocks=2684354560 -
80
block groups -
32768
blocks per group, 32768 fragments per group -
8192
inodes per group -
Superblock
backups stored on blocks: -
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 -
-
Writing
inode tables: done -
Creating
journal (32768 blocks): done -
Writing
superblocks and filesystem accounting information: done -
-
This
filesystem will be automatically checked every 32 mounts or -
180
days, whichever comes first. Use tune2fs -c or -i to override.
格式化完毕后,要将新的分区加入到LVM(Logical
Volume Manager)以便以后管理。
我们先用pvdisplay命令查看LVM的物理卷信息
-
[root@localhost
~]# pvdisplay -
--- Physical volume --- -
PV Name /dev/sda2 -
VG Name VolGroup00 -
PV Size 29.81 GB / not usable 25.93 MB -
Allocatable yes -
PE Size (KByte) 32768 -
Total PE 953 -
Free PE 1 -
Allocated PE 952 -
PV UUID gjFDfY-J0vK-7YKm-uo32-eiQZ-YO3D-PFbp1A
可以看到,只有/dev/sda2加入了LVM的VolGroup00卷组。
使用我们新的/dev/sda5创建LVM物理卷,命令是pvcreate
-
[root@localhost
~]# pvcreate /dev/sda5 -
Physical volume "/dev/sda5" successfully created
创建成功,再键入pvdisplay查看
-
[root@localhost
~]# pvdisplay -
--- Physical volume --- -
PV Name /dev/sda2 -
VG Name VolGroup00 -
PV Size 29.81 GB / not usable 25.93 MB -
Allocatable yes -
PE Size (KByte) 32768 -
Total PE 953 -
Free PE 1 -
Allocated PE 952 -
PV UUID gjFDfY-J0vK-7YKm-uo32-eiQZ-YO3D-PFbp1A -
-
"/dev/sda5" is a new physical volume of "10.00 GB" -
--- NEW Physical volume --- -
PV Name /dev/sda5 -
VG Name -
PV Size 10.00 GB -
Allocatable NO -
PE Size (KByte) 0 -
Total PE 0 -
Free PE 0 -
Allocated PE 0 -
PV UUID WiG7f0-jGuq-HCUR-3MCZ-d8V0-rwV9-rEF2wg
新的物理卷已经创建,但是没有加入任何卷组。现在我们把它加入VolGroup00卷组
-
[root@localhost
~]# vgextend VolGroup00 /dev/sda5 -
Volume group "VolGroup00" successfully extended
加入成功。键入lvdisplay查看我们当前的逻辑卷信息
-
[root@localhost
~]# lvdisplay -
--- Logical volume --- -
LV Name /dev/VolGroup00/LogVol00 -
VG Name VolGroup00 -
LV UUID nvo0P1-8kmf-f9jJ-X4ii-RHUD-VvwK-AvIm36 -
LV Write Access read/write -
LV Status available -
# open 1 -
LV Size 29.25 GB -
Current LE 936 -
Segments 1 -
Allocation inherit -
Read ahead sectors auto -
- currently set to 256 -
Block device 253:0 -
-
--- Logical volume --- -
LV Name /dev/VolGroup00/LogVol01 -
VG Name VolGroup00 -
LV UUID 2hi7f1-SPdo-FQdX-s4gE-Y74F-rgzQ-KAO55X -
LV Write Access read/write -
LV Status available -
# open 1 -
LV Size 512.00 MB -
Current LE 16 -
Segments 1 -
Allocation inherit -
Read ahead sectors auto -
- currently set to 256 -
Block device 253:1
我们要扩展的是名为/dev/VolGroup00/LogVol00的逻辑卷,使用lvextend命令
-
[root@localhost
~]# lvextend /dev/VolGroup00/LogVol00 /dev/sda5 -
Extending logical volume LogVol01 to 10.47 GB -
Logical volume LogVol01 successfully resized
扩展成功,此时再键入lvdisplay查看信息,显示如下
-
[root@localhost
~]# lvdisplay -
--- Logical volume --- -
LV Name /dev/VolGroup00/LogVol00 -
VG Name VolGroup00 -
LV UUID nvo0P1-8kmf-f9jJ-X4ii-RHUD-VvwK-AvIm36 -
LV Write Access read/write -
LV Status available -
# open 1 -
LV Size 39.22 GB -
Current LE 1255 -
Segments 2 -
Allocation inherit -
Read ahead sectors auto -
- currently set to 256 -
Block device 253:0 -
-
--- Logical volume --- -
LV Name /dev/VolGroup00/LogVol01 -
VG Name VolGroup00 -
LV UUID 2hi7f1-SPdo-FQdX-s4gE-Y74F-rgzQ-KAO55X -
LV Write Access read/write -
LV Status available -
# open 1 -
LV Size 32.00 MB -
Current LE 1 -
Segments 1 -
Allocation inherit -
Read ahead sectors auto -
- currently set to 256 -
Block device 253:1
LogVol00扩大了10G。但此时键入df -h,会发现我们的空间还是不能用
-
[root@localhost
~]# df -h -
Filesystem
Size Used Avail Use% Mounted on -
/dev/mapper/VolGroup00-LogVol00
-
29G 26G 2.3G 92% / -
/dev/sda1
190M 13M 168M 7% /boot -
tmpfs
506M 48K 506M 1% /dev/shm -
gvfs-fuse-daemon
29G 26G 2.3G 92% /root/.gvfs
需要用resize2fs工具调整Linux文件系统大小。如果你的Linux内核是2.6及以上版本,可以直接调整,否则需要先umount相关 的设备后进行调整。Fedora 9的内核版本是2.6.25。OK,我们直接调整df命令中列出的/dev/mapper/VolGroup00-LogVol00文件系统
-
[root@localhost
~]# resize2fs /dev/mapper/VolGroup00-LogVol00 -
resize2fs
1.40.8 (13-Mar-2008) -
Filesystem
at /dev/mapper/VolGroup00-LogVol00 is mounted on /; on-line resizing required -
old
desc_blocks = 2, new_desc_blocks = 3 -
Performing
an on-line resize of /dev/mapper/VolGroup00-LogVol00 to 10280960 (4k) blocks. -
The
filesystem on /dev/mapper/VolGroup00-LogVol00 is now 10280960 blocks long.
此时再键入df -h命令查看
-
[root@localhost
~]# df -h -
Filesystem
Size Used Avail Use% Mounted on -
/dev/mapper/VolGroup00-LogVol00
-
39G 26G 12G 69% / -
/dev/sda1
190M 13M 168M 7% /boot -
tmpfs
506M 48K 506M 1% /dev/shm -
gvfs-fuse-daemon
39G 26G 12G 69% /root/.gvfs
至此,我们的文件系统扩充完毕。