扩展虚拟机硬盘大小

虚拟机装的Redhat 9 分配了16G硬盘空间,用了一段时间发现不够用了才想起扩展硬盘大小。

先百度了下,好像挺容易的。就照着来了。

一般有两种方式要么就是直接在虚拟机中直接添加设备再加一块硬盘,另一种就是扩展现有虚拟机硬盘的大小。我用的是这种方式

1    关闭虚拟机,然后点击编辑虚拟机edit virtual machine setting

在扩展虚拟机硬盘之前要先把所有的快照删除,有快照是不能更改虚拟机硬盘大小的。


选择expand选项 

注意填的值是扩展以后硬盘的大小

然后点expand,一段时间以后进入系统,我的RedHat9在扩展硬盘以后进不了图形界面,会有一个shell界面让你输入管理员密码,看上面的错误提示好像是有个字体 不能初始化,错误文件在/etc/X11/X86Config

把字体第21行  FontPath  "unix/:7100 屏蔽,就在这一行前面加个# 就OK,重启进入图形界面

2  用fdisk -l查看当前硬盘设备,

fdisk -l
Disk /dev/sda:10.7 GB, 10737418240 bytes
255 heads, 63sectors/track, 1305 cylinders
Units =cylinders of 16065 * 512 = 8225280 bytes
Diskidentifier: 0x000b2cce

  DeviceBoot     Start        End     Blocks   Id System
/dev/sda1                  922    7405933+  83 Linux
/dev/sda2            923       1044     979965   82 Linux swap / Solaris

fdisk /dev/sda //然后依次选择m,p,n,e,3,p,n,l,p,w

这个具体要看自己的电脑设置了,m是命令帮助,多看几次这个

p是建立主分区,

n是新建分区

e是建立扩展分区

我当时自己的也是探索过程,命令行里面的东西没记下来,网上找了一个别人写的大概过程差不多,希望对别人有所参考

3

接下来就是对加进去的那部分进行扩展,让linux可以使用这部分扩展的空间。在扩展之前,是用不了的。


以下是我执行的过程

[root@localhost ~]# fdisk/dev/hda4

The number of cylindersfor this disk is set to 3916.
There is nothing wrong with that, but this is larger than1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions ofLILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)

Command (m forhelp): p

Disk /dev/hda4:32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000bc363

Device Boot StartEnd Blocks Id System
/dev/hda1 * 1 25 200781 83 Linux
/dev/hda2 26 1958 15526822+ 8e Linux LVM
/dev/hda3 1959 2610 5237190 8eLinux LVM

Command (m forhelp): n
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (2611-3916, default 2611): 
Using default value 2611
Last cylinder or +size or +sizeM or +sizeK (2611-3916, default3916): 
Using default value 3916

Command (m forhelp): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (LinuxLVM)

Command (m forhelp): p

Disk /dev/hda:32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000bc363

Device Boot StartEnd Blocks Id System
/dev/hda1 * 1 25 200781 83 Linux
/dev/hda2 26 1958 15526822+ 8e Linux LVM
/dev/hda3 1959 2610 5237190 8e Linux LVM
/dev/hda4 2611 3916 10490445 8e LinuxLVM

Command (m forhelp): 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 orresource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

我的REDHAT9原来只有/dev/sda1 /dev/sda2 

在经过上面的过程以后就出现了/dev/sda3   用fdisk -l命令后面显示的是extended

在别人的教程里面都是马上执行

# mkfs.ext3 /dev/sda3
发现这个命令执行不了,提示 not enough space to build proposed filesystem
百度了下,发现我们建立硬盘的时候选择的是primary 这个选项,还在把这个再划成 logic磁盘
再整,还是用fdisk  /dev/sda
n 添加一个新的分区
然后选择逻辑分区就好了
然后执行上面那个命令# mkfs.ext3 /dev/sda5就没问题了
注意原来未分逻辑分区的时候是sda3  分了以后sda3中又分出了sda5
再次fdisk -l
这里的/dev/sda显示的就是扩展以后的硬盘大小,我这个是扩展分区以后的截图。

挂载虚拟硬盘
       分区分好,也格式化好了,下面就是挂载

因为原来硬盘不够用,所以先把opt目录改成opt1,然后新建一个文件夹,命名为opt

         我把/dev/sda5   挂载到/opt下
         [root@linux root]# mkdir /opt   ## 首先建立挂载的目录opt
         [root@linux root]# mount /dev/sda5  /opt  ##将sda5挂载到opt

然后把opt1目录中的东西转移到opt目录下

cp -r /opt1/*   /opt

删除opt1文件夹  

然后再用df -h查看磁盘状态

我原来/sda1使用是100%,转移opt文件夹到sda5以后,使用就成62%,

 

# vi /etc/fstab ## 用vi修改/etc/fstab,使系统启动就可以自动挂载
         (点击“i”进入插入模式对文本内容进行修改,改好后点“Esc”,输入冒号“:”进入命令行模式,输入wq保存退出,具体操作可以参考vi常用指令)

        在内容中加上一行:
         /dev/sda5            /opt                     ext3     defaults         1 2

OVER,空间增加成功

以上参考了下面这个创建虚拟硬盘的文章,对作者表示感谢。

转在下面供参考。






创建虚拟硬盘
        1、关闭VM中正在运行的虚拟系统;
        2、在虚拟系统名称上点右键-》Virtual Machine Settings;
        3、在Hardware页点“Add”-》Add a hard disk-》Create a new virtual disk-》SCSI(recommended)-》分配空间大小-》OK;
        4、可以看见Hardware中出现了一块新的硬盘Hard Disk 2。

对虚拟硬盘进行分区和格式化(转载自bixuan)
       [root@cncmail data1]# fdisk -l ## 这里是查看目前系统上有几块硬盘

         Disk /dev/sda: 36.4 GB, 36401479680 bytes
         255 heads, 63 sectors/track, 4425 cylinders
         Units = cylinders of 16065 * 512 = 8225280 bytes

        Device Boot     Start        End     Blocks    Id   System
         /dev/sda1    *          1        255    2048256    83   Linux
         /dev/sda2            256       1530   10241437+   83   Linux
         /dev/sda3           4296       4425    1044225    82   Linux swap
         /dev/sda4           1531       4295   22209862+    f   Win95 Ext'd (LBA)
         /dev/sda5           1531       2805   10241406    83   Linux
         /dev/sda6           2806       4295   11968393+   83   Linux

         Partition table entries are not in disk order

         Disk /dev/sdb: 36.7 GB, 36703918080 bytes ## 这里发现/dev/sdb,容量36.7G,切未被分区
         255 heads, 63 sectors/track, 4462 cylinders
         Units = cylinders of 16065 * 512 = 8225280 bytes

         Disk /dev/sdc doesn't contain a valid partition table
         [root@linux root]# fdisk /dev/sdb ## 接下去就对/dev/sdb分区进行分区

         The number of cylinders for this disk is set to 4462.
         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)

         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/sdb: 36.7 GB, 36703918080 bytes
         255 heads, 63 sectors/track, 4462 cylinders
         Units = cylinders of 16065 * 512 = 8225280 bytes

            Device Boot     Start        End     Blocks    Id   System

         Command (m for help): n      ## 增加一个分区
         Command action
            e    extended
            p    primary partition (1-4)
            ## 因为通常选择主分区,所以这里打一个p
         p
         Partition number (1-4): 1     ## 这里因为是第一个分却,所以只选择1,如果是第二个分区,则选择2,依次类推
         First cylinder (1-4462, default 1): ## 新分区起始的磁盘块数
         Using default value 1
         Last cylinder or +size or +sizeM or +sizeK (1-4462, default 4462): 如果要分区10G,这里可以直接输入:+10240M,因为这里要全部使用硬盘空间,则用默认
         Using default value 4462

         Command (m for help): p

         Disk /dev/sdb: 36.7 GB, 36703918080 bytes
         255 heads, 63 sectors/track, 4462 cylinders
         Units = cylinders of 16065 * 512 = 8225280 bytes

            Device Boot     Start        End     Blocks    Id   System
         /dev/sdb1              1       4462   35840983+   83   Linux
## 这里第一个分区已经分好了,接下去得把这个分区写入硬盘,用w
         Command (m for help): w
         The partition table has been altered!

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

         下面的工作就是对该硬盘进行格式,我这里是格式化成ext3
         [root@linux root]# mkfs.ext3 /dev/sdb1 (这里原来的命令是:mke2fs -j /dev/sdb1,试了一下不成         功,改了)
         mke2fs 1.32 (09-Nov-2002)
         Filesystem label=
         OS type: Linux
         Block size=4096 (log=2)
         Fragment size=4096 (log=2)
         4480448 inodes, 8960245 blocks
         448012 blocks (5.00%) reserved for the super user
         First data block=0
         274 block groups
         32768 blocks per group, 32768 fragments per group
         16352 inodes per group
         Superblock backups stored on blocks:
                 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
                 4096000, 7962624

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

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

         检查一下,是否已经格式好
         [root@linux root]# fdisk -l

         Disk /dev/sda: 36.4 GB, 36401479680 bytes
         255 heads, 63 sectors/track, 4425 cylinders
         Units = cylinders of 16065 * 512 = 8225280 bytes

          Device Boot     Start        End     Blocks    Id   System
         /dev/sda1    *          1        255    2048256    83   Linux
         /dev/sda2            256       1530   10241437+   83   Linux
         /dev/sda3           4296       4425    1044225    82   Linux swap
         /dev/sda4           1531       4295   22209862+    f   Win95 Ext'd (LBA)
         /dev/sda5           1531       2805   10241406    83   Linux
         /dev/sda6           2806       4295   11968393+   83   Linux

         Partition table entries are not in disk order

         Disk /dev/sdb: 36.7 GB, 36703918080 bytes
         255 heads, 63 sectors/track, 4462 cylinders
         Units = cylinders of 16065 * 512 = 8225280 bytes

         Device Boot     Start        End     Blocks    Id   System
         /dev/sdb1              1       4462   35840983+   83   Linux

挂载虚拟硬盘
       分区分好,也格式化好了,下面就是挂载
         我把/dev/sdb1挂载到/data1下
         [root@linux root]# mkdir /data1   ## 首先建立挂载的目录data1
         [root@linux root]# mount /dev/sdb1 /data1 ##将sdb1挂载到data1
       
         重启系统之后,查看是否挂载成功:
         [root@linux data1]# df -h
         文件系统               容量   已用 可用 已用% 挂载点
         /dev/sda1              2.0G   454M   1.4G   25% /
         /dev/sda6               12G    53M    11G    1% /bak
         /dev/sdb1               34G    33M    32G    1% /data1
         none                   250M      0   250M    0% /dev/shm
         /dev/sda2              9.7G   1.5G   7.7G   17% /usr
         /dev/sda5              9.7G   8.6G   559M   95% /var

         这里看到/dev/sda6               12G    53M    11G    1% /bak
         说明已经挂载成功了。到根目录“/”下可以查看到这个挂载好的data1。

转移数据
       其实一直做到这里都还只是准备工作,如果根分区下的数据不转移到这个虚拟硬盘中的话,还是会提示空间不足。下面是将/usr全部转移到虚拟硬盘中的过程(参考Linux人生的《Linux系统精华之一——挂载》),同样也可以转移其他目录:

        1、将/usr中的全部数据拷贝到data1(可以用mv一个一个拷贝,也可以用tar压缩之后一次拷贝,具体参见这两个命令的man)
        2、清空usr目录:
              # rm -r /usr
              # mkdir /usr
        3、卸载刚才挂上的虚拟硬盘,重新将它挂载到usr目录:
              # umount /dev/sdb1 /data1
              # mount /dev/sdb1 /usr
        4、# vi /etc/fstab ## 用vi修改/etc/fstab,使系统启动就可以自动挂载
         (点击“i”进入插入模式对文本内容进行修改,改好后点“Esc”,输入冒号“:”进入命令行模式,输入wq保存退出,具体操作可以参考vi常用指令)

        在内容中加上一行:
         /dev/sdb1                /usr                     ext3     defaults         1 2

        4、Ok,重新启动之后,可以查看现在的硬盘使用情况了:
              # df -h
              文件系统               容量   已用 可用 已用% 挂载点
             /dev/sda2              3.6G   1.3G   2.4G   35% /
                 udev                   125M   124K   125M    1% /dev
             /dev/sdb1              4.0G   2.3G   1.6G   60% /usr

------------------------------------------------------------------------------------------




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值