[轉]VMware虛擬機中調整Linux分區大小手記

使用後的總結: 第一步的擴展虛擬硬盤空間其實很簡單,主要是虛擬機所在HOST系統分區空間足夠大,多花點時間而已;Gparted工具很好用,分區的前後都可調整, 從90G擴展到120G用了4小時多,要有足夠耐心,呵呵!

------下面是參考文章------

前段時間用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

         到此所有問題已解決,花費了大半天的時間才整理出文檔性的記錄,以後可以直接參考着解決了。睡覺@@@

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章