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

来源:互联网 发布:爱淘宝有返利吗 编辑:程序博客网 时间:2024/06/11 10:09

利用vmware安装centos6.4时,发现虚拟机的linux存储容量不够,则增加10G的空间给linux,可是这样并不能解决linux中分区大小,需要配置。查看分区占用情况,发现/dev/sda2挂载的根分区占用最多,软件全部安装在该分区中。/dev/sda1 及swap分区情况正常。我们分区扩大sda2的分区大小

[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上。

            网上有很多方法都是说,将空余空间划分成第四个主分区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 。或者通过查看cat /proc/partitions查看分区情况,通过命令

解决方法:执行下partprobe 命令

 

       partprobe包含在parted的rpm软件包中。partprobe可以修改kernel中分区表,使kernel重新读取分区表。 因此,使用该命令就可以创建分区并且在不重新启动机器的情况下系统能够识别这些分区

 

查看是否安装该命令:

[root@db1 dev]# rpm -q parted

parted-1.8.1-23.el5

 

我们执行一下该命令:

[root@db1 dev]# partprobe

Warning: Unable to open /dev/hdc read-write (Read-only file system).  /dev/hdc has been opened read-only.


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

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

[root@localhost~]# mkfs.ext4 /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从光驱启动………………  

 


一路默认选项。

可以查看分区选项,通过resize来调整分区。



嘿嘿,还可以,行了,所有调整好后,最后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