VMware虚拟机中调整Linux分区大小

root@localhost ~]# fdisk -l
                                                                               
Disk /dev/sda: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
                                                                               
   Device Boot    Start       End    Blocks   Id System
/dev/sda1   *         1        25    200781   83 Linux
/dev/sda2            26       620   4779337+ 83 Linux
/dev/sda3           621       652    257040   82 Linux swap
sda1挂载/boot 分区 200 M,sda2挂载 / 分区 剩余所有,sda3 为swap分区 100M

[root@localhost ~]# df -l

文件系统    1K-块        已用         可用           已用%   挂载点

/dev/sda2              4704236    4374939    329297        93%        /
/dev/sda1              194443      9377        175027        6%          /boot
none                     95336        0              95336          0%         /dev/shm

         如果重装系统,可以重新划分虚拟硬盘空间和分区,但系统很多东西要重新设置,有些软件也要重新安装和设置,比较麻烦。不选择重装,于是在网上搜索资料,看能不能扩充虚拟硬盘容量并调整Linux分区大小,最好是不改变现有分区结构,只将扩充的虚拟硬盘空间加大到sda2分区上。

          首先是扩充虚拟硬盘容量,这个问题找到了解决方法。在VMware中有一个工具,专门用来管理虚拟硬盘,不过得在命令行中运行。在VMware安装目录下有这个程序vmware-vdiskmanager.exe 。win+R 打开cmd窗口 cd d:/vmware (我的VMware安装在 D:/vmware目录中) 运行vmware-vdiskmanager 出来以下界面

 

 

下面是命令帮助信息。可以看到如果要扩充虚拟硬盘空间,可以用参数 -x ( -x <new-capacity>    : expand the disk to the specified capacity) 它也有使用举例 : ex 4: vmware-vdiskmanager.exe -x 36Gb myDisk.vmdk 意思是将 myDisk.vmdk (虚拟机存放目录中的虚拟硬盘文件) 容量扩充至36Gb 。按照此信息,将我的虚拟硬盘文件 E:/Red Hat Linux/Red Hat Linux.vmdk 扩充至10Gb 吧。键入命令D:/vmware>vmware-vdiskmanager -x 10Gb "E:/Red Hat Linux/Red Hat Linux.vmdk" 。出现错误:

 

,这里要关闭虚拟机后方可运行
原因是没有关闭虚拟机正在运行的Linux系统,init 0 关闭它再来。D:/vmware>vmware-vdiskmanager -x 10Gb "E:/Red Hat Linux/Red Hat Linux.vmdk" ,好!此时开始执行
等到100% 后,成功

=================================================================================

将空余空间划分成第四个主分区sda4,格式化成ext3,然后挂载到 / 目录下,毕竟是根目录占用了93%的硬盘空间,想来有道理,动手~~~

我们先启动Linux系统来fdisk -l 看一下

root@localhost ~]# 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        25    200781   83 Linux
/dev/sda2            26       620   4779337+ 83 Linux
/dev/sda3           621       652    257040   82 Linux swap

可以看到硬盘空间确实已经增大了,但没有出现在我们的分区列表中。df 查看也和原来一样

[root@localhost ~]# df -l

文件系统    1K-块        已用         可用           已用%   挂载点

/dev/sda2              4704236    4374939    329297        93%        /
/dev/sda1              194443      9377        175027        6%          /boot
none                     95336        0              95336          0%         /dev/shm

那么我们就把这个空余空间用起来吧—— fdisk命令在/dev/sda(第一块scsi硬盘) 上   n 新加一个分区

[root@localhost ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 1305.
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       按m或输入 help 获取参数帮助
Command action
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):n          选择 n 添加一个新分区

Command action
   e   extended
   p   primary partition (1-4)        选择p 创建主分区,e为创建逻辑分区
p

Selected partition 4
First cylinder (653-1305, default 653):               不作输入按系统默认大小去分配
Using default value 653
Last cylinder or +size or +sizeM or +sizeK (653-1305, default 1305):       不作输入按系统默认大小去分配
Using default value 1305

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: /x{8BBE}/x{5907}/x{6216}/x{8D44}/x{6E90}/x{5FD9}.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

w保存并退出,将空余空间全部划分给了一个新分区 sda4,The kernel still uses the old table.
The new table will be used at the next reboot.   好吧,重启后再来 reboot 。

好了,再来fdisk -l 看一下

[root@localhost ~]# 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        25    200781   83 Linux
/dev/sda2            26       620   4779337+ 83 Linux
/dev/sda3           621       652    257040   82 Linux swap
/dev/sda4           653      1305   5245222+ 83 Linux

/dev/sda4出来了,但还没有格式化,也没有挂载到目录上面去。接下来就将它格式化成ext3,并挂载到 根目录(/)下去。

格式化成ext3 命令 mkfs.ext3 /dev/sda4

[root@localhost~]# mkfs.ext3 /dev/sda4
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
656000 inodes, 1311305 blocks
65565 blocks (5.00%) reserved for the super user
First data block=0
41 block groups
32768 blocks per group, 32768 fragments per group
16000 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

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

========================================================================

 

挂载到根目录命令:mount /dev/sda4   /         命令执行完,成功则没有任何提示,我们df 看一下

[root@localhost ~]# df -l

文件系统    1K-块        已用         可用           已用%   挂载点

/dev/sda2              4704236    4374939    329297        93%        /
/dev/sda1              194443      9377        175027        6%          /boot
none                     95336        0              95336          0%         /dev/shm

/dev/sda4              4704236    4374939    329297        93%        /

 

这里可以看出挂载并没什么结果,所以放弃使用这个方法

 

结合下面这个方法,跳过使用parted工具,将其加入到逻辑卷标即可

 

 

 Resizing LVM Volumes in Linux:
By David Maphis 4/29/2008
In this example I will changed the size of my root partition without
affecting any data. Since this is a VM, I can extend my vmdk with the
“vmkfstools” command. To do this I will have to shutdown the VM to release
the lock on the file then resize it. Power up when complete.
Once this is done, make a partition that uses the available space you wish to
add to the LVM volume using “parted”. It has an interactive shell, so just
enter “parted” in a shell:
parted
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 32.3kB 206MB 206MB primary ext3 boot
2 206MB 8587MB 8382MB primary lvm
So I have from 8588MB to 10.7GB available for a new partition. For this
example I’m only using up to 9588MB. Your numbers will obviously vary.
So now let’s make a partition from the new space:
(parted) mkpart primary 8588 9588
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
1 32.3kB 206MB 206MB primary ext3 boot
2 206MB 8587MB 8382MB primary lvm
3 8587MB 9588MB 1001MB primary
You can now see that we have allocated a total of 1001MB of space to
partition 3 or /dev/sda3.
Next we need to create a Physical Volume (PV) with “pvcreate”. LVM also has
an interactive shell, so type “lvm” in a shell prompt:
lvm
lvm> pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
lvm> pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup00 lvm2 a- 7.78G 32.00M
/dev/sda3 lvm2 -- 954.44M 954.44M
Now there is a partition that is about 1GB of space available on PV
/dev/sda3.
Now we need to extend the Volume Group (VG) to include the new PV with the
“vgextend” command:
lvm> vgextend VolGroup00 /dev/sda3
Volume group "VolGroup00" successfully extended
lvm> pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup00 lvm2 a- 7.78G 32.00M
/dev/sda3 VolGroup00 lvm2 a- 928.00M 928.00M
By virtue of the fact that we used the same name as the existing VG, we have
now included this PV in the VG.
Extend the Logical Volume (LV) to include the new space with “lvextend”. To
determine that size of the extents we want to add, we first need to view the
VG:
lvm> vgdisplay VolGroup00
--- 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 8.69 GB
PE Size 32.00 MB
Total PE 278
Alloc PE / Size 248 / 7.75 GB
Free PE / Size 30 / 960.00 MB
VG UUID Ll4Bg4-jIFV-lfut-z4Ae-JTfH-0Hzp-Zf9RjM
Looking at this we can see that the total “Free PE” is 30. We will use this
in the following command:
lvm> lvextend -l+30 /dev/VolGroup00/LogVol00
Extending logical volume LogVol00 to 8.19 GB
Logical volume LogVol00 successfully resized
lvm> lvs
LV VG Attr LSize Origin Snap% Move Log Copy%
LogVol00 VolGroup00 -wi-ao 8.19G
LogVol01 VolGroup00 -wi-ao 512.00M
So there you are, you have now extended your LV. Now we have to clue the OS
in what is happening.
We need to expand the file system to make the new space visible to the OS
with resize2fs (Fedora, CentOS) or ext2online (RedHat)
[root@labserver02 dev]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
7.1G 1.3G 5.4G 20% /
[root@labserver02 dev]# resize2fs
resize2fs 1.40.2 (12-Jul-2007)
Usage: resize2fs [-d debug_flags] [-f] [-F] [-p] device [new_size]
[root@labserver02 dev]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.40.2 (12-Jul-2007)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing
required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 2146304 (4k)
blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 2146304 blocks long.
[root@labserver02 dev]# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
8.0G 1.3G 6.3G 17% /

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值