How to Add Memory, vCPU, Hard Disk to Linux KVM Virtual Machine

n this example, let us increase the memory of myRHELVM1’s VM from 2GB to 4GB.

First, shutdown the VM using virsh shutdown as shown below:

# virsh shutdown myRHELVM1
Domain myRHELVM1 is being shutdown

Next, edit the VM using virsh edit:

# virsh edit myRHELVM1

Look for the below line and change the value for memory to the following. In my example, earlier it was 2097152:

<memory unit='KiB'>4194304</memory>

Please note that the above value is in KB. After making the change, save and exit:

# virsh edit myRHELVM1
Domain myRHELVM1 XML configuration edited.

Restart the VM with the updated configuration file. Now you will see the max memory increased from 2G to 4G.

You can now dynamically modify the VM memory upto the 4G max limit.

Create the Domain XML file using virsh create

# virsh create /etc/libvirt/qemu/myRHELVM1.xml
Domain myRHELVM1 created from /etc/libvirt/qemu/myRHELVM1.xml

View the available Memory for this domain. As you see below, even though the maximum available memory is 4GB, this domain only has 2GB (Used memory).

# virsh dominfo myRHELVM1 | grep memory
Max memory:     4194304 KiB
Used memory:    2097152 KiB

Set the memory for this domain to 4GB using virsh setmem as shown below:

# virsh setmem myRHELVM1 4194304

Now, the following indicates that we’ve allocated 4GB (Used memory) to this domain.

# virsh dominfo myRHELVM1 | grep memory
Max memory:     4194304 KiB
Used memory:    4194304 KiB

2. Add VCPU to VM

To increase the virtual CPU that is allocated to the VM, do virsh edit, and change the vcpu parameter as explained below.

In this example, let us increase the memory of myRHELVM1’s VM from 2GB to 4GB.

First, shutdown the VM using virsh shutdown as shown below:

# virsh shutdown myRHELVM1
Domain myRHELVM1 is being shutdown

Next, edit the VM using virsh edit:

# virsh edit myRHELVM1

Look for the below line and change the value for vcpu to the following. In my example, earlier it was 2.

<vcpu placement='static'>4</vcpu>

Create the Domain XML file using virsh create

# virsh create /etc/libvirt/qemu/myRHELVM1.xml
Domain myRHELVM1 created from /etc/libvirt/qemu/myRHELVM1.xml

View the virtual CPUs allocated to this domain as shown below. This indicates that we’ve increased the vCPU from 2 to 4.

# virsh dominfo myRHELVM1 | grep -i cpu
CPU(s):         4
CPU time:       21.0s

3. Add Disk to VM

In this example, we have only two virtual disks (vda1 and vda2) on this VM.

# fdisk -l | grep vd
Disk /dev/vda: 10.7 GB, 10737418240 bytes
/dev/vda1   *           3        1018      512000   83  Linux
/dev/vda2            1018       20806     9972736   8e  Linux LVM

There are two steps involved in creating and attaching a new storage device to Linux KVM guest VM:

  • First, create a virtual disk image
  • Attach the virtual disk image to the VM

Let us create one more virtual disk and attach it to our VM. For this, we first need to create a disk image file using qemu-img create command as shown below.

In the following example, we are creating a virtual disk image with 7GB of size. The disk images are typically located under /var/lib/libvirt/images/ directory.

# cd /var/lib/libvirt/images/

# qemu-img create -f raw myRHELVM1-disk2.img 7G
Formatting 'myRHELVM1-disk2.img', fmt=raw size=7516192768

To attach the newly created disk image, use the virsh attach-disk command as shown below:

# virsh attach-disk myRHELVM1 --source /var/lib/libvirt/images/myRHELVM1-disk2.img --target vdb --persistent
Disk attached successfully

The above virsh attach-disk command has the following parameters:

  • myRHELVM1 The name of the VM
  • –source The full path of the source disk image. This is the one that we created using qemu-image command above. i.e: myRHELVM1-disk2.img
  • –target This is the device mount point. In this example, we want to attach the given disk image as /dev/vdb. Please note that we don’t really need to specify /dev. It is enough if you just specify vdb.
  • –persistent indicates that the disk that attached to the VM will be persistent.

As you see below, the new /dev/vdb is now available on the VM.

# fdisk -l | grep vd
Disk /dev/vda: 10.7 GB, 10737418240 bytes
/dev/vda1   *           3        1018      512000   83  Linux
/dev/vda2            1018       20806     9972736   8e  Linux LVM
Disk /dev/vdb: 7516 MB, 7516192768 bytes

Now, you can partition the /dev/vdb device, and create multiple partitions /dev/vdb1, /dev/vdb2, etc, and mount it to the VM. Use fdisk to create the partitions as we explained earlier.

Similarly to detach a disk from the guest VM, you can use the below command. But be careful to specify the correct vd* otherwise you may end-up removing wrong device.

# virsh detach-disk myRHELVM1 vdb
Disk detached successfully

4. Save Virtual Machine Configuration

If you make lot of changes to your VM, it is recommended that you save the configurations.

Use the virsh dumpxml file to take a backup and save the configuration information of your VM as shown below.

# virsh dumpxml myRHELVM1 > myrhelvm1.xml

# ls myrhelvm1.xml
myrhelvm1.xml

Once you have the configuration file in the XML format, you can always recreate your guest VM from this XML file, using virsh create command as shown below:

virsh create myrhelvm1.xml

5. Delete KVM Virtual Machine

If you’ve created multiple VMs for testing purpose, and like to delete them, you should do the following three steps:

  • Shutdown the VM
  • Destroy the VM (and undefine it)
  • Remove the Disk Image File

In this example, let us delete myRHELVM2 VM. First, shutdown this VM:

# virsh shutdown myRHELVM2
Domain myRHELVM2 is being shutdown

Next, destory this VM as shown below:

# virsh destroy myRHELVM2
Domain myRHELVM2 destroyed

Apart from destroying it, you should also undefine the VM as shown below:

# virsh undefine myRHELVM2
Domain myRHELVM2 has been undefined

Finally, remove any disk image file that you’ve created for this VM from the /var/lib/libvirt/images directory:
Now you can remove the disk img file under /var/lib/libvirt/images

rm /var/lib/libvirt/images/myRHELVM2-disk1.img
rm /var/lib/libvirt/images/myRHELVM2-disk2.img

How to run cloud-init manually

1)
$rm -rf /var/lib/cloud/*
OR
$rm -rf /var/lib/cloud/sem/* /var/lib/cloud/instance /var/lib/cloud/instances/*
2)
$cloud-init init
$cloud-init modules -m final

3)
$tail -f /var/log/cloud-init.log
$tail -f /var/log/cloud-init-output.log

OR

The commands have been updated so to re-run you need to clean out the existing config:

sudo cloud-init clean

Then re-run it using the init directive:

sudo cloud-init init

Beware: things like ssh host keys maybe regenerated.

Linux KVM – How to Resize Virtual disk on the fly?

  1. Login to the Guest VM (UAKVM2) and identify which disk require to resize.
[root@UA-KVM1 ~]# df -h /orastage
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdc       1014M   33M  982M   4% /orastage
[root@UA-KVM1 ~]# mount -v |grep /orastage
/dev/vdc on /orastage type xfs (rw,relatime,attr2,inode64,noquota)
[root@UA-KVM1 ~]#
[root@UA-KVM1 ~]# fdisk -l /dev/vdc

Disk /dev/vdc: 1073 MB, 1073741824 bytes, 2097152 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

[root@UA-KVM1 ~]#

 

2. Login to the KVM hypervisor which hosts the VM

3. Identify the virtual disk mapping for the KVM guest.

[root@UA-HA ~]# virsh domblklist UAKVM2 --details
Type       Device     Target     Source
------------------------------------------------
file       disk       vda        /var/lib/libvirt/images/UAKVM2.qcow2
block      disk       vdb        /dev/sdb
file       disk       vdc        /var/lib/libvirt/images/UAKVM2.disk2.qcow2
block      cdrom      hda        -

[root@UA-HA ~]#

4. Refresh the KVM storage pool.

[root@UA-HA ~]# virsh pool-list
 Name                 State      Autostart
-------------------------------------------
 default              active     yes
 [root@UA-HA ~]#
[root@UA-HA ~]# virsh pool-refresh default
Pool default refreshed
[root@UA-HA ~]#

5. List the virtual disks using virsh-vol list command. (vdc = UAKVM2.disk2.qcow2)

[root@UA-HA ~]# virsh vol-list  default
 Name                 Path
------------------------------------------------------------------------------
 UAKVM2.disk2.qcow2   /var/lib/libvirt/images/UAKVM2.disk2.qcow2
 UAKVM2.disk3.img     /var/lib/libvirt/images/UAKVM2.disk3.img
 UAKVM2.disk4.img     /var/lib/libvirt/images/UAKVM2.disk4.img
 UAKVM2.qcow2         /var/lib/libvirt/images/UAKVM2.qcow2
[root@UA-HA ~]#

6. Use “qemu-monitor” to list the allocated block devices to “UAKVM2” domain.

[root@UA-HA ~]# virsh qemu-monitor-command UAKVM2 --hmp "info block"
drive-virtio-disk0: removable=0 io-status=ok file=/var/lib/libvirt/images/UAKVM2.qcow2 ro=0 drv=qcow2 encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-virtio-disk1: removable=0 io-status=ok file=/dev/sdb ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-virtio-disk2: removable=0 io-status=ok file=/var/lib/libvirt/images/UAKVM2.disk2.qcow2 ro=0 drv=raw encrypted=0 bps=0 bps_rd=0 bps_wr=0 iops=0 iops_rd=0 iops_wr=0
drive-ide0-0-0: removable=1 locked=0 tray-open=0 io-status=ok [not inserted]
[root@UA-HA ~]#

From the above command output, we can see that virtual disk “UAKVM2.disk2.qcow2” is mapped to drive-virtio-disk2.

7. Increase the virtual disk size and intimate the virtio driver about the changes. (Do not reduce the disk size !!!)

[root@UA-HA images]# virsh qemu-monitor-command UAKVM2 --hmp "block_resize drive-virtio-disk2 2G"
[root@UA-HA images]#

8. Login to the KVM guest – UAKVM2 and check the “vdc” disk size.

[root@UA-KVM1 ~]# fdisk -l /dev/vdc

Disk /dev/vdc: 2147 MB, 2147483648 bytes, 4194304 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

[root@UA-KVM1 ~]#

9. Extend the filesystem. My filesystem type is XFS.

[root@UA-KVM1 ~]# df -h /orastage
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdc       1014M   33M  982M   4% /orastage
[root@UA-KVM1 ~]# mount -v |grep /orastage
/dev/vdc on /orastage type xfs (rw,relatime,attr2,inode64,noquota)
[root@UA-KVM1 ~]#
[root@UA-KVM1 ~]# xfs_growfs /orastage/
meta-data=/dev/vdc               isize=256    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 262144 to 1310720
[root@UA-KVM1 ~]#
[root@UA-KVM1 ~]# df -h /orastage/
Filesystem      Size  Used Avail Use% Mounted on
/dev/vdc        2.0G   33M  2.0G   1% /orastage
[root@UA-KVM1 ~]#

We have successfully resized virtual size and intimated to virtio driver about the changes. No specific instructions required for the VM to see the new disk size.

virt tools

The libguestfs project has a number of other useful tools, including:

  • virt-edit for editing a file inside of an image.
  • virt-df for displaying free space inside of an image.
  • virt-resize for resizing an image.
  • virt-sysprep for preparing an image for distribution (for example, delete SSH host keys, remove MAC address info, or remove user accounts).
  • virt-sparsify for making an image sparse.
  • virt-p2v for converting a physical machine to an image that runs on KVM.
  • virt-v2v for converting Xen and VMware images to KVM images.

Modify a single file inside of an image

This example shows how to use virt-edit to modify a file. The command can take either a filename as an argument with the -a flag, or a domain name as an argument with the -d flag. The following examples shows how to use this to modify the /etc/shadow file in instance with libvirt domain name instance-000000e1 that is currently running:

# virsh shutdown instance-000000e1
# virt-edit -d instance-000000e1 /etc/ssh/sshd_config
# virsh start instance-000000e1

Resize an image

Here is an example of how to use virt-resize to resize an image. Assume we have a 16 GB Windows image in qcow2 format that we want to resize to 50 GB.

  1. First, we use virt-filesystems to identify the partitions:
    # virt-filesystems --long --parts --blkdevs -h -a /data/images/win2012.qcow2
    Name       Type       MBR  Size  Parent
    /dev/sda1  partition  07   350M  /dev/sda
    /dev/sda2  partition  07   16G   /dev/sda
    /dev/sda   device     -    16G   -
    
  2. In this case, it is the /dev/sda2 partition that we want to resize. We create a new qcow2 image and use the virt-resize command to write a resized copy of the original into the new image:
    # qemu-img create -f qcow2 /data/images/win2012-50gb.qcow2 50G
    # virt-resize --expand /dev/sda2 /data/images/win2012.qcow2 \
      /data/images/win2012-50gb.qcow2
    Examining /data/images/win2012.qcow2 ...
    **********
    
    Summary of changes:
    
    /dev/sda1: This partition will be left alone.
    
    /dev/sda2: This partition will be resized from 15.7G to 49.7G.  The
        filesystem ntfs on /dev/sda2 will be expanded using the
        'ntfsresize' method.
    
    **********
    Setting up initial partition table on /data/images/win2012-50gb.qcow2 ...
    Copying /dev/sda1 ...
     100% [                                                                 ] 00:00
    Copying /dev/sda2 ...
     100% [                                                                 ] 00:00
    Expanding /dev/sda2 using the 'ntfsresize' method ...
    
    Resize operation completed with no errors. Before deleting the old
    disk, carefully check that the resized disk boots and works correctly.

     

 

Modify a virtual machine image with guestfish

Guestfish is a shell and command-line tool for examining and modifying virtual machine filesystems. It uses libguestfs and exposes all of the functionality of the guestfs API.

Sometimes you must modify a virtual machine image to remove any traces of the MAC address that was assigned to the virtual network interface card when the image was first created. This is because the MAC address is different when the virtual machine images boots. This example shows how to use the guestfish to remove references to the old MAC address by deleting the/etc/udev/rules.d/70-persistent-net.rules file and removing the HWADDR line from the /etc/sysconfig/network-scripts/ifcfg-eth0 file.

Assume that you have a CentOS qcow2 image called centos63_desktop.img. Mount the image in read-write mode as root, as follows:

# guestfish --rw -a centos.img

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing virtual machine filesystems.

Type: 'help' for help on commands
'man' to read the manual
'quit' to quit the shell

><fs>

This starts a guestfish session.

Notethe guestfish prompt looks like a fish: ><fs>.

We must first use the run command at the guestfish prompt before we can do anything else. This will launch a virtual machine, which will be used to perform all of the file manipulations.

><fs> run
  1. We can now view the file systems in the image using the list-filesystems command:
    ><fs> list-filesystems
    /dev/vda1: ext4
    /dev/vg_centosbase/lv_root: ext4
    /dev/vg_centosbase/lv_swap: swap
    
  2. We need to mount the logical volume that contains the root partition:
    ><fs> mount /dev/vg_centosbase/lv_root /
    
  3. Next, we want to delete a file. We can use the rm guestfish command, which works the same way it does in a traditional shell.
    ><fs> rm /etc/udev/rules.d/70-persistent-net.rules
    
  4. We want to edit the ifcfg-eth0 file to remove the HWADDR line. The edit command will copy the file to the host, invoke your editor, and then copy the file back.
    ><fs> edit /etc/sysconfig/network-scripts/ifcfg-eth0
    
  5. If you want to modify this image to load the 8021q kernel at boot time, you must create an executable script in the/etc/sysconfig/modules/ directory. You can use the touch guestfish command to create an empty file, the edit command to edit it, and the chmod command to make it executable.
    ><fs> touch /etc/sysconfig/modules/8021q.modules
    ><fs> edit /etc/sysconfig/modules/8021q.modules
    
  6. We add the following line to the file and save it:
    modprobe 8021q
    
  7. Then we set to executable:
    ><fs> chmod 0755 /etc/sysconfig/modules/8021q.modules
    
  8. We are done, so we can exit using the exit command:
    ><fs> exit
    

Example 2: Add passwordless sudo to a user:

guestfish --rw -a centos.img
><fs> run
><fs> list-filesystems
/dev/sda1: ext4
><fs> mount /dev/sda1 /
><fs> touch /etc/sudoers.d/user1-sudo
><fs> vi /etc/sudoers.d/useer1-sudo
><fs> quit

This will add the sudoers file and enable passwordless sudo for user1

virt-customize

Virt-customize can customize a virtual machine (disk image) by installing packages, editing configuration files, and so on.

Virt-customize modifies the guest or disk image in place. The guest must be shut down. If you want to preserve the existing contents of the guest, you must snapshot, copy or clone the disk first.

Customization options

–append-line FILE:LINE
Append a single line of text to the FILE. If the file does not already end with a newline, then one is added before the appended line. Also a newline is added to the end of the LINE string automatically.

For example (assuming ordinary shell quoting) this command:

 --append-line '/etc/hosts:10.0.0.1 foo'

will add either 10.0.0.1 foo⏎ or ⏎10.0.0.1 foo⏎ to the file, the latter only if the existing file does not already end with a newline.

 represents a newline character, which is guessed by looking at the existing content of the file, so this command does the right thing for files using Unix or Windows line endings. It also works for empty or non-existent files.

To insert several lines, use the same option several times:

 --append-line '/etc/hosts:10.0.0.1 foo'
 --append-line '/etc/hosts:10.0.0.2 bar'

To insert a blank line before the appended line, do:

 --append-line '/etc/hosts:'
 --append-line '/etc/hosts:10.0.0.1 foo'
–chmod PERMISSIONS:FILE
Change the permissions of FILE to PERMISSIONS.

NotePERMISSIONS by default would be decimal, unless you prefix it with 0to get octal, ie. use 0700 not 700.

–commands-from-file FILENAME
Read the customize commands from a file, one (and its arguments) each line.

Each line contains a single customization command and its arguments, for example:

 delete /some/file
 install some-package
 password some-user:password:its-new-password

Empty lines are ignored, and lines starting with # are comments and are ignored as well. Furthermore, arguments can be spread across multiple lines, by adding a \ (continuation character) at the of a line, for example

 edit /some/file:\
   s/^OPT=.*/OPT=ok/

The commands are handled in the same order as they are in the file, as if they were specified as –delete /some/file on the command line.

–copy SOURCE:DEST
Copy files or directories recursively inside the guest.

Wildcards cannot be used.

–copy-in LOCALPATH:REMOTEDIR
Copy local files or directories recursively into the disk image, placing them in the directory REMOTEDIR (which must exist).

Wildcards cannot be used.

–delete PATH
Delete a file from the guest. Or delete a directory (and all its contents, recursively).

You can use shell glob characters in the specified path. Be careful to escape glob characters from the host shell, if that is required. For example:

 virt-customize --delete '/var/log/*.log'.

See also: –upload–scrub.

–edit FILE:EXPR
Edit FILE using the Perl expression EXPR.

Be careful to properly quote the expression to prevent it from being altered by the shell.

Note that this option is only available when Perl 5 is installed.

See “NON-INTERACTIVE EDITING” in virt-edit(1).

–firstboot SCRIPT
Install SCRIPT inside the guest, so that when the guest first boots up, the script runs (as root, late in the boot process).

The script is automatically chmod +x after installation in the guest.

The alternative version –firstboot-command is the same, but it conveniently wraps the command up in a single line script for you.

You can have multiple –firstboot options. They run in the same order that they appear on the command line.

Please take a look at “FIRST BOOT SCRIPTS” in virt-builder(1) for more information and caveats about the first boot scripts.

See also –run.

–firstboot-command ‘CMD+ARGS’
Run command (and arguments) inside the guest when the guest first boots up (as root, late in the boot process).

You can have multiple –firstboot options. They run in the same order that they appear on the command line.

Please take a look at “FIRST BOOT SCRIPTS” in virt-builder(1) for more information and caveats about the first boot scripts.

See also –run.

–firstboot-install PKG,PKG..
Install the named packages (a comma-separated list). These are installed when the guest first boots using the guest’s package manager (eg. apt, yum, etc.) and the guest’s network connection.

For an overview on the different ways to install packages, see “INSTALLING PACKAGES” in virt-builder(1).

–hostname HOSTNAME
Set the hostname of the guest to HOSTNAME. You can use a dotted hostname.domainname (FQDN) if you want.

–install PKG,PKG..
Install the named packages (a comma-separated list). These are installed during the image build using the guest’s package manager (eg. apt, yum, etc.) and the host’s network connection.

For an overview on the different ways to install packages, see “INSTALLING PACKAGES” in virt-builder(1).

See also –update–uninstall.

–link TARGET:LINK[:LINK..]
Create symbolic link(s) in the guest, starting at LINK and pointing at TARGET.

–mkdir DIR
Create a directory in the guest.

This uses mkdir -p so any intermediate directories are created, and it also works if the directory already exists.

–move SOURCE:DEST
Move files or directories inside the guest.

Wildcards cannot be used.

–no-logfile
Scrub builder.log (log file from build commands) from the image after building is complete. If you don’t want to reveal precisely how the image was built, use this option.

See also: “LOG FILE”.

–password USER:SELECTOR
Set the password for USER. (Note this option does not create the user account).

See “USERS AND PASSWORDS” in virt-builder(1) for the format of the SELECTOR field, and also how to set up user accounts.

–password-crypto md5|sha256|sha512
When the virt tools change or set a password in the guest, this option sets the password encryption of that password to md5sha256 or sha512.

sha256 and sha512 require glibc ≥ 2.7 (check crypt(3) inside the guest).

md5 will work with relatively old Linux guests (eg. RHEL 3), but is not secure against modern attacks.

The default is sha512 unless libguestfs detects an old guest that didn’t have support for SHA-512, in which case it will use md5. You can override libguestfs by specifying this option.

Note this does not change the default password encryption used by the guest when you create new user accounts inside the guest. If you want to do that, then you should use the –edit option to modify /etc/sysconfig/authconfig (Fedora, RHEL) or /etc/pam.d/common-password (Debian, Ubuntu).

–root-password SELECTOR
Set the root password.

See “USERS AND PASSWORDS” in virt-builder(1) for the format of the SELECTOR field, and also how to set up user accounts.

Note: In virt-builder, if you don’t set –root-password then the guest is given a random root password.

–run SCRIPT
Run the shell script (or any program) called SCRIPT on the disk image. The script runs virtualized inside a small appliance, chrooted into the guest filesystem.

The script is automatically chmod +x.

If libguestfs supports it then a limited network connection is available but it only allows outgoing network connections. You can also attach data disks (eg. ISO files) as another way to provide data (eg. software packages) to the script without needing a network connection (–attach). You can also upload data files (–upload).

You can have multiple –run options. They run in the same order that they appear on the command line.

See also: –firstboot–attach–upload.

–run-command ‘CMD+ARGS’
Run the command and arguments on the disk image. The command runs virtualized inside a small appliance, chrooted into the guest filesystem.

If libguestfs supports it then a limited network connection is available but it only allows outgoing network connections. You can also attach data disks (eg. ISO files) as another way to provide data (eg. software packages) to the script without needing a network connection (–attach). You can also upload data files (–upload).

You can have multiple –run-command options. They run in the same order that they appear on the command line.

See also: –firstboot–attach–upload.

–scrub FILE
Scrub a file from the guest. This is like –delete except that:

  • It scrubs the data so a guest could not recover it.
  • It cannot delete directories, only regular files.
–selinux-relabel
Relabel files in the guest so that they have the correct SELinux label.

This will attempt to relabel files immediately, but if the operation fails this will instead touch /.autorelabel on the image to schedule a relabel operation for the next time the image boots.

You should only use this option for guests which support SELinux.

–sm-attach SELECTOR
Attach to a pool using subscription-manager.

See “SUBSCRIPTION-MANAGER” in virt-builder(1) for the format of the SELECTOR field.

–sm-credentials SELECTOR
Set the credentials for subscription-manager.

See “SUBSCRIPTION-MANAGER” in virt-builder(1) for the format of the SELECTOR field.

–sm-register
Register the guest using subscription-manager.

This requires credentials being set using –sm-credentials.

–sm-remove
Remove all the subscriptions from the guest using subscription-manager.

–sm-unregister
Unregister the guest using subscription-manager.

–ssh-inject USER[:SELECTOR]
Inject an ssh key so the given USER will be able to log in over ssh without supplying a password. The USER must exist already in the guest.

See “SSH KEYS” in virt-builder(1) for the format of the SELECTOR field.

You can have multiple –ssh-inject options, for different users and also for more keys for each user.

–timezone TIMEZONE
Set the default timezone of the guest to TIMEZONE. Use a location string like Europe/London

–touch FILE
This command performs a touch(1)-like operation on FILE.

–truncate FILE
This command truncates FILE to a zero-length file. The file must exist already.

–truncate-recursive PATH
This command recursively truncates all files under PATH to zero-length.

–uninstall PKG,PKG..
Uninstall the named packages (a comma-separated list). These are removed during the image build using the guest’s package manager (eg. apt, yum, etc.). Dependent packages may also need to be uninstalled to satisfy the request.

See also –install–update.

–update
Do the equivalent of yum updateapt-get upgrade, or whatever command is required to update the packages already installed in the template to their latest versions.

See also –install–uninstall.

–upload FILE:DEST
Upload local file FILE to destination DEST in the disk image. File owner and permissions from the original are preserved, so you should set them to what you want them to be in the disk image.

DEST could be the final filename. This can be used to rename the file on upload.

If DEST is a directory name (which must already exist in the guest) then the file is uploaded into that directory, and it keeps the same name as on the local filesystem.

See also: –mkdir–delete–scrub.

–write FILE:CONTENT
Write CONTENT to FILE.

Install KVM Hypervisor on CentOS 7.x and RHEL 7.x

KVM is an open source hardware virtualization software through which we can create and run multiple Linux based and windows based virtual machines simultaneously. KVM is known as Kernel based Virtual Machine because when we install KVM package then KVM module is loaded into the current kernel and turns our Linux machine into a hypervisor.

In this post first we will demonstrate how we can install KVM hypervisor on CentOS 7.x and RHEL 7.x and then we will try to install virtual machines.

Before proceeding KVM installation, let’s check whether your system’s CPU supports Hardware Virtualization.

Run the beneath command from the console.

[root@linuxtechi ~]# grep -E '(vmx|svm)' /proc/cpuinfo

We should get the word either vmx or svm in the output, otherwise CPU doesn’t support virtualization.

Step:1 Install KVM and its associate packages

Run the following yum command to install KVM and its associated packages.

[root@linuxtechi ~]# yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer bridge-utils

Start and enable the libvirtd service

[root@linuxtechi ~]# systemctl start libvirtd
[root@linuxtechi ~]# systemctl enable libvirtd

Run the beneath command to check whether KVM module is loaded or not

[root@linuxtechi ~]# lsmod | grep kvm
kvm_intel             162153  0
kvm                   525409  1 kvm_intel
[root@linuxtechi ~]#

Install Virtualbox on Centos 7

1. Change to root User

Bash

su -
## OR ##
sudo -i

2. Install Fedora or RHEL Repo Files

Bash

cd /etc/yum.repos.d/

## Fedora 26/25/24/23/22/21/20/19/18/17/16 users
wget http://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo

## CentOS 7.4/6.9 and Red Hat (RHEL) 7.4/6.9 users
wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo

3. Update latest packages and check your kernel version

Update packages

Bash

## Fedora 26/25/24/23/22 ##
dnf update

## Fedora 21/20/19/18/17/16 and CentOS/RHEL 7/6/5 ##
yum update


While executing yum update if you get any dependency errorfor glibc use the following to resolve it:

rpm -qa | grep glibc
You may see duplicates for either glibc or glibc common. There might be a common version in between any two packages of glibc and glibc-common. Remove the package which has uncommon version.

You can also check for duplicates with the following command:
yum list –showduplicates glibc

rpm -e glibc-common-2.17-196.el7

rpm -qa kernel |sort -V |tail -n 1

uname -r

Note: If you got kernel update or run older kernel than newest installed then reboot:

Bash

reboot

4. Install following dependency packages

CentOS 7/6/5 and Red Hat (RHEL) 7/6/5 needs EPEL repository, install it with following command:

Bash

## CentOS 7 and RHEL 7 ##
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

## CentOS 6 and RHEL 6 ##
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

## CentOS 5 and RHEL 5 ##
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-5.noarch.rpm
Bash

## Fedora 26/25/24/23/22 ##
dnf install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms

## Fedora 21/20/19/18/17/16 and CentOS/RHEL 7/6/5 ##
yum install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-devel dkms

## PAE kernel users install ##
## Fedora 24/23/22 ##
dnf install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-PAE-devel dkms

## Fedora 21/20/19/18/17/16 and CentOS/RHEL 7/6/5 ##
yum install binutils gcc make patch libgomp glibc-headers glibc-devel kernel-headers kernel-PAE-devel dkms

5. Install VirtualBox Latest Version 5.1 (currently 5.1.28)

Bash

## Fedora 26/25/24/23/22 ##
dnf install VirtualBox-5.1

## Fedora 21/20/19/18/17/16/15/14 and CentOS/RHEL 7/6/5 ##
yum install VirtualBox-5.1

Note:
This command create automatically vboxusers group and VirtualBox user must be member of that group.
This command also build needed kernel modules.
Package is VirtualBox-5.1 not VirtualBox.

Rebuild kernel modules with following command:

Bash

## Fedora 26/25/24/23/22/21/20/19 and CentOS/RHEL 7 ##
/usr/lib/virtualbox/vboxdrv.sh setup

## Fedora 18/17/16 and CentOS/RHEL 6/5 ##
/etc/init.d/vboxdrv setup
## OR ##
service vboxdrv setup

You might get the following error:

vboxdrv.sh: Stopping VirtualBox services.
vboxdrv.sh: Building VirtualBox kernel modules.
This system is not currently set up to build kernel modules (system extensions).
Running the following commands should set the system up correctly:

yum install kernel-devel-3.10.0-327.28.2.el7.x86_64
(The last command may fail if your system is not fully updated.)
yum install kernel-devel
vboxdrv.sh: failed: Look at /var/log/vbox-install.log to find out what went wrong.

Execute the following: yum install kernel-devel

If the error still persists look in the log: /var/log/vbox-install.log
If you find the following error: Error: unable to find the sources of your current Linux kernel. Specify KERN_DIR=<directory> and run Make again. Stop.

## Current running kernel on Fedora ##
KERN_DIR=/usr/src/kernels/`uname -r`
 
## Current running kernel on CentOS and Red Hat (RHEL) ##
KERN_DIR=/usr/src/kernels/`uname -r`-`uname -m`
 
## Fedora example ##
KERN_DIR=/usr/src/kernels/2.6.33.5-124.fc13.i686
 
## CentOS and Red Hat (RHEL) example ##
KERN_DIR=/usr/src/kernels/2.6.18-194.11.1.el5-x86_64
 
## Export KERN_DIR ##
export KERN_DIR

Make sure your system is rebooted.

6. Add VirtualBox User(s) to vboxusers Group

Replace user_name with your own user name or some another real user name.

Bash

usermod -a -G vboxusers user_name

7. Start VirtualBox

Use launcher from menu or simply run:

Bash

VirtualBox

Docker Part 1

currently with docker you are limited to only linux machines and apps. It is like installing various zip files on your os.

because of the use of base image we can save space.All the containers use the same base image. If you want 100 containers in traditional vm env you would have needed 100GB space considering 1gb per image but with docker you will need only 1gb of space. Suppose if you need emacs and apache then docker will add to separate images for it on top of each other and with the base image it will form a union image. this image is readonly so to write in it docker will place a writable container and the whole set container:image(apache)+image(emacs)+base image will make a whole container in the docker.  But the writable container is not persistent. Docker boots the image/ image layers on top f each other.

apt-cache search docker.io —will display a list of packages containing docker

apt-chache show docker.io —- will display all details of packaage

Check if it is latest version.

Docker guys have provided us with a script get.docker.com. it has the script for installation.

This is to be executed .

wget -qO- https://get.docker.com/ | sh to install docker latest.

If you want to add non root user in docker then execute the following command:

sudo usermod -aG docker your-user

check by id your-user to check in which group  user is present it should be present in docker group.

all images are present in registry. docker own registry at docker hub. first you will need to pull the docker images from the registry with docker pull command. After that you can use docker run. If you have specified which images to use then docker will automatically pull the images from registry while docker run is executed. the registry of docker is hub.docker.com

You can search using docker search mysql

to download use docker pull ubuntu

always pull the latest you can also use docker pull ubuntu:latest it is same. :latest is called image tag or the container tag. you can pull using version as well: docker pull ubuntu:14.04

To check what images you have you can issue the docker images command

To start a container use

docker run -ti ubuntu:14.04 /bin/bash -t for terminal i for interactive

to quit container without stopping it press CTRL+P+Q

to check which docker containers are running then command docker ps

to attach to running container use command docker attach <container id>/<container name>

To exit docker and stop the container as well press CTRL+D

To list all the containers even the which that are not running use command docker ps -a

If you exit with ctrl + d and want to start the container again then you can use the command docker start <container-name> 

This will start container in the background

To display docker processes use command docker top / docker top <container-name>

to stop use docker stop <container-name>

in /var/lib there is a folder docker. in that you will have folder container in that you can see folders of running containers with their long ids. In that folders you can see the following files:

<id>-json.log, hosts, config.json, resolv.json, hostconfig.json, resolv.conf.hash, hostname.

In hostname file you will have the short id of container which is shorter version of the long id.

you can also run docker run -ti ubuntu:12.04 /bin/bash it will download automatically.

If you dont want to immediately attach to the container then you can run docker in detached mode:

docker run -d -ti ubuntu:14.04 –name=duck /bin/bash

-d for detached

-ti also because if you dont give you wont be able to login into it.

But it is not persistant if you want the docker container to be persistent then you can use the command:

docker run -ti -v /data –name=duck2 ubuntu:14.04 /bin/bash

-v for volume you have to give it a mount point it will create this mount point inside the container folder.

name should not be same as previous

When in bash inside the container goto /dataand then start working and creat files.

then after quiting the container with ctrl+p+q then in your local machine goto /var/lib/volumes/<container-long-id>/_data you can find all your files here.

If you remove from inside container it will be removed from local machine as well

If you want to attach local directory to continer then

docker run -ti -v /srv/duck3:/data –name=duck3 ubuntu:14.04 /bin/bash

/srv/duck3 local dir

/data container dir

docker run -d -p 3306 -ti mysql /bin/bash

-p for port

this will open port 3306 and nat it to external port.

you check by this command locally iptables -L -t nat

-L list

-t for table nat

it will nat to random port if want to pen specific port

docker run -d -ti -p 3306:3306 mysql /bin/bash

you can also specify the specific ip address if you want with -p <ip>:3306:3306