作者:金良(golden1314521@gmail.com) csdn博客:http://blog.csdn.net/u012176591
用了一段Linux虛擬機,發現安裝虛擬機時分配的空間不夠用,又不舍得刪掉虛擬機重裝,畢竟辛辛苦苦在上面裝了好多軟件,我可不想再為這些配置折騰一番。
怎么辦呢,當然是虛擬機擴容。
1.虛擬機所在磁盤空間不夠,能夠把虛擬機復制到還有一塊磁盤里。
2.進入VMware的安裝文件夾,在命令提示符后輸入"vmware-vdiskmanager",什么參數也不加直接按回車鍵,可顯示這一命令的說明。
3.這一步要關閉虛擬機才行:
擴容命令:C:\Program Files\VMware\VMware Workstation>vmware-vdiskmanager -x 40Gb "D:\Program Files\Virtual Machines\Ubuntu\Ubuntu.vmdk"
參數 "-x" 表示要擴展虛擬機硬盤空間,緊隨其后的數字是要擴展到的大小 ( 擴展到 40GB ,包括了原先的磁盤容量 ) 。
重新啟動 VMware ,會發現虛擬機硬盤空間已變成 40GB 了
4.運行命令 df -ah查看磁盤情況,發現磁盤空間和擴容前一樣,仍是20G:
5.查看分區,運行命令:
fdisk -l以下是當前的磁盤分區表截圖,這時我們能夠看到磁盤的總量的確添加�到 40GB 了,可是分區僅僅有曾經的那幾個原有的分區。

添加�分區過程截圖:
重新啟動后格式化該分區:
發現一個問題,我命名擴增了20G,為什么新增的分區容量這么小,以至於格式化時提示“Filesystem too small for a journal”
再看虛擬機配置,截圖:
圖上顯示的是:眼下此虛擬硬盤的大小(current size)是19.8G,最大是(maximum size)40G。可是怎么獲得這個最大磁盤容量呢?
然后我用了還有一種方法擴容:
與之前的思路不同,本方法不是擴大原來的虛擬磁盤的容量,而是另加一個虛擬磁盤來擴充容量。
第一步、退出Linux虛擬機系統,通過在VMware上對虛擬機進行設置來添加�一塊虛擬硬盤。
附截圖:
第二步、重新啟動Linux虛擬機,對這塊虛擬硬盤進行分區、格式化和掛載。
看下磁盤分區情況:
root@ubuntu:/home/now/android4.0.1# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e4e09 Device Boot Start End Blocks Id System /dev/sda1 * 2048 39845887 19921920 83 Linux /dev/sda2 39847934 41940991 1046529 5 Extended /dev/sda5 39847936 41940991 1046528 82 Linux swap / Solaris Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x00000000 Disk /dev/sdb doesn't contain a valid partition table
這里能夠看到/dev/sdb 就是我們新加入�的硬盤,我們須要給新的硬盤分區。
步驟例如以下(看提示):
root@ubuntu:/home/now/android4.0.1# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x9d6a9772. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) Command (m for help): m 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 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): 1 First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): Using default value 41943039 Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x9d6a9772 Device Boot Start End Blocks Id System /dev/sdb1 2048 41943039 20970496 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
再看磁盤分區,已達到我們想要的結果:
root@ubuntu:/home/now/android4.0.1# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders, total 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x000e4e09 Device Boot Start End Blocks Id System /dev/sda1 * 2048 39845887 19921920 83 Linux /dev/sda2 39847934 41940991 1046529 5 Extended /dev/sda5 39847936 41940991 1046528 82 Linux swap / Solaris Disk /dev/sdb: 21.5 GB, 21474836480 bytes 213 heads, 34 sectors/track, 5791 cylinders, total 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x9d6a9772 Device Boot Start End Blocks Id System /dev/sdb1 2048 41943039 20970496 83 Linux
我們把新加的磁盤/dev/sdb1 掛載到根分區
root@ubuntu:/home/now# mount /dev/sdb1 / mount: you must specify the filesystem type
系統提示須要明白的文件類型,即我們須要對其進行格式化。
以下是格式化操作:
root@ubuntu:/home/now# mkfs -t ext3 /dev/sdb1 mke2fs 1.42 (29-Nov-2011) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 1310720 inodes, 5242624 blocks 262131 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=0 160 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, 2654208, 4096000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done然后掛載,成功:
root@ubuntu:/home/now# mount /dev/sdb1 / root@ubuntu:/home/now#
查看掛載情況,能夠看到/dev/sdb1掛載到了根分區:
root@ubuntu:/home/now# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 19478204 16257028 2208696 89% / udev 503544 4 503540 1% /dev tmpfs 102472 1108 101364 2% /run none 5120 0 5120 0% /run/lock none 512352 152 512200 1% /run/shm /dev/sdb1 19478204 16257028 2208696 89% /
能夠看到這不是我們想要的結果,這是由於“掛載點”必須是一個已經存在的文件夾,這個文件夾能夠不為空,但掛載后這個文件夾下曾經的內容將不可用,只是umount以后會恢復正常。
使用卸載命令,發現不能卸載:
root@ubuntu:/# umount /dev/sdb1 umount: /: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))
好在使用mount掛載系統的一個特點是一旦系統重新啟動之后就失效,須要又一次掛載。所以我們重新啟動就能卸載掉磁盤/dev/sdb1了。
系統重新啟動后,在根文件夾下新建一個子文件夾(由於全部的文件夾都是掛載到根文件夾下的),然后把磁盤/dev/sdb1掛載在此文件夾下:
root@ubuntu:/home/now# cd / root@ubuntu:/# mkdir /storehouse root@ubuntu:/# mount /dev/sdb1 /storehouse
查看存儲空間利用情況:
root@ubuntu:/# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 19G 16G 2.2G 88% / udev 492M 4.0K 492M 1% /dev tmpfs 101M 956K 100M 1% /run none 5.0M 0 5.0M 0% /run/lock none 501M 152K 501M 1% /run/shm /dev/sdb1 20G 173M 19G 1% /storehouse
看到了吧,最后一行就是我們掛載的文件夾。
使用下面試試,往里面拷貝一個壓縮文件:
root@ubuntu:/# cp /home/now/android4.0.1.tar.gz /storehouse root@ubuntu:/# ls /storehouse/ android4.0.1.tar.gz lost+found
然后查看存儲空間利用情況:
root@ubuntu:/# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 19G 16G 2.2G 88% / udev 492M 4.0K 492M 1% /dev tmpfs 101M 968K 100M 1% /run none 5.0M 0 5.0M 0% /run/lock none 501M 152K 501M 1% /run/shm /dev/sdb1 20G 1.9G 17G 11% /storehouse
可以看到可以正常使用。
改動/etc/fstab文件,開機自己主動掛載:
如圖,紅色方框是我們加入�的