linux中KVM 虚拟机增加硬盘例子

方法一、常规方式

采用模板的方式可能导致虚拟机的硬盘容量不够,可以通过单独增加一块硬盘来作为数据的存储。
1、首先使用dd命令在默认路径下创建一个大小为10G的映像文件:

dd bs=1M count=10240 if=/dev/zero of=/var/lib/libvirt/images/guest1_data.img

另外也可以使用 qemu-img 命令来创建,具体可以参考:centos 6.6 安装 KVM 虚拟机。

2、使用virsh edit 命令来编辑 domain 的配置文件,在其中已有的disk 段后增加如下内容:

<disk type='file' device='disk'>
  <driver name='qemu' type='raw' cache='none'/>
  <source file='/var/lib/libvirt/images/guest1_data.img'/>
  <target dev='hdb' bus='ide'/>
  <address type='drive' controller='0' bus='0' target='0' unit='1'/>
</disk>

3、分区格式化以及自动挂载硬盘
使用fdisk -l 查看系统的硬盘,根据上次配置,应该可以看到设备 /dev/sdb,然后使用如下命令进行分区

fdisk /dev/sdb

然后进行格式化

mkfs.ext4 /dev/sdb1

假设把新的硬盘mount 到 /data 目录,可以采用命令

mount /dev/sdb1 /data

这种方式如果机器重启后就需要重新mount,所以可以把他加入到fstab 中,这样系统启动时就能自动mount上去

/dev/sdb1   /data   ext4    defaults    0   0   

方法二、直接拉升分区

思 路,采用挂载的方法是而外添加了一块盘,有没有一种方法可以直接对硬盘进行拉伸。qemu-img中提供了一个resize的命令,但是该命令只是单纯的 简单了拉升或者缩小了一个raw的img镜像大小,对于其中的分区却不能进行修改。我需要对其中的的分区进行拓展。很碰巧红帽子提供这种插件可以时间。此方法是采用红帽子自带的插件virt-resize进行拓展。该命令首先是获取原来的分区信息,还 有其他文件信息。然后对新的镜像进行重新分区、格式化。最后拷贝原镜像中的文件到新文件系统中,再用新拓展的镜像替换原有镜像。因为实际采用copy的方 式,所以他花的时间比较长,如果是一个大镜像不建议使用此方法

具体解决方案。
前提安装libguestfs-tools工具包。关闭虚拟机
#yum -y install libguestfs-tools

A,新建一个大镜像
 #qemu-img create -f raw test_extend.img 15G 
注意,这里的img大小是你需要拓展的总大小
B,使用virt-resize进行拉升分区
#virt-resize —expand   /dev/vda1 /var/lib/libvirt/images/test.img /var/lib/libvirt/images/test_new.img
注意,此时间很长,请耐心等待
C.使用新扩展的镜像代替原镜像
 #mv /var/lib/libvirt/images/test_new.img /var/lib/libvirt/images/test.img
D,启动虚拟机
 #virsh start test
拓展完成,

virt-resize的优势:能对虚拟机中的特定的分区进行拓展。并且能够拓展windows镜像。不需要登录到虚拟机里面进行任何操作。
缺点:拓展的时候需要关机。对于大的镜像,拓展的时间比较长。
 
 
对于LVM格式的虚拟机
如果你的硬盘格式支持LVM。那么你的硬盘拓展将容易许多,LVM支持硬盘的在线扩容。相当方便。
方法步骤:
 #lvcreate -L 40G -n lv_vm_test1 VolGroup
 #virsh attach-disk test /dev/mapper/VolGroup/lv_vm_test vdb
 注意,有可能碰到权限问题。请先修改/dev/mapper/VolGroup/lv_vm_test的权限,是虚拟机可以挂载。
 进入虚拟机的操作:
#pvcreate /dev/vdb
#vgextend VolGroup /dev/vdb
#vgs 
#lvextend -l +100%FREE /dev/VolGroup/lv_root
#resize2fs -p /dev/VolGroup/lv_root
优势:拓展时间很快,支持动态扩展。
缺点,不适合window的拓展。

补充:

1. host增加物理硬盘并分区

HOST作为KVM的宿主计算机,管理所有GUEST虚拟机。我们通过给HOST增加物理硬盘,然后分给GUEST,从而实现给虚拟机硬盘扩容的效果。

如何给计算机增加物理硬盘并分区,请参考:多硬盘分区管理fdisk 文章

查看HOST机的硬盘

~ sudo fdisk -l

Disk /dev/sda: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders, total 584843264 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000efd7c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    97656831    48827392   82  Linux swap / Solaris
/dev/sda2        97656832   136718335    19530752   83  Linux
/dev/sda3       136718336   214843335    39062500   83  Linux
/dev/sda4   *   214843392   215037951       97280   83  Linux

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT
/dev/sdb2      1952448512  3904897023   976224256    5  Extended
/dev/sdb5      1952450560  2267023360   157286400+  83  Linux
/dev/sdb6      2267025409  2581596160   157285376   83  Linux
/dev/sdb7      2581598209  2896168960   157285376   83  Linux
/dev/sdb8      2896171009  3210741760   157285376   83  Linux
/dev/sdb9      3210743809  3525314560   157285376   83  Linux
/dev/sdb10     3525316609  3904897023   189790207+  83  Linux

~ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        37G  3.4G   32G  10% /
udev             24G  4.0K   24G   1% /dev
tmpfs           9.5G  1.1M  9.5G   1% /run
none            5.0M  8.0K  5.0M   1% /run/lock
none             24G  152K   24G   1% /run/shm
none            100M   28K  100M   1% /run/user
cgroup           24G     0   24G   0% /sys/fs/cgroup
/dev/sda2        19G  3.6G   14G  21% /home
/dev/sda4        92M   34M   54M  39% /boot
/dev/sdb1       931G  100G  832G  11% /disk/sdb1
/dev/sdb6       148G  188M  140G   1% /disk/sdb6
下面将进行两个测试:
通过virsh给guest增加文件硬盘:通过文件硬盘的镜像/disk/sdb6/c1d6.img
通过virsh给guest增加分区硬盘:直接使用分区硬盘/dev/sdb5

2. 通过virsh给guest增加文件硬盘

创建文件硬盘的镜像

~ cd /disk/sdb6/
~ sudo qemu-img create -f raw /disk/sdb6/c1d6.img 10G
Formatting '/disk/sdb6/c1d6.img', fmt=raw size=10737418240

~ ls -l
-rw-r--r-- 1 root root 10737418240 Jul  8 16:37 c1d6.img
drwx------ 2 root root       16384 Jul  8 09:03 lost+found/
通过virsh管理工具加载硬盘

~ sudo virsh
Welcome to virsh, the virtualization interactive terminal.
Type:  'help' for help with commands
       'quit' to quit

#查看系统内的虚拟机
virsh # list
Id Name State
----------------------------------------------------
5 server3 running
6 server4 running
7 d2 running
8 r1 running
9 server2 running
12 c1 running

#在这里我们要对c1进行硬盘扩容
virsh # edit c1

#找到硬盘配置(原来的系统硬盘)
<disk type='file' device='disk'>
<driver name='qemu' type='raw'/>
<source file='/disk/sdb1/c1.img'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</disk>

#增加文件硬盘,vdb
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/disk/sdb6/c1d6.img'/>
<target dev='vdb' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>

#保存退出
重启c1虚拟机

#请使用destroy命令,reboot和shutdown不管用。
~ virsh # destroy c1
Domain c1 destroyed

#list找不到c1
~ virsh # list
 Id    Name                           State
----------------------------------------------------
 5     server3                        running
 6     server4                        running
 7     d2                             running
 8     r1                             running
 9     server2                        running

#启动虚拟机c1
~ virsh # start c1
Domain c1 started

#进入虚拟机c1
~ console c1
在c1中,进行硬盘查检并分区

~ sudo fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes
16 heads, 63 sectors/track, 83220 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000516aa

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          501758    83884031    41691137    5  Extended
/dev/vda5          501760    83884031    41691136   8e  Linux LVM

Disk /dev/vdb: 10.7 GB, 10737418240 bytes
16 heads, 63 sectors/track, 20805 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

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

Disk /dev/mapper/u1210-root: 38.4 GB, 38394658816 bytes
255 heads, 63 sectors/track, 4667 cylinders, total 74989568 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/u1210-root doesn't contain a valid partition table

Disk /dev/mapper/u1210-swap_1: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders, total 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/u1210-swap_1 doesn't contain a valid partition table
/dev/vdb已经被识别,接下来 分区,格式化,挂载,使用

硬盘分区

~ sudo fdisk /dev/vdb

Command (m for help): p

Disk /dev/vdb: 161.1 GB, 161061274112 bytes
16 heads, 63 sectors/track, 312076 cylinders, total 314572801 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x3b49c6a0

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-314572800, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-314572800, default 314572800):
Using default value 314572800

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

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

#分区生效
~ sudo partprobe

~ sudo fdisk -l
Disk /dev/vdb: 10.7 GB, 10737418240 bytes
2 heads, 17 sectors/track, 616809 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf0432cd6

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    20971519    10484736   83  Linux

格式化

~ sudo mkfs -t ext4 /dev/vdb1
mke2fs 1.42.5 (29-Jul-2012)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
9830400 inodes, 39321344 blocks
1966067 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
1200 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

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

~ sudo mount /dev/vdb1 /home/cos/hadoopb

~ df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/u1210-root   36G  1.1G   33G   4% /
udev                    2.0G  4.0K  2.0G   1% /dev
tmpfs                   791M  232K  791M   1% /run
none                    5.0M     0  5.0M   0% /run/lock
none                    2.0G     0  2.0G   0% /run/shm
none                    100M     0  100M   0% /run/user
/dev/vda1               228M   29M  188M  14% /boot
/dev/vdb1               9.9G  151M  9.2G   2% /home/cos/hadoopb
使用
/home/cos/hadoopb的目录,已经挂载到了/dev/vdb1上面,我可以在hadoopb下载做任何的操作。

3. 通过virsh给guest增加分区硬盘

直接使用HOST的分区硬盘/dev/sdb5,做个虚拟机c1的分区

virsh # edit c1

#新增新硬盘vbc
<disk type='block' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source dev='/dev/sdb5'/>
<target dev='vbc' bus='virtio'/>
</disk>

virsh # destroy c1
Domain c1 destroyed

virsh # start c1
Domain c1 started

virsh # console c1
登陆虚拟c1,查看硬盘信息

sudo fdisk -l
[sudo] password for cos:

Disk /dev/vda: 42.9 GB, 42949672960 bytes
16 heads, 63 sectors/track, 83220 cylinders, total 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000516aa

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      499711      248832   83  Linux
/dev/vda2          501758    83884031    41691137    5  Extended
/dev/vda5          501760    83884031    41691136   8e  Linux LVM

Disk /dev/vdb: 10.7 GB, 10737418240 bytes
2 heads, 17 sectors/track, 616809 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf0432cd6

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    20971519    10484736   83  Linux

Disk /dev/vdc: 161.1 GB, 161061274112 bytes
4 heads, 4 sectors/track, 19660800 cylinders, total 314572801 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x3b49c6a0

   Device Boot      Start         End      Blocks   Id  System
/dev/vdc1            2048   314572800   157285376+  83  Linux

Disk /dev/mapper/u1210-root: 38.4 GB, 38394658816 bytes
255 heads, 63 sectors/track, 4667 cylinders, total 74989568 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/u1210-root doesn't contain a valid partition table

Disk /dev/mapper/u1210-swap_1: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders, total 8388608 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/mapper/u1210-swap_1 doesn't contain a valid partition table
已经被正确识别
Disk /dev/vdc: 161.1 GB, 161061274112 bytes

接下来,可以参考上面类似的过程,分区,格式化,挂载,使用

时间: 2024-09-09 17:05:52

linux中KVM 虚拟机增加硬盘例子的相关文章

Linux系统挂载未分配硬盘例子

***前言***本文并非教程,仅为参照小夜教程的一个操作过程记录,给大家做个参考.   首先,SSH使用root用户登陆,检查下当前硬盘空间使用状况:  代码如下 复制代码 fdisk –l     如上图我买的这个VPS硬盘为15GB,已分配10GB左右,剩余未分配约5GB.然后我们看看目前空间的使用情况:    代码如下 复制代码 df –hal     下面,我们要开始增加分区的操作了.首先如下命令:    代码如下 复制代码 fdisk /dev/xvdb     接下来,如上图中依次输

linux中ssh公钥认证配置例子

在两台linux主机上由于环境的需要,经常要配置两台主机之间免密码登录,这就要用到key认证,也就是所谓的公私钥认证.便于理解,我这里指定两台主机为 A 和 B .如果A主机想免密码登录到B主机上,则A主机上存放私钥,B 主机上存放公钥.通过ssh-keygen 命令生成的两个文件为:公钥文件 ~/.ssh/id_rsa.pub: 私钥文件 ~/.ssh/id_rsa .而B主机上存放公钥时,需要将id_rsa.pub的内容存放到~/.ssh/authorized_keys 文件内,并且保证权限

linux中XenServer虚拟机扩容LVM磁盘分区

说明:XenServer里面安装的虚拟机,分区的时候采用的是LVM磁盘分区 需求:现在需要扩容虚拟机根分区/ 具体操作: 一.使用XenCenter增大虚拟机磁盘容量,如下图所示 增大当前磁盘到50GB 注意:必须在虚拟机关机状态下进行 二.查看硬盘容量 df -h #可以看到此时根分区还是12G,没有变化 fdisk -l #已经可以看到整个磁盘容量变为53.6GB了 三.创建新的磁盘分区 fdisk /dev/xvda #对磁盘/dev/xvda进行操作 p #查看当前分区 n #创建新分区

linux中挂载数据盘的例子

  最近给Linux服务器升级了,原因在于之前的配置太低了,以至于数据库无法访问.一升级之后,发现分配了一个50G的数据盘,而且需要自己挂载在系统上,所以只好找资料来挂载数据库,并且把网站数据转移到此数据盘上.步骤如下:   说明:   1. 适用系统:Linux(Redhat , CentOS,Debian,Ubuntu)   2. Linux的云服务器数据盘未做分区和格式化   1.查看数据盘   在没有分区和格式化数据盘之前,使用 "df –h"命令,是无法看到数据盘的,可以使用

linux中openshift搭建与使用例子

本篇所在总结的内容本来是一个过时的东西,不过由于当下红帽考试仍使用的这个旧的版本,这里还是简单总结下openshift2.x的配置和使用.openshift是红帽的paas 应用,想要和cloudfoundry之类的paas类产品竞争,不过从当前市场占用份额上来看,cloudfoundry处于行业主宰者,龙头地位.openshit2.x是基于红帽自己搞的gear技术---可以看做是类似沙箱的一种技术,和docker一样也是基于lxc基础上做的封装.当前openshitf3.x 版本较opensh

linux中查找php木马程序例子

1.服务器本身的安全 安装denyhost,防止SSH的暴力破解,具体安装方法参考<denyhost防止SSH暴力破解,保护你的linux>一文. 另外对系统的一些重要文件添加一个i权限 比如:  代码如下 复制代码 # chattr +i /etc/passwd # chattr +i /etc/group # chattr +i /etc/shadow # chattr +i /etc/gshadow # chattr +i /etc/ssh/sshd_config 2.nginx和php

linux中为PHP增加mongodb扩展模块

Linux下增加扩展模块,首先从http://pecl.php.net/package/mongo下载源码包,这里下载的是mongo-1.2.12.tgz,接下来执行安装步骤 tar zxvf mongo-1.2.12.tgz cd mongo-1.2.12 准备编译环境/usr/local/webserver/php/bin/phpize ./configure --with-php-config=/usr/local/webserver/php/bin/php-config make mak

Linux中imagemagick图片批量压缩例子

 代码如下 复制代码 sudo apt-get install imagemagick       其它linux有对应的软件安装命令,以下有我写的两个脚本,分别对图片进行批量格式以及压缩操作:     图片格式转换,这里的示例是将[bB][mM][pP](所有BMP文件)格式的文件,转换为占空间小的jpg文件:  代码如下 复制代码 #!/bin/sh for img in `find ./ -name "*.[bB][mM][pP]"`; do         #change up

linux中bash读写文件的例子

#!/bin/bash   #读取文件内容到变量中 filecontent=`cat ~/file.txt`    `是TAB键上那个 echo $filecontent   #取得文件内容的每一行 for fileline in $filecontent   do     echo $fileline   done   #写内容到文件中 echo $filecontent >> test.txt bash内置Unix命令 .:执行当前进程环境中的程序.同source. . file:dotU