为虚拟机linux扩充硬盘容量

来源:互联网 发布:淘宝拍卖的玉是真的吗 编辑:程序博客网 时间:2024/04/29 15:06
一、在虚拟机设置界面添加一个硬盘

这里写图片描述
添加完成后要重启一次才能看到新加的设备

二、在linux中查找添加的硬盘设备
  • 一般ls /dev/sd*下的就会看到多出来的设备了。这里是sdb

    anzyelay@ubuntu:~$ ls /dev/sd*sda   sda1  sda2  sda3  sda5  sdb

    可以使用df 确认下sdb是否还未被mount,如果mount了,那这个肯定不是新加的设备了。

  • 使用fdisk查看

    anzyelay@ubuntu:~$ sudo fdisk -lDisk /dev/sda: 42.9 GB, 42949672960 bytes255 heads, 63 sectors/track, 5221 cylinders, total 83886080 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x0001cfa3   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048    39845887    19921920   83  Linux/dev/sda2        39847934    41940991     1046529    5  Extended/dev/sda3        41945715    83875364    20964825   83  Linux/dev/sda5        39847936    41940991     1046528   82  Linux swap / SolarisDisk /dev/sdb: 21.5 GB, 21474836480 bytes213 heads, 34 sectors/track, 5791 cylinders, total 41943040 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x22bea7e9Disk /dev/sdb doesn't contain a valid partition table

    可以看到新加的设备,由于还没有分区,所以报最后一条错误。

三、添加新分区
  • 输入如下命令

    anzyelay@ubuntu:~$ sudo fdisk /dev/sdbCommand (m for help): mCommand 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)
    • 输入命令【n】添加新分区。
    • 输入命令【p】创建主分区。
    • 输入【回车】,选择默认大小,这样不浪费空间
    • 输入【回车】,选择默认的start cylinder。
    • 输入【w】,保持修改
      新分区添加后再次ls /dev/sdb*可看到多了一个/dev/sdb1出来。
  • 格式化新分区,如果不行重启次再格式化。

    sudo mkfs.ext2 /dev/sdb1
mount新分区到一目录

可以新建一目录/dir。然后mount /dev/sdb1 /dir,现在再来查看下df -h

anzyelay@ubuntu:~$ df -hFilesystem      Size  Used Avail Use% Mounted on/dev/sda1        19G   11G  7.6G  58% /udev            2.0G  4.0K  2.0G   1% /devtmpfs           396M  852K  395M   1% /runnone            5.0M     0  5.0M   0% /run/locknone            2.0G  124K  2.0G   1% /run/shm/dev/sdb1        20G  20M   20G    1% /dir/dev/sda3        20G   13G  6.3G  67% /home
修改配置自动加载

vim修改/etc/fstab文件,加入【/dev/sdb1 /dir ext2 defaults 0 0】一行,并保存,实现开机自动mount。

0 0