Total Pageviews

Wednesday, 11 January 2017

SVN Commands and Administration !!!

In my previous post we discussed the concept of SVN and its installation. In this post I am going to show the working with commands of SVN from Client and administration part at the server end.

SVN is a source code management tool which is used by a developer to push the code to the actual SVN server. Sometimes different users may work on the same directory and files, so to avoid conflicts related to data, svn uses "commit" which allows whether a file can be committed or any other user is still working on it.

If other users working on it, svn throws us a message asking whether to continue with the changes by A developer or to commit another developer B's work.

Let us get into the action by practicing few basic commands:

We have seen already, how to add a file and commit. Now lets see how to add a directory which contains different files.

F:\Workspace_local>svn st
svn: warning: W155007: 'F:\Workspace_local' is not a working copy

F:\Workspace_local>

Important rule never forget to perform work/ execute svn cmds in your Workspace.

F:\Workspace_local>cd subversion
F:\Workspace_local\subversion>
F:\Workspace_local\subversion>svn st  --- just to know workspace is in sync with repo or not....
F:\Workspace_local\subversion>

Before adding a directory to my repo,

F:\Workspace_local\subversion>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local\subversion

01/09/2017  05:51 PM    <DIR>          .
01/09/2017  05:51 PM    <DIR>          ..
01/09/2017  05:50 PM             1,914 README.md
01/09/2017  05:53 PM                17 Welcome.txt
               2 File(s)          1,931 bytes
               2 Dir(s)  230,929,895,424 bytes free
F:\Workspace_local\subversion>
Adding a folder named "New folder", it contains some text files and a image file.

Now add it to our server repo, 

F:\Workspace_local\subversion>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local\subversion

01/11/2017  07:17 PM    <DIR>          .
01/11/2017  07:17 PM    <DIR>          ..
01/11/2017  07:13 PM    <DIR>          New folder
01/09/2017  05:50 PM             1,914 README.md
01/09/2017  05:53 PM                17 Welcome.txt
               2 File(s)          1,931 bytes
               3 Dir(s)  230,929,895,424 bytes free
F:\Workspace_local\subversion>        Above is, current status before commit and after copying..

F:\Workspace_local\subversion>svn st
?       New folder
F:\Workspace_local\subversion>        Status shows recent changes which are yet to sync local with repo..

It is time to add our directory...

F:\Workspace_local\subversion>svn add "New folder"
A         New folder
A  (bin)  New folder\Capture1.PNG
A         New folder\hp_sr.txt
A         New folder\antivir.txt
A         New folder\icici amts.txt
F:\Workspace_local\subversion>

Once added, we need to commit this to reflect in repo....


F:\Workspace_local\subversion>svn commit -m "adding newfolder to repo"
Adding         New folder
Adding  (bin)  New folder\Capture1.PNG
Adding         New folder\antivir.txt
Adding         New folder\hp_sr.txt
Adding         New folder\icici amts.txt
Transmitting file data ....
Committed revision 3.
F:\Workspace_local\subversion>



To delete a file from local and repo,

F:\Workspace_local\subversion>svn delete "New folder"\hp_sr.txt
D         New folder\hp_sr.txt
F:\Workspace_local\subversion>cd "New folder"
F:\Workspace_local\subversion\New folder>
F:\Workspace_local\subversion\New folder>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local\subversion\New folder

01/11/2017  07:22 PM    <DIR>          .
01/11/2017  07:22 PM    <DIR>          ..
02/15/2016  01:13 PM               157 antivir.txt
01/11/2017  07:12 PM           170,057 Capture1.PNG
09/08/2016  10:37 AM                50 icici amts.txt
               3 File(s)        170,264 bytes
               2 Dir(s)  230,929,723,392 bytes free
F:\Workspace_local\subversion\New folder>

So far this is temporary. If we check in browser, this won't be reflected at server end.

Before committing and after deleting at local, still we can view the file and its contents...

F:\Workspace_local\subversion\New folder>svn commit -m "deleted hp_sr"
Deleting       hp_sr.txt
Committed revision 4.
F:\Workspace_local\subversion\New folder>


After commit, it will be permanently deleted....

To check the logs, we have "svn log"command.

F:\Workspace_local\subversion>svn log -r3
------------------------------------------------------------------------
r3 | (no author) | 2017-01-11 19:18:45 -0500 (Wed, 11 Jan 2017) | 1 line

adding newfolder to repo
------------------------------------------------------------------------
F:\Workspace_local\subversion>

Similarly to view log for a range of revisions,

F:\Workspace_local\subversion>svn log -r3:4
------------------------------------------------------------------------
r3 | (no author) | 2017-01-11 19:18:45 -0500 (Wed, 11 Jan 2017) | 1 line

adding newfolder to repo
------------------------------------------------------------------------
r4 | (no author) | 2017-01-11 19:23:55 -0500 (Wed, 11 Jan 2017) | 1 line

deleted hp_sr
------------------------------------------------------------------------
F:\Workspace_local\subversion>

Whenever we feel like updating repo with older we have "update" command,
If we consider that our previous deletion is not a proper step and want changes to go back.

F:\Workspace_local\subversion\New folder>svn update -r3
Updating '.':
A    hp_sr.txt
Updated to revision 3.
F:\Workspace_local\subversion\New folder>

F:\Workspace_local\subversion\New folder>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local\subversion\New folder

01/11/2017  07:32 PM    <DIR>          .
01/11/2017  07:32 PM    <DIR>          ..
02/15/2016  01:13 PM               157 antivir.txt
01/11/2017  07:12 PM           170,057 Capture1.PNG
01/11/2017  07:32 PM                11 hp_sr.txt
09/08/2016  10:37 AM                50 icici amts.txt
               4 File(s)        170,275 bytes
               2 Dir(s)  230,929,723,392 bytes free
F:\Workspace_local\subversion\New folder>

There are two other main commands with special usage. When it comes to a developer, he has his workspace checked out with the repo. But what if a one time user want to push some data from his PC. Similarly what if someone wants to pull a single file.

Usually we can use checkout command to push entire directory, so to push a single file we have "svn import" command.

Note: To make use of "import" command, no need to run it from Workspace. I copied a file outside of my workspace "subversion".

F:\Workspace_local>svn st
svn: warning: W155007: 'F:\Workspace_local' is not a working copy
F:\Workspace_local>

F:\Workspace_local>svn import hp_sr.txt http://10.0.0.218:81/subversion/New\folder/hp_sr.txt -m "adding hp_sr thru import"
Adding         hp_sr.txt
Committed revision 5.
F:\Workspace_local>

One import to New\folder... and below import to "subversion" (Workspace)...

F:\Workspace_local>svn import hp_sr.txt http://10.0.0.218:81/subversion/hp_sr.txt -m "adding hp_sr thru import"
Adding         hp_sr.txt
Committed revision 6.
F:\Workspace_local>
F:\Workspace_local>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local

01/11/2017  07:39 PM    <DIR>          .
01/11/2017  07:39 PM    <DIR>          ..
01/11/2017  07:32 PM                11 hp_sr.txt
01/11/2017  07:17 PM    <DIR>          subversion
               1 File(s)             11 bytes
               3 Dir(s)  230,929,723,392 bytes free

F:\Workspace_local>
To fetch/pull a single file, we make use of "export" command... We can run it from outside of workspace. 

F:\Workspace_local>svn export http://10.0.0.218:81/subversion/New%20folder/antivir.txt
A    antivir.txt
Export complete.

F:\Workspace_local>
F:\Workspace_local>dir
 Volume in drive F is ENTERTAINMENT
 Volume Serial Number is 42C5-DACA

 Directory of F:\Workspace_local

01/11/2017  07:48 PM    <DIR>          .
01/11/2017  07:48 PM    <DIR>          ..
01/11/2017  07:18 PM               157 antivir.txt
01/11/2017  07:32 PM                11 hp_sr.txt
01/11/2017  07:17 PM    <DIR>          subversion
               2 File(s)            168 bytes
               3 Dir(s)  230,929,723,392 bytes free

F:\Workspace_local>




Other than these, there are few other commands like "svn revert" to revert any operations before committing, and "svn diff" used to view difference between two files.

So far we completed svn commands practical from client side. Now let us see some SVN admin tasks. 

Admin should perform tasks related to Authorization, backups of repo and restricting few tasks or applying conditions to perform a task.

Observe the output:

F:\Workspace_local\subversion>svn log -r2:6
------------------------------------------------------------------------
r2 | (no author) | 2017-01-09 18:01:13 -0500 (Mon, 09 Jan 2017) | 1 line

1st Commit
------------------------------------------------------------------------
r3 | (no author) | 2017-01-11 19:18:45 -0500 (Wed, 11 Jan 2017) | 1 line

adding newfolder to repo
------------------------------------------------------------------------
r4 | (no author) | 2017-01-11 19:23:55 -0500 (Wed, 11 Jan 2017) | 1 line

deleted hp_sr
------------------------------------------------------------------------
r5 | (no author) | 2017-01-11 19:44:45 -0500 (Wed, 11 Jan 2017) | 1 line

adding hp_sr thru import
------------------------------------------------------------------------
r6 | (no author) | 2017-01-11 19:46:34 -0500 (Wed, 11 Jan 2017) | 1 line

adding hp_sr thru import
------------------------------------------------------------------------


F:\Workspace_local\subversion>

We are working with anonymous user, we can restrict this by creating separate users and also by restricting their access to certain level (directories).

To create users,

root@ubuntu-new:/opt/subversion-1.9.5-0# cd apache2/
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# htpasswd -c /opt/subversion-1.9.5-0/svn_users dev1
New password:
Re-type new password:
Adding password for user dev1
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

For second developer,  used -m to modify the users file. If we use -c it will create the file again...

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# htpasswd -m /opt/subversion-1.9.5-0/svn_users dev2
New password:
Re-type new password:
Adding password for user dev2
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# cat /opt/subversion-1.9.5-0/svn_users
dev1:$apr1$hb7S2CtJ$GwVa5dEBDzY3EFS/K6yj71
dev2:$apr1$vhccSww7$y.W95.QX5ofsBosFPcxZH0
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

Let me show, if I use -c option:

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# htpasswd -c /opt/subversion-1.9.5-0/svn_users dev3
New password:
Re-type new password:
Adding password for user dev3
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# cat /opt/subversion-1.9.5-0/svn_users
dev3:$apr1$Fa3y4u5Q$INzi63RXzYiThQZ9hm8S.1
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

Create a new file with name "svn_access" (can give according to admin wish)

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2# vi /opt/subversion-1.9.5-0/svn_access
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2#

root@ubuntu-new:/opt/subversion-1.9.5-0# cat svn_access
[subversion:/]                        ------ can mention desired directory
dev3=rw
[groups]                        ------ can assign a group to a user

root@ubuntu-new:/opt/subversion-1.9.5-0#

To enable this authentication in our apache server,

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2/conf# vi httpd.conf
root@ubuntu-new:/opt/subversion-1.9.5-0/apache2/conf#

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2/conf# cat httpd.conf | tail -12
Include "/opt/subversion-1.9.5-0/apache2/conf/ssi.conf"
Include "/opt/subversion-1.9.5-0/apache2/conf/bitnami/bitnami.conf"
<Location /subversion>
DAV svn
SVNPath "/opt/subversion-1.9.5-0/repository"

AuthType Basic
AuthName "Subversion Project1"
AuthUserFile /opt/subversion-1.9.5-0/svn_users
Require valid-user
AuthzSVNAccessFile /opt/subversion-1.9.5-0/svn_access
</Location>

root@ubuntu-new:/opt/subversion-1.9.5-0/apache2/conf#

Then restart the apache server,

root@ubuntu-new:/opt/subversion-1.9.5-0# ./ctlscript.sh restart
/opt/subversion-1.9.5-0/subversion/scripts/ctl.sh : subversion stopped
Syntax OK
/opt/subversion-1.9.5-0/apache2/scripts/ctl.sh : httpd stopped
Syntax OK
/opt/subversion-1.9.5-0/apache2/scripts/ctl.sh : httpd started at port 81
/opt/subversion-1.9.5-0/subversion/scripts/ctl.sh : subversion started at port 3690

root@ubuntu-new:/opt/subversion-1.9.5-0#

Now try to open in browser, this time server will pop-up the Authentication window....
Use of svn_access file, is to restrict the access permissions of a user...

root@ubuntu-new:/opt/subversion-1.9.5-0# vi svn_access
root@ubuntu-new:/opt/subversion-1.9.5-0#
root@ubuntu-new:/opt/subversion-1.9.5-0#
root@ubuntu-new:/opt/subversion-1.9.5-0# cat svn_access
[subversion:/New folder]
dev3=rw
[groups]
root@ubuntu-new:/opt/subversion-1.9.5-0#

Provided access only to "New folder" for dev3 user...

Before changing access,
After providing new access, he can only view and edit "New folder"
Another admin task is to handle backup and restore of repo.

To take dump,

svnadmin dump /opt/subversion-1.9.5-0/repository/ > /file_loc

To load it,

svnadmin load /opt/subversion-1.9.5-0/repository/ < /file_loc

Similar to these, an admin can restrict commits with some conditions. Admin can restrict few types of files to get added to server.

root@ubuntu-new:/opt/subversion-1.9.5-0/repository# cd hooks
root@ubuntu-new:/opt/subversion-1.9.5-0/repository/hooks#

In this hooks directory, we have different templates where we can write our scripts to restrict any kind of task...

root@ubuntu-new:/opt/subversion-1.9.5-0/repository/hooks# ls
post-commit.tmpl  post-revprop-change.tmpl  pre-commit.tmpl  pre-revprop-change.tmpl  start-commit.tmpl
post-lock.tmpl    post-unlock.tmpl          pre-lock.tmpl    pre-unlock.tmpl

root@ubuntu-new:/opt/subversion-1.9.5-0/repository/hooks#

For example, we can write a script inside "pre-commit.tmpl" which needs to be executed before commiting any type of file/dir...

An admin can perform/restrict different tasks, in this post I covered few of them....

################################################################################

Monday, 9 January 2017

SubVersion (SVN) for Source Code Management !!!

In any Software project, there are different methodologies and tools used during the project. Coming to methodologies there are Agile and Waterfall. Similarly when it comes to tools there are many tools we use during different stages of project.

In this post I am going to discuss about such a tool which widely used for Source Code Management during a Software Project. In Production environment, for any project there will be different releases. 

All these releases will be scheduled with some timelines. To avoid confusion related to source code, we need some tools to manage it. SubVersion (SVN) is tool many of the Software Companies make use for Code management.

Using SVN, we create a directory structure which helps to maintain clarity about the code related to every release. This proper structure make use of following branching strategies:

1. Sequential 

                Sequential is a strategy, in which we can work on one release after completion of the other. No work related to different releases can be carried out during single timeline.

R2 will be started after completion of R1. Similarly R3 will be started after R2. So there will be no confusion in moving the developers data to production. But in real time this strategy will not be a good one.

2. Parallel

                 Most of the Clients cannot wait to start working on R2 till the R1 completes. So then comes the Parallel branching strategy. In Parallel, we can work on more than one Release at single point of time. 

This helps to overcome the problem of longer duration for every release as we face in Sequential branching. To maintain proper directory structure, after completion of coding for each release we update the production with delta changes.

There is a problem with this strategy. For initial releases the updation of production is something like we merge 90% of new data to 10% of old data. But when it comes to 15th release or more, to update 10% of new data we need to update it by merging with 90% of old data.

3. Merging

                  To avoid the problem with Parallel branching, the Merging strategy was evolved. I will make use of two locations Core and Prod. Prod is the data what actually our Production server contains and core is data at developer end.

After completion of every release the Core should equals with Prod. When we use this strategy, during the development of R15 data which we need to update to Prod is just R13, R14 and R15 (Since R13,R14 may be in development stage).

What I mean, when R1 is released, core=prod=R1+old
                               R2 is released, core=R1+old  Prod=R2+R1+old to make Core=Prod
                                           we copy R2 to core......
                                        .
                                        .
                                        .
                                        .
                                        .
                               R8 , core=R1...R6/7+old and Prod=R8 + R1....R6/7 + old
                                            So to make Core=Prod, copy back R8 to Core.
                                        .
                                        .
                                        .
                                        .
                                        .
                    For R15, we need delta changes from R13, R14 and R15 only....
But in Parallel branching, we need to update Prod with R15,R14,R13 changes by merging with R1....R12 data.

Now lets see some practical part like installing SVN and by working with some commands usually used by developers at Client level....

I am using svn from bitnami which is parented by APACHE.


root@ubuntu-new:~# cd /home/ubu/
root@ubuntu-new:/home/ubu#
root@ubuntu-new:/home/ubu# ls
bitnami-subversion-1.9.5-0-linux-x64-installer.run  Music
Desktop                                             Pictures
docker-master.zip                                   Public
Documents                                           Setup-Subversion-1.8.17.msi
Downloads                                           Templates
libapache2-mod-php5_5.3.10-1ubuntu3.25_amd64.deb    Videos
root@ubuntu-new:/home/ubu#

Already downloaded and copied my installer file to my Ubuntu server...

root@ubuntu-new:/home/ubu# ./bitnami-subversion-1.9.5-0-linux-x64-installer.run
----------------------------------------------------------------------------
Welcome to the Bitnami Subversion Stack Setup Wizard.

----------------------------------------------------------------------------
Installation folder

Please, choose a folder to install Bitnami Subversion Stack

Select a folder [/opt/subversion-1.9.5-0]:

----------------------------------------------------------------------------
Web Server Port

Choose a port that is not currently in use, such as port 81.

Apache Web Server Port [81]:

----------------------------------------------------------------------------
Repository

Please configure your repository

Path to repository [/opt/subversion-1.9.5-0/repository]:

----------------------------------------------------------------------------
Setup is now ready to begin installing Bitnami Subversion Stack on your
computer.

Do you want to continue? [Y/n]: Y

----------------------------------------------------------------------------
Please wait while Setup installs Bitnami Subversion Stack on your computer.

 Installing
 0% ______________ 50% ______________ 100%
 #########################################

----------------------------------------------------------------------------
Setup has finished installing Bitnami Subversion Stack on your computer.

Launch Bitnami Subversion Stack [Y/n]: Y

root@ubuntu-new:/home/ubu#

To Check the status of our subversion...


root@ubuntu-new:/home/ubu# cd 

/opt/subversion-1.9.5-0
root@ubuntu-new:/opt/subversion-1.9.5-0# ls
apache2        config        licenses               README.txt  sqlite      uninstall.dat
changelog.txt  ctlscript.sh  manager-linux-x64.run  repository  subversion  use_subversion
common         img           properties.ini         scripts     uninstall
root@ubuntu-new:/opt/subversion-1.9.5-0#

root@ubuntu-new:/opt/subversion-1.9.5-0# ./ctlscript.sh status
subversion already running
apache already running
root@ubuntu-new:/opt/subversion-1.9.5-0#

Through browser we can check.....


Now to work with SVN CLI, lets install the client in our Windows.
We need to checkout the SVN server using the URL in our desired location from our client...


Most important thing is, Workspace is the place where a developer can do all the coding. We can add and then commit the files which are inside our Workspace.

As you see, soon after checkout command, there is a subversion directory within which readme file was created. Our workspace is the subversion directory...


Above I tried executing svn commands outside the workspace, it didn't allow me. But when I tried changing directory to subversion then I am able to execute "svn info".

Let us try creating a file and adding it to our SVN server.


Above I created Welcome file with some text in it.... 
Soon after creating a file, we can check the "svn status" command....

Command Prompt output of our SVN Client machine....

F:\Workspace_local\subversion>
F:\Workspace_local\subversion>svn status
?       Welcome.txt

F:\Workspace_local\subversion>

In the above output, ? represents this file is not yet reflected on our server....
First add it, then we need to commit....

Note: Commit always needs a message, we can give any message so that whenever we check the logs, we can remember what are the changes done with this commit....

F:\Workspace_local\subversion>svn add Welcome.txt
A         Welcome.txt

F:\Workspace_local\subversion>svn commit Welcome.txt -m "1st Commit"
Adding         Welcome.txt
Transmitting file data .
Committed revision 2.

F:\Workspace_local\subversion>

This time if we check status command, no new changes done at client side. All data is in sync with server...

F:\Workspace_local\subversion>svn status
F:\Workspace_local\subversion>

Screenshots from browser,

Revision number before commit of new file....

Revision number incremented after commit of new file....

We can view the content of the file....

################################################################################

Friday, 23 December 2016

Virtualization Shell (virsh) Commands !!!

In this post I am going to show different commands of "virsh" CLI including output. To manage virtual machines of KVM through command line we need "virsh". To enable virsh CLI we install libvirt-bin package.

Now I am going to show basic commands and their sub-commands usage. Whenever we learn any new commands, it is always a good practice to remember a command as follows:

command: virsh.
sub-command: the part comes next to the command.
options or optional things: which changes as per sub-command.

Sorry if I messed it, but what I mean to explain is...

Every command starts with a name, sub-command and options. So if we need to attach a disk the sub-command will be attach-disk and options are like size of the disk. Similarly command for renaming a domain, can be virsh rename oldname newname. So sometimes instead of options there will be optional things.

Even if my explanation is still unclear, don't worry about it, just continue with the post. Below are the few commands and its usage with output.

root@ubuntu-kvm:~# virsh -v                                 --- for short description
2.1.0
root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh --version
2.1.0
root@ubuntu-kvm:~#

root@ubuntu-kvm:~# virsh -V                                  --- for long description
Virsh command line tool of libvirt 2.1.0
See web site at http://libvirt.org/

Compiled with support for:
 Hypervisors: QEMU/KVM LXC UML Xen LibXL OpenVZ VMware VirtualBox ESX Test
 Networking: Remote Network Bridging Interface netcf Nwfilter VirtualPort
 Storage: Dir Disk Filesystem SCSI Multipath iSCSI LVM RBD Sheepdog ZFS
 Miscellaneous: Daemon Nodedev AppArmor Secrets Debug DTrace Readline Modular
root@ubuntu-kvm:~#

root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running
 8     VM1                            running


root@ubuntu-kvm:~#

Host and Hypervisor related:

To view version of hypervisor,

root@ubuntu-kvm:~# virsh version                       
Compiled against library: libvirt 2.1.0
Using library: libvirt 2.1.0
Using API: QEMU 2.1.0
Running hypervisor: QEMU 2.6.1

root@ubuntu-kvm:~#

To view maximum cpus of host,

root@ubuntu-kvm:~# virsh maxvcpus                     
16

root@ubuntu-kvm:~#

To view memory statistics of host,

root@ubuntu-kvm:~# virsh nodememstats
total  :              2047852 KiB
free   :                81620 KiB
buffers:                 9600 KiB
cached :               148500 KiB

root@ubuntu-kvm:~#

To view cpu statistics of host,

root@ubuntu-kvm:~# virsh nodecpustats
user:                 27478810000000
system:                 736920000000
idle:                 14469860000000
iowait:                1864430000000

root@ubuntu-kvm:~#

To view info related to host,

root@ubuntu-kvm:~# virsh nodeinfo
CPU model:           x86_64
CPU(s):              1
CPU frequency:       2200 MHz
CPU socket(s):       1
Core(s) per socket:  1
Thread(s) per core:  1
NUMA cell(s):        1
Memory size:         2047852 KiB

root@ubuntu-kvm:~#

To view hostname of host machine,

root@ubuntu-kvm:~# virsh hostname
ubuntu-kvm

root@ubuntu-kvm:~#

To view URI (Uniform Resource Identifier),

root@ubuntu-kvm:~# virsh uri
qemu:///system

root@ubuntu-kvm:~#

To view complete system info of host,

root@ubuntu-kvm:~# virsh sysinfo
<sysinfo type='smbios'>
  <bios>
    <entry name='vendor'>innotek GmbH</entry>
    <entry name='version'>VirtualBox</entry>
    <entry name='date'>12/01/2006</entry>
  </bios>
  <system>
    <entry name='manufacturer'>innotek GmbH</entry>
    <entry name='product'>VirtualBox</entry>
    <entry name='version'>1.2</entry>
    <entry name='serial'>0</entry>
    <entry name='uuid'>6F5ED8B0-1976-4DB6-A716-93F90015C16A</entry>
    <entry name='sku'>Not Specified</entry>
    <entry name='family'>Virtual Machine</entry>
  </system>
  <baseBoard>
    <entry name='manufacturer'>Oracle Corporation</entry>
    <entry name='product'>VirtualBox</entry>
    <entry name='version'>1.2</entry>
    <entry name='serial'>0</entry>
    <entry name='asset'>Not Specified</entry>
    <entry name='location'>Not Specified</entry>
  </baseBoard>
</sysinfo>


root@ubuntu-kvm:~#

Domain Administration related:

To take console of our domain/VM,

root@ubuntu-kvm:~# virsh console VM1
Connected to domain VM1
Escape character is ^]
Killed

root@ubuntu-kvm:~#

To view description of our domain/VM,

root@ubuntu-kvm:~# virsh desc VM1
No description for domain: VM1

root@ubuntu-kvm:~#

To view cpustats of domain/VM,

root@ubuntu-kvm:~# virsh cpu-stats VM1
CPU0:
        cpu_time           451.542828932 seconds
Total:
        cpu_time           451.542828932 seconds
        user_time          360.800000000 seconds
        system_time         67.150000000 seconds

root@ubuntu-kvm:~#

To view memory stats of domain/VM,

root@ubuntu-kvm:~# virsh dommemstat VM1
actual 1048576
swap_in 0
last_update 1482452747
rss 1101100
root@ubuntu-kvm:~#

To display connection URI of domain/VM,

root@ubuntu-kvm:~# virsh domdisplay VM1
spice://127.0.0.1:5900
root@ubuntu-kvm:~#

To create dump of domain/VM,

root@ubuntu-kvm:~# virsh dumpxml VM1 >>vm1.xml
root@ubuntu-kvm:~#
root@ubuntu-kvm:~# ls
vm1.xml
root@ubuntu-kvm:~#

To take snapshot of domain/VM,

root@ubuntu-kvm:~# virsh screenshot VM1 vmsnap
Screenshot saved to vmsnap, with type of image/x-portable-pixmap
root@ubuntu-kvm:~#
root@ubuntu-kvm:~# ls
vm1.xml  vmsnap
root@ubuntu-kvm:~#

To suspend our domain/VM,

root@ubuntu-kvm:~# virsh suspend VM1
Domain VM1 suspended

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running
 8     VM1                            paused

root@ubuntu-kvm:~#

To resume our domain/VM,

root@ubuntu-kvm:~# virsh resume VM1
Domain VM1 resumed

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running
 8     VM1                            running

root@ubuntu-kvm:~#

To shutdown our domain/VM,

root@ubuntu-kvm:~# virsh shutdown VM1
Domain VM1 is being shutdown

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running

root@ubuntu-kvm:~#

To rename our domain/VM,

root@ubuntu-kvm:~# virsh domrename VM1 Ubuntu
Domain successfully renamed

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running

root@ubuntu-kvm:~#

To check state of our domain/VM,

root@ubuntu-kvm:~# virsh domstate Ubuntu
shut off
root@ubuntu-kvm:~#

To start our domain/VM,

root@ubuntu-kvm:~# virsh start VM1
error: failed to get domain 'VM1'                                 Error because VM1 no longer exists...
error: Domain not found: no domain with matching name 'VM1'
root@ubuntu-kvm:~#

As soon as we renamed our domain, the default xml file of our domain also gets renamed automatically...

root@ubuntu-kvm:/etc/libvirt/qemu# ls -lrth
total 16K
drwxr-xr-x 3 root root 4.0K Dec 21 18:48 networks
-rw------- 1 root root 4.3K Dec 22 18:48 win2003.xml
-rw------- 1 root root 3.9K Dec 23 17:58 Ubuntu.xml
root@ubuntu-kvm:/etc/libvirt/qemu#

root@ubuntu-kvm:~# virsh start Ubuntu
Domain Ubuntu started

root@ubuntu-kvm:~#
root@ubuntu-kvm:~# virsh list
 Id    Name                           State
----------------------------------------------------
 7     win2003                        running
 9     Ubuntu                         running

root@ubuntu-kvm:~#

Similarly to restart/reset/destroy our domain/vm....

root@ubuntu-kvm:~# virsh restart/reset/destroy <domname>

Similarly to save/restore state of our domain/vm....

root@ubuntu-kvm:~#
 virsh save Ubuntu vm1state
Domain Ubuntu saved to vm1state
root@ubuntu-kvm:~#
root@ubuntu-kvm:~#
 virsh restore Ubuntu vm1state
root@ubuntu-kvm:~#

Adding/removing resources related:

To change vcpus of our domain/VM,

root@ubuntu-kvm:~# virsh setvcpus --count 1 Ubuntu

To change vcpus of our domain/VM,

root@ubuntu-kvm:~# virsh setmem --size 512 Ubuntu

To attach disk/device/interface to our domain/VM,

root@ubuntu-kvm:~# virsh 
attach-device 
<domain> <file> 
                                           attach-disk 
<domain> <source> <target> 
                                           attach-interface 
<domain> <type> <source>

To detach disk/device/interface to our domain/VM,

root@ubuntu-kvm:~# virsh de
tach-device 
<domain> <file> 
                                           detach-disk 
<domain> <source> <target> 
                                           detach-interface 
<domain> <type> <source> 

To view disks attached to our domain/VM,

root@ubuntu-kvm:~# virsh domblklist win2003
Target     Source
------------------------------------------------
hda        /var/lib/libvirt/images/win2003.qcow2
hdb        /test/Windows 2003 Server.iso

root@ubuntu-kvm:~#

To view network properties of our domain/VM,

root@ubuntu-kvm:~# virsh domiflist win2003
Interface  Type       Source     Model       MAC
-------------------------------------------------------
-          network    default    rtl8139     52:54:00:20:1d:55

root@ubuntu-kvm:~#

######################################################################################