Friday, February 23, 2018

Bash assign variable from a command output

How do I assign the output of a shell command to a shell variable under Unix like operating system? For example, I want to store the date command output to a variable called $now. 

How do you do that?



var=$(/path/to/command arg1 arg2)

The content of test.sh

#!/bin/bash

PROD=$(ls -al)

echo "${PROD}"



the output

~$ bash test.sh |grep xen
drwxrwxr-x   3 user group       4096 Mar  7  2017 test_xen
drwxrwxr-x   3 user group       4096 Mar  7  2017 test_xen_4.7

Wednesday, February 21, 2018

install cross compiled VIM on the target machine

1. cross compiling vim8.0
2. copy vim to /usr/local/bin on the target machine
3. copy .vimrc to target machine
4. copy syntax.vim to /usr/local/share/vim/syntax on the target machine
5. copy ~/.vim/colors/kolor.vim (kolor is my favorite color theme) to the target machine
6. you are good to go

Wednesday, February 14, 2018

Migrate KVM Disk Access from IDE to Virtio

So in order to make the switch from from ide or Sata to virtio, the following steps need to be taken:
Run virsh edit <your_vm_name>. From there, edit the config file and adjust all lines of
<target dev='hda' bus='ide'/>
<target dev='sda' bus='sata'/>
so they look like this
<target dev='vda' bus='virtio'/>
Furthermore, remove all <address type .../> lines so that libvirt can regenerate them appropriately.
Inside the guest, edit /etc/fstab and replace all occurrences of /dev/sdX with /dev/vdX`.
That’s it, now shutdown the machine and start it with an virsh start <your_vm_name> (just a reboot inside the started VM won’t work).

Friday, February 9, 2018

using virt-viewer remote access KVM guest vm

sometime you try to access someone's host machine, which KVM running several vm image, you want to access one of the vm.
one the remote host (name: wawr-builder, ip 10.71.50.228) machine: 
wawr@wawr-builder:~$ virsh list
 Id    Name                           State
----------------------------------------------------
 2     Ubuntu64                       running
 11    av_virtual_3                   running

from my local machine, you can do:
jim@jim-dell-5810:~$ virt-viewer --connect qemu+ssh://wawr@10.71.50.228/system av_virtual_3

With the above, you’ll have to enter your SSH password twice – first to establish the connection to the hypervisor and secondly to establish a tunnel to the VM’s VNC/SPICE session

see original post:
https://www.jethrocarr.com/2012/08/04/virt-viewer-remote-access-tricks/

using perf for profiling

the sample code test_a.c as below:

#include <stdio.h>
#include <stdint.h>
#include <string.h>

void func3(void)
{
   int count = 0;
   char src[100];
   char dst[100];
   for(count=0; count < 0XFF; count++)
       memcpy(src,dst, sizeof(src));

   return;
}

void func2()
{
   int count = 0;
   int64_t s =1;
   for(count=0; count < 0XFF; count++)
   {
       s =s *(count+1);
       func3();
   }

   return;
}

void func4()
{
   int count = 0;
   int64_t s =1;
   for(count=0; count < 0XFF; count++)
   {
       s =s *(count+1);
       func3();
   }

   return;
}
void func1(void)
{
   int count = 0;
   for(count=0; count < 0XFFFF; count++)
       func2();

   return;
}

int main(void)
{
    printf("\n Hello World! \n");
    func1();
    printf("\n step 2! \n");
    func4();
    return 0;
}

#compiling with:
gcc -Wall  test_a.c -g -o test_a

#install perf on ubuntu 14:
 sudo apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`

#run test_a:
./test_a

#find test_a pid as 21033 through
ps aux|grep test_a

sudo perf record -p 21033
#ctrl+c to break

sudo perf report
it will show the profiling result as below:


now you know the bottle neck--- func3()
you can also see real time cpu usage by :
sudo perf top

other option -g
perf record -g -p pid
perf report -g 'graph,0.5,caller'

perf report --max-stack=6 --stdio -s parent

ref: http://rhaas.blogspot.co.uk/2012/06/perf-good-bad-ugly.html

Thursday, February 1, 2018

My git quick start

1. setup git diff tool in ~/.gitconfig
[diff]
    tool = p4merge
You can use git difftool to show a single commit.
Say you want to see the commit with the sha1 abc123:
git difftool abc123~1 abc123
(~1 tells git to move to the previous commit, so abc123~1 is the commit before abc123)
If you use this regularly, you could make a custom git command to make it easier:
  1. Create a file called git-show somewhere on your $PATH with the following contents:
    git difftool $1~1 $1
    
  2. Give that file execute permissions:
    chmod +x ~/path/to/git-show
    
  3. Use the command git-show <sha1 or tag or ...>
For the modified files on current branch:
git difftool ./my_modified_file
After git add command (staged files)
git difftool --staged
2. git show current branch name:
git branch
3. git show all remote branches:
git branch --all
4. git switch branch:
git checkout my_branch_name
5. git Add file:
git add ./path/my_file.txt
6. git commit changes:
git commit -m "my change text"
7. git push
git push
8. git log, show change history
git log
9. delete remote branch
git push origin --delete branch_name
10. branch diff
git diff master_git master_git_test
11. Create a local branch test which will use to track remote branch origin/test:
git branch test origin/test