Linux KVM virtual machine add hard drive example

Source: Internet
Author: User
Tags cos file size

Method one, conventional way

Using a template may cause the virtual machine to have insufficient hard disk capacity and can be stored as data by adding a single hard disk.
1. First use the DD command to create an image file size 10G under the default path:

DD bs=1m count=10240 If=/dev/zero of=/var/lib/libvirt/images/guest1_data.img

You can also use the QEMU-IMG command to create a specific reference: CentOS 6.6 To install a KVM virtual machine.

2, use the Virsh Edit command to edit domain configuration file, in which the existing disk paragraph after adding the following:

<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, partition format and automatic mounting of hard disk
Using Fdisk-l to view the system's hard disk, depending on the last configuration, you should be able to see the device/dev/sdb, and then use the following command to partition

Fdisk/dev/sdb

And then format the

Mkfs.ext4/dev/sdb1

Assuming you mount the new hard drive to the/data directory, you can use the command

Mount/dev/sdb1/data

This way, if the machine needs to be reinstalled after reboot, you can add it to the fstab so that it can mount automatically when the system starts up.

/dev/sdb1/data EXT4 Defaults 0 0

Method Two, direct pull up partition


Ideas, the use of the method is to add a disk, there is a way to directly to the hard drive to stretch. A resize command is provided in the qemu-img, but the command is simply simple to pull up or reduce the size of a raw IMG image, which cannot be modified for the partition. I need to expand the partitions in them. It happens that the Red Hat provides this plug-in to time. This method is the use of Red Hat with the plug-in virt-resize to expand. The command first obtains the original partition information, as well as other file information. The new mirrors are then repartition and formatted. Finally, copy the files from the original image into the new file system and replace the original mirrors with the newly expanded mirrors. Because the actual use of the copy way, so he spent a long time, if it is a large mirror does not recommend using this method


Specific solutions.
Prerequisites for Installing the Libguestfs-tools Toolkit. Turn off virtual machines
#yum-y Install Libguestfs-tools


A, create a new large mirror
  #qemu-img create-f Raw test_extend.img 15g 
Note that the IMG size here is the total size you need to expand
B, using Virt-re Size to be pulled up partition
#virt-resize-expand  /dev/vda1/var/lib/libvirt/images/test.img/var/lib/libvirt/images/ TEST_NEW.IMG
Note that this is a long time, please be patient
C. Replace the original image with the new extended mirror
  #mv/var/lib/libvirt/images/test_new.img/var/lib/libvirt/images/test.img
D, start the virtual machine
& nbsp #virsh start Test
extend complete,


Advantages of Virt-resize: the ability to expand specific partitions in a virtual machine. and can expand Windows mirroring. There is no need to log in to the virtual machine for any action.
Disadvantage: When expanding the need to shut down. For large mirrors, the extension time is relatively long.


For LVM-formatted virtual machines
If your hard drive format supports LVM. Then your hard drive expansion will be much easier, LVM supports the online expansion of the hard drive. Quite convenient.
Method steps:
#lvcreate-L 40g-n lv_vm_test1 Volgroup
#virsh Attach-disk test/dev/mapper/volgroup/lv_vm_test VDB
Note that you may encounter permissions issues. Please modify the permissions of the/dev/mapper/volgroup/lv_vm_test before the virtual machine can be mounted.
To enter the virtual machine operation:
#pvcreate/dev/vdb
#vgextend Volgroup/dev/vdb
#vgs
#lvextend-L +100%free/dev/volgroup/lv_root
#resize2fs-P/dev/volgroup/lv_root
Advantages: Fast development time, support dynamic expansion.
Disadvantage, not suitable for the expansion of window.

Add:

1. Host increase physical hard disk and partition

Host computer, which hosts the KVM, manages all guest virtual machines. By adding a physical hard disk to the host and then giving it to the guest, we can achieve the effect of expanding the virtual machine's hard disk.

How to add physical hard disk to the computer and partition it, refer to: Multi-disk partition management Fdisk article

View the hard drive of the host machine


~ sudo fdisk-l

disk/dev/sda:299.4 GB, 299439751168 bytes
255 heads, Sectors/track, 36404 cylinders, total 584843264 sectors Units = sectors of 1 * A-bytes
sector size (logical/physical): bytes/512 bytes
I/o size (minimum/ Optimal): 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, Sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): 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+ Linux


/DEV/SDB6 2267025409 2581596160 157285376 Linux


/dev/sdb7 2581598209 2896168960 157285376 Linux


/DEV/SDB8 2896171009 3210741760 157285376 Linux


/DEV/SDB9 3210743809 3525314560 157285376 Linux


/DEV/SDB10 3525316609 3904897023 189790207+ 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


The following two tests are performed:


Add a file hard drive to guest via Virsh: Mirrored by file hard drive/disk/sdb6/c1d6.img


Add partition hard disk to guest via Virsh: Use partition hard disk directly/DEV/SDB5

2. Add file hard drive to guest via Virsh


Create a mirror image of a file hard disk


~ 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 June 8 16:37 c1d6.img
drwx------2 root root 16384 June 8 09:03 lost+found/
Load the hard drive through the Virsh administration tool


~ 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
C1 Running

#在这里我们要对c1进行硬盘扩容
Virsh # Edit C1

#找到硬盘配置 (original system hard drive)
<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>

#保存退出
Restart C1 virtual Machine


#请使用destroy命令, reboot and shutdown no use.
~ 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
In C1, make a hard drive search and partition


~ sudo fdisk-l

disk/dev/vda:42.9 GB, 42949672960 bytes
Heads, Sectors/track, 83220 cylinders, total 83886080 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): 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
Heads, Sectors/track, 20805 cylinders, total 20971520 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): 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, Sectors/track, 4667 cylinders, total 74989568 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): 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, Sectors/track, 522 cylinders, Total 8388608 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): bytes/512 bytes
Disk identifier:0x00000000

Disk/dev/mapper/u1210-swap_1 doesn ' t contain a valid partition table
/DEV/VDB has been identified and then partitioned, formatted, mounted, and used

Hard Disk partitions


~ sudo fdisk/dev/vdb

Command (M for help): P

disk/dev/vdb:161.1 GB, 161061274112 bytes
Heads, Sectors/track, 312076 cylinders, total 314572801 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): 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
The 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, Sectors/track, 616809 cylinders, total 20971520 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): bytes/512 bytes
Disk Identifier:0xf0432cd6

Device Boot Start End Blocks Id System
/DEV/VDB1 2048 20971519 10484736 Linux

Formatting


~ 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
The 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
Mount


~ 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


Use


/HOME/COS/HADOOPB directory, has been mounted on the/dev/vdb1 above, I can do anything in HADOOPB download.

3. Increase the partition hard disk for guest through Virsh


Directly using host's partitioned hard drive/DEV/SDB5, do a virtual machine C1 partition


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
Login Virtual C1 To view hard drive information


sudo fdisk-l
[sudo] password for cos:

disk/dev/vda:42.9 GB, 42949672960 bytes
Heads, Sectors/track, 83220 cylinders, total 83886080 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): 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, Sectors/track, 616809 cylinders, total 20971520 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): bytes/512 bytes
Disk Identifier:0xf0432cd6

Device Boot Start End Blocks Id System
/DEV/VDB1 2048 20971519 10484736 Linux

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

Device Boot Start End Blocks Id System
/DEV/VDC1 2048 314572800 157285376+ Linux

disk/dev/mapper/u1210-root:38.4 GB, 38394658816 bytes
255 heads, Sectors/track, 4667 cylinders, total 74989568 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): 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, Sectors/track, 522 cylinders, Total 8388608 sectors
Units = sectors of 1 * the bytes
Sector size (logical/physical): bytes/512 bytes
I/O size (minimum/optimal): bytes/512 bytes
Disk identifier:0x00000000

Disk/dev/mapper/u1210-swap_1 doesn ' t contain a valid partition table
has been properly identified
disk/dev/vdc:161.1 GB, 161061274112 bytes

Next, you can refer to a similar process above, partition, format, mount, use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.