为虚拟机添加硬盘并进行分区

如果你的Linux硬盘空间不足,那么我们可以添加硬盘并且对其进行分区,下面是主要步骤:
1.首先,把虚拟机中的系统关闭,如果现在不关闭的话,后面添加完硬盘还要重启。然后选择
虚拟机》设置,在弹出的对话框中点击硬盘(SCSI),接着点击添加,就会出现添加硬件向导:硬盘》
SCSI(S)(推荐)》创建新虚拟磁盘》,设置磁盘大小,默认是20G。选择将虚拟磁盘拆分成多个文件。
2.打开虚拟机的Linux系统。执行fdisk -l:
[root@localhost ~]# fdisk -l


Disk /dev/sda: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          38      305203+  83  Linux
/dev/sda2              39       12540   100422315   83  Linux
/dev/sda3           12541       13054     4128705   82  Linux swap / Solaris


Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table

可以看到此时多了个sdb,但是并没有分区,下面我们为sdb进行分区:
输入命令fdisk /dev/sdb,在提示下输入m,就会出现以下列表,根据需要选择选项完成自己的需要。输入n,即为增加分区,接着输入p建立主分区,再输入1,下面会提示卷的起始地址和结束地址,回车默认就可以了。最后我们键入w保存并退出。
[root@localhost /]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

The number of cylinders for this disk is set to 2610.
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)
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
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): 
Using default value 2610


Command (m for help): w
The partition table has been altered!


Calling ioctl() to re-read partition table.
Syncing disks.

3.再次输入命令fdisk -l就可以看到多了/dev/sdb1了,说明我们已经成功为sdb添加了分区。
[root@localhost /]# fdisk -l


Disk /dev/sda: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          38      305203+  83  Linux
/dev/sda2              39       12540   100422315   83  Linux
/dev/sda3           12541       13054     4128705   82  Linux swap / Solaris


Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes


   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2610    20964793+  83  Linux

4.接着,对新建立的分区进行格式化,格式化成ext3的系统:
[root@localhost /]# mkfs.ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2621440 inodes, 5241198 blocks
262059 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
16384 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000


Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done


This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

5.最后,我们就可以把/dev/sdb1挂载到新建的/u02目录了:
[root@localhost /]# mount /dev/sdb1 /u02
[root@localhost /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              93G   18G   71G  21% /
/dev/sda1             289M   16M  259M   6% /boot
tmpfs                1014M     0 1014M   0% /dev/shm
/dev/scd0              23M   23M     0 100% /media/CDROM
/dev/scd1             2.8G  2.8G     0 100% /media/RHEL_5.4 i386 DVD
/dev/sdb1              20G  173M   19G   1% /u02

6.挂载成功之后,会在本次系统启动过程中生效,如果下次系统重启了,那么就会失去这次的挂载。
想要每次启动系统都自动挂载,可以在/etc/fstab文件中加入如下一行内容:
/dev/sdb1 /u02 ext3 defaults 0 0

那么就可以保证即使系统重新启动/dev/sdb1也会自动挂载


请使用浏览器的分享功能分享到微信等