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

 
前段时间用VMware5.5安装了CentOS5.3,安装的时候分配了5Gb的虚拟硬盘空间给Linux系统,系统安装选择很多组件和软件,后面使用时又安装也一些软件,结果导致虚拟硬盘空间不足。查看分区占用情况,发现/dev/sda2挂载的根分区占用最多,软件全部安装在该分区中。/dev/sda1 及swap分区情况正常。

[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% 后,成功


到这里第一个问题(扩充虚拟硬盘容量)解决了。

          接下来第二个问题,如何将扩充进来的空余空间,加到sda2上。百度 & Google了一通,都没有找到合适的解决方法。很多都说ext3分区不能调整分区大小,是在partitionmagic中,而partitionmagic赪indows下可以调整分区,而且功能也很好,却偏偏又不能调整ext3分区。

            网上有很多方法都是说,将空余空间划分成第四个主分区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%        /
           成了这个样子,看来这方法并不行,怎么新挂上去的分区也被占用了93%。这里没懂是怎么回事。没有达到我想要的效果,这个问题用这个方法解决不了,怎么办呢?歇会儿,再去百度&Google逛逛。

           恩,据CSDN有位仁兄说,Linux有这么个命令 parted ,可以来调整分区,不过很麻烦,全部是命令操作,还要自己计算好调整分区的起始和结束位置,技术含量太高,偶不会了,只会搞些简单的。

          继续找ing ~~,又找到这么个方法,有个软件可以像Windows中的partitionmagic一样操作Linux分区,名叫gparted ,和parted命令有些相似,是不是有些根源?赶快去网上找找,在它的官网下载了这个软件,最新版滴,liveCD版的ISO文件,加载在光驱中启动去操作分区。init 6 重启 ,加载ISO文件,设置虚拟机BIOS从光驱启动………………  


GNOME partition editor 启动选择界面,就第一个直接回车吧,反正我也不晓得。接下来好像还有软件语言选择吧,找了下,好像没有Chinese,有Japanese(TMD小日本),不过还是直接回车用USA吧。进入软件,看一下界面,是不是很像partitionmagic。

里面也看到了我的所有分区信息,接下来就是删除sda4,让他空出来,然后调整sda2,将它调大。

          又有问题了,swap分区在sda2 后面,sda2调不动。那就把swap先删了,把sda2调大,给swap留256M,然后再把这256M左右(调不准,输数字还不行)化成swap分区。

嘿嘿,还可以,行了,所有调整好后,最后Apply,等待它去执行吧!等待



完成后,再重启,进入Linux系统,用fdisk -l 和df 命令查看,哈哈,OK了

[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      1272 10016527+ 83 Linux
/dev/sda3          1273      1305    265072+ 82 Linux swap
[root@localhost ~]# df
文件系统    1K-块        已用         可用           已用%   挂载点

/dev/sda2              9860296   2024280    7435356     22%       /
/dev/sda1              194443      9377         175027       6%        /boot
none                     95336       0              95336        0%        /dev/shm

         到此所有问题已解决,花费了大半天的时间才整理出文档性的记录,以后可以直接参考着解决了。睡觉@@@

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值