2013年8月22日星期四

Encounter "UUID is duplicate" message after moving the VirtualBox image file

C:\Program Files\Oracle\VirtualBox> VBoxManage internalcommands sethduuid "E:\VirtualBox VMs\centos\centos.vdi"


2013年8月19日星期一

GRUB2 general configuration

grub-mkconfig, default is output to stdout

Valid keys are in the /etc/default/grub

--------------------------------------------------------

Manually add the menu entry)

1) Adding extra custom menu entries to the end of the list can be done by editing /etc/grub.d/40_custom
Alternatively, in/etc/grub.d folder, create the file XX_whichos where XX is the number. It is used to indicate the ordering of the menuitem.

2) After the file is edited, make it executable.
sudo chmod +x /etc/grub.d/XX_whichos

3) Execute "update-grub2" command

The updated grub.cfg will be placed at /boot/grub/ folder

=====================================================
For traditional partition,

Windows 7 entry (first harddisk, 2nd primary partition)

menuentry ‘Windows 7′ {
set root=’(hd0,msdos2)’
chainloader +1
}

=====================================================
For EFI,

menuentry "Windows 7" {
insmod part_gpt
insmod fat
insmod search_fs_uuid
insmod chain
search --fs-uuid --no-floppy --set=root 080F-E6DA
chainloader (${root})/efi/Microsoft/Boot/bootmgfw.efi
}

UUID can be obtained from blkid

=====================================================


2013年8月18日星期日

ncat usage


quota from "http://nmap.org/ncat/guide/ncat-usage.html":


Main usage:

Ncat always operates in one of two basic modes: connect mode and listen mode. In connect mode, Ncat initiates a connection (or sends UDP data) to a service that is listening somewhere. For those familiar with socket programming, connect mode is like using the connect function. In listen mode, Ncat waits for an incoming connection (or data receipt), like using the bind and listen functions. You can think of connect mode as client mode and listen mode as server mode.

Connect mode:
ncat <host> [<port>]

Listen mode:
ncat -l [<host>] [<port>]

In listen mode, <host> controls the address on which Ncat listens; if you omit it, Ncat will bind to all local interfaces (INADDR_ANY). If the port number is omitted, Ncat uses its default port 31337.Typically only privileged (root) users may bind to a port number lower than 1024. A listening TCP server normally accepts only one connection and will exit after the client disconnects. Combined with the --keep-open option, Ncat accepts multiple concurrent connections up to the connection limit. With --keep-open (or -k for short), the server receives everything sent by any of its clients, and anything the server sends is sent to all of them. A UDP server will communicate with only one client (the first one to send it data), because in UDP there is no list of “connected” clients.

Ncat can use TCP, UDP, SCTP, SSL, IPv4, IPv6, and various combinations of these. TCP over IPv4 is the default.

-------------------------------------------------------------------------

File transfer using ncat:

input file on host1
output file on host2

Transfer a file, receiver listens
host2$ ncat -l > outputfile
host1$ ncat --send-only host2 < inputfile

Transfer a file, sender listens
host1$ ncat -l --send-only < inputfile
host2$ ncat host1 > outputfile


Transfer a bundle of files

host2$ ncat -l | tar xzv
host1$ tar czv <files> | ncat --send-only host2


---------------------------------------------------------------------------


Example. Running a command with --sh-exec
ncat -l --sh-exec "echo `pwd`"

Example. Ncat as mail client

$ ncat -C mail.example.com 25
220 mail.example.com ESMTP
HELO client.example.com
250 mail.example.com Hello client.example.com
MAIL FROM:a@example.com
250 OK
RCPT TO:b@example.com
250 Accepted
DATA
354 Enter message, ending with "." on a line by itself
From: a@example.com
To: b@example.com
Subject: Greetings from Ncat

Hello. This short message is being sent by Ncat.
.
250 OK
QUIT
221 mail.example.com closing connection

2013年8月16日星期五

Compress / Extract Files using tar Command

tar.bz2 file:
Compress:
tar -jcvf archive_name.tar.bz2 directory_to_compress

Extract:
tar -jxvf archive_name.tar.bz2
tar -jxvf archive_name.tar.bz2 -C /tmp/extract_here/

------------------------------------------------------------------------

tar.gz file:
Compress:
:tar -cvzf archive_name.tar.gz directory_to_compress/

Extract:
tar -zxvf archive_name.tar.gz
tar -zxvf archive_name.tar.gz -C /tmp/extract_here/

------------------------------------------------------------------------

tar file:
Compress:
tar -cvf archive_name.tar directory_to_compress
Extract:
tar -xvf archive_name.tar
tar -xvf archive_name.tar -C /tmp/extract_here/

------------------------------------------------------------------------

tar.xz file:
Compress:
tar -cvJf archive_name.tar.xz
Extract:
tar -xvJf archive_name.tar.xz
tar -xvJf archive_name.tar.xz -C /tmp/extract_here/

2013年8月15日星期四

Setup COM port in VirtualBox (Host: XUbuntu, Guest: Windows XP)

In VirtualBox Setting,
Port Number: COM1
IRQ: 4
I/O Port: 0x3F8
Port Mode: Host Device
Port/File Path: /dev/ttyS0

If encounter read/write permission error,
add the current Linux account to:
'dialout' Group

In xubuntu, System Administrator > 'Edit Group...' , add current user to the 'dialup' group

Check serial port in Linux:
dmesg | grep tty
If the screen shows out ttyS0, it means COM1

FYI,
as previous 'dialout' group account stated,
ls -l /dev/ttyS0 can view the COM1 permission group.

Executing background task while running SSH

nohup ./whatever > /dev/null 2>&1 &

Enable / Lock root account in Linux

Lock root account in Linux:
sudo passwd -l root

Enable root account in Linux:
sudo passwd root


2013年8月1日星期四

Effective C++ notes (Rule 1 - Rule 10)

Rule 1: View C++ as a federation of language

view as 4 sub language
a) C
b) Object-oriented C
c) Template C++
d) STL

C++ Looks like 4 "sub-language" merge to form a language.
They are different => we should understand each "sub-language" behavior.

------------------------------------------------------

Rule 2: consts, enums and inlines to #define

Use: consts replace define
easy for symbol table look up, debug made easy, const = 1 copy only
class:

class c
{
static const int a = 5;
};

For some of old compilers:
In header:
class c
{
static const int v1;
}

In source:
const int c::v1 = 1;

Use enum:
class c
{
enum {v1 = 1};
int i[v1];
}
//can be found in template metprogramming

Use: template<typename T>
inline void CallWithMax(const T& a, const T& b)
{
f(a>b?a:b);
}

not:
#define CALL_WITH_MAX(a,b) f((a)>b()?(a):(b))

-------------------------------------------------------------

Rule 3: Use const whenever possible

char greeting[] = "Hello";
char* p = greeting; //non-const pointer, non-const data
const char* p = greeting; //non-const pointer, const data
char* const p = greeting; //const pointer, non-const data
const char* const p = greeting; //const pointer, const data

In other words,
const located at: (left)*, const data
const located at: *(right), const pointer

void f1(const Widget* pw);
is as same as
void f2(Widget const * pw);

iterator
const_iterator //cannot change value

logical-constness
mutable,prevent "bitwise-constness"

operator[] overload:
merge const operator[] and operator[]:
const cha& operator[] (std::size_t position)const
{
return text[position];
}
char& operator[](std::size_t position)
{
return
const_cast<char&>(
static_cast<const TextBlock&>(*this)[posiiton];
)
}

-------------------------------------------------------------

Rule 4: Make sure that objects are initialized before they are used

ABEntry::ABEntry(const str::string& name, const std::string& address)
{
theName = name;
theAddress = address;
}
//Call theName, theAddress assignment

ABEntry::ABEntry():theName(),theAddress(),numTimesConsulted(0)
{
}
//call theName, theAddress constructor (run faster!)

static -> singleton design pattern, initialize and reference only one time.

-------------------------------------------------------------

Rule 5: Know what functions C++ silently writes and calls

class Empty
{
public:
Empty(){...}
Empty(const Empty& rhs){...}
~Empty(){...}
Empty& operator=(const Empty& rhs){...}
};

cannot compile (generate implicate copy assignment) if the class has following conditions:
For the below cases, we must declare the copy constructor and assignment explicitly.
1) Reference
2) Const
3) base class copy assignment = private

-------------------------------------------------------------

Rule 6: Explicitly disallow the use of compiler-generated functions you do not want

1) Mark it as private
2) Do not implement it, declare only.

Private, derived from below class (Boost library - noncopyable class)

class Uncopyable
{
protected:
Uncopyable(){}
~Uncopyable(){}
private:
Uncopyable(const Uncopyable&);
Uncopyable& operator=(const Uncopyable&);
}

class HomeForSale : private Uncopyable
{

}

-------------------------------------------------------------

Rule 7: Declare destructors virtual in polymorphic base classes

If the class is suppose not to be polymorphic, don't declare the class as virtual destructor.

-------------------------------------------------------------

Rule 8: Prevent exceptions from leaving destructor

The exception leave to outside world <- may cause undefined behavior / exit at earlier stage.
solution: explicit declare a function to let the user explicit "close" it.
The user call the "close" function with try{} catch{} case,
Equivalent:
Because the class has given the chance for the user explicit handle the exception, but the user didn't.
It is not the fault made by the class.

class cla
{
private:
//......

bool closed;

void close()
{
//.....
closed = true;
}

~cla::cla()
{
if(!closed)
{
try
{
close();
}
catch()
{
//catch exception, log
}
}
}
};

-------------------------------------------------------------

Rule 9: Never call virtual functions during construction or destruction

-------------------------------------------------------------

Rule 10: Have assignment operators return a reference to *this

because x=y=z=15;
equivalent x=(y=(z=15));

also apply in +=, -=, *=...etc operator.
Widget& operator+=(const Widget& rhs)
{
//....
return *this;
}

Widget& operator=(int rhs)
{
//...
return *this;
}