歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Windows下Linux虛擬機磁盤分區擴容

Windows下Linux虛擬機磁盤分區擴容

日期:2017/2/28 16:26:00   编辑:Linux教程

場景說明:我的主機是Windows 7, 虛擬機是Wmware,裡面裝的是Linux。
Windows下的Linux虛擬機, 磁盤分區不夠,想擴容。

1) 首先進入vm中的Linux,sudo fdisk -l 列出磁盤的情況
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders #可以看出sda硬盤共10G,1306個磁道, 如果你有多個硬盤,應該會有sdb, sdc等
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00087bbc

Device Boot Start End Blocks Id System
/dev/sda1 * 1 618 4959232 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 618 653 280577 5 Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5 618 653 280576 82 Linux swap / Solaris


#可以看出
1)已經用了 1~653磁道, 還有1306-653=653個磁道未用, 已經有5G已經分區,還有5G未分區。
和現實一致,我的虛擬機文件本來默認5G, 剛把磁盤擴容到10G,還有5G還為分區。
2)現有的5G被分為了3個區, 除了有一個是擴展分區(擴展分區是用來掛其他邏輯分區用的),也就兩個分區 一個是 linux分區 一個是 linux swap(交換分區)

2)查看現有分區的磁盤使用情況以及掛載情況
root@Ubuntu:/home/nemo# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 4.7G 4.1G 410M 91% /
none 119M 248K 119M 1% /dev
none 123M 164K 123M 1% /dev/shm
none 123M 296K 123M 1% /var/run
none 123M 0 123M 0% /var/lock
none 123M 0 123M 0% /lib/init/rw

可以看到現有的5G分區已經基本上被使用玩。/dev/sda1分區是被掛載在了"/"根目錄下, 還剩9%的剩余容量。

2)開始把剩余的為使用的5G磁盤也分區吧,並且給其掛載一個目錄
root@ubuntu:/home/nemo# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

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): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00087bbc

Device Boot Start End Blocks Id System
/dev/sda1 * 1 618 4959232 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 618 653 280577 5 Extended
Partition 2 does not end on cylinder boundary.
/dev/sda5 618 653 280576 82 Linux swap / Solaris

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
No free sectors available #上面的擴展分區已經被用完。

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
primary partition #需要輸入全稱:primary partition,直接輸入 p 變成了打印的命令
Partition number (1-4): 2
Partition 2 is already defined. Delete it before re-adding it.

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
primary partition 3
Partition number (1-4): 3
First cylinder (653-1305, default 653):
Using default value 653
Last cylinder, +cylinders or +size{K,M,G} (653-1305, default 1305):
Using default value 1305 #一路回車 九八剩余的未使用的分區全利用了。


Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00087bbc

Device Boot Start End Blocks Id System
/dev/sda1 * 1 618 4959232 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 618 653 280577 5 Extended
Partition 2 does not end on cylinder boundary.
/dev/sda3 653 1305 5240556+ 83 Linux
/dev/sda5 618 653 280576 82 Linux swap / Solaris

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: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
root@ubuntu:/home/nemo#


注意,
1)這裡雖然通過fdisk -l能看到新增的分區,但是要重啟後才能寫入分區表,比如這時要創建一個PV(物理卷)會提示找不到分區:
重啟系統後,新的分區就能正常使用了:
root@ubuntu:/home/nemo# reboot #

2)新的分區必須經過格式化之後才能掛載,否則提示mount: you must specify the filesystem type
root@ubuntu:/# mkfs.ext4 /dev/sda3

3)可以將分區掛接到一個已存在的不為空的目錄上。但掛載後這個目錄下以前的內容將不可用。我們想掛到/home下,所以先備份下/home, 再掛載,掛完後再copy回來。
cd /
mv /home /home2
mkdir /home
mount /dev/sda3 /home
cp /home2/* /home/
ok..

4)查看戰果 查看各個分區的掛載目錄以及分區的磁盤占用情況
root@ubuntu:/home# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 4.7G 4.1G 410M 91% /
none devtmpfs 119M 252K 119M 1% /dev
none tmpfs 123M 88K 123M 1% /dev/shm
none tmpfs 123M 296K 123M 1% /var/run
none tmpfs 123M 0 123M 0% /var/lock
none tmpfs 123M 0 123M 0% /lib/init/rw
/dev/sda3 ext4 5.0G 138M 4.6G 3% /home #=========> 剩余4.6G, 可以放心玩啦!

2、一個分區掛載在一個已存在的目錄上,這個目錄可以不為空,但掛載後這個目錄下以前的內容將不可用。對於其他操作系統建立的文件系統的掛載也是這樣。

附:
1)
參考如下文章: Linux下的分區工具
http://www.linuxidc.com/Linux/2007-12/9635.htm2)
root@ubuntu:/# du --max-depth=1 -h
查看當前目錄的磁盤占用情況

Copyright © Linux教程網 All Rights Reserved