Windows下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下的分区工具
2)
root@ubuntu:/# du --max-depth=1 -h
查看当前目录的磁盘占用情况

相关内容