2017年12月20日 星期三

Python的Unicode HOWTO

https://docs.python.org/3/howto/unicode.html
最早的ASCII碼(Code),只有0~127(7bits),無法包含更多特殊字。
1980年代的個人電腦幾乎都是8bit,所以可以處理ASCII且還多出128~255可以使用,但不同機器有不同的Code。
Unicode一開始用16bits來取代原來的8bits,有2^16 (65536)個值可使用,目標是可包含所有的語言,但其實還是不夠。後來擴展到 0 through 1,114,111 ( 0x10FFFF in base 16).
一個字元(Character),Unicode定義了一個字元的Code point,就是這個字元的值。表示法如U+12CA就表示值是0x12ca的字元。
Encoding(編碼)是指從字串轉為位元組序列的規則,UTF-8就是一種最普遍的編碼方式,UTF (Unicode Transformation Format),8是指8-bits的數字被使用來編碼。規則是這樣:
  1. If the code point is < 128, it’s represented by the corresponding byte value.
  2. If the code point is >= 128, it’s turned into a sequence of two, three, or four bytes, where each byte of the sequence is between 128 and 255.
Python的預設編碼方式是UTF-8,可用特殊的註解改變(第一或第二行)
# -*- coding:  -*-
Python支援以Unicode為名的變數
若想保持Python的原始碼是ASCII-only,可用Escape字符 /u或/U或/N字元名來編寫
>>> "\N{GREEK CAPITAL LETTER DELTA}"  # Using the character name
'\u0394'
>>> "\u0394"                          # Using a 16-bit hex value
'\u0394'
>>> "\U00000394"                      # Using a 32-bit hex value
'\u0394'
另外,可用bytes的method "decode()"來輸出Unicode,另外帶encoding參數和error處理方式(請看最上方連結)
Python 3.2有100種不同的encoding。
單個字元的轉換,可用chr(int)輸出Unicode,用ord(str)輸出值(code point)
字串與位元組串的操作是bytes.decode() 和 str.encode()

The most important tip is:

Software should only work with Unicode strings internally, decoding the input data as soon as possible and encoding the output only at the end.

2017年11月28日 星期二

Bash if

if [[ -n "${VAR}" ]]; then
  echo variable VAR is set and value is ${VAR}
fi

if [[ -z "${VAR}" ]]; then
  echo variable VAR is unset or empty
fi

                        +-------+-------+-----------+
                VAR is: | unset | empty | non-empty |
+-----------------------+-------+-------+-----------+
| [ -z "${VAR}" ]       | true  | true  | false     |
| [ -z "${VAR+set}" ]   | true  | false | false     |
| [ -z "${VAR-unset}" ] | false | true  | false     |
| [ -n "${VAR}" ]       | false | false | true      |
| [ -n "${VAR+set}" ]   | false | true  | true      |
| [ -n "${VAR-unset}" ] | true  | false | true      |
+-----------------------+-------+-------+-----------+

if [[ -e file.txt ]]; then
  echo file.txt is exist
fi

if [[ ! -e file.txt ]]; then
  echo file.txt is NOT exist
fi

http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

2017年11月20日 星期一

docker jekyll on windows 10

docker run -p 80:4000 -v c:\tmp:/srv/jekyll jekyll/jekyll jekyll s -s ./my-site --watch --force-polling

Browse http://127.0.0.1:4000

on Windows 7  with Docker toolbox:
replace "c:\tmp" to "/c/tmp"

docker run -p 4000:4000 -v /c/Users/Jack_Lin/Documents/abc:/srv/jekyll jekyll/jekyll jekyll s --watch --force-polling

Browse http://192.168.99.100:4000

If your want to customize minima theme. check
https://github.com/jekyll/minima

Setup proxy on Docker Toolbox on Windows7

https://github.com/docker/toolbox/issues/102

Hi!,
Just tested the following steps on Win7 with Docker-toolbox and it works ! Wow!
Split from windows command directory to linux with the following command:
$ docker-machine ssh default
Now the command line start with "docker@default:~$"
Write sudo ..... as the following line
docker@default:~$ sudo vi /var/lib/boot2docker/profile
Write "i" to pass in insert mode and return button.
Add the two http export settings with username and password :
export "HTTP_PROXY=http://username:password@proxy.something.it:port"
export "HTTPS_PROXY=http://username:password@proxy.something.it:port"
Digit esc ": wq!" to save the new line and verify if everithing is ok with the cat command
docker@default:~$ cat /var/lib/boot2docker/profile
Restart the daemon
docker@default:~$ sudo /etc/init.d/docker restart
Now i have restarted the Docker toolbox and the "Docker pull image_name" work fine!
Thanks a lot to everybody!

2017年8月18日 星期五

Setup Centos syslog server and Ubuntu syslog client

On CentOS syslog server: (172.16.0.1)
edit /etc/rsyslogd.conf
uncomment
$ModLoad imtcp
$InputTCPServerRun 514

add a line after "*.emerg"
*.emerg                /var/log/emerg.conf

Add firewall rule of TCP port 514
# firewall-cmd --permanent --zone=public --add-port=514/tcp
# firewall-cmd --reload

On Ubuntu syslog client
edit /etc/rsyslog.d/50-default.conf
add a line after "*.emerg"
*.emerg           @@172.16.0.1

Try on client:
# logger -p user.emerg "emerg from XXXX"

Check the syslog server is recieved.
# cat /var/log/emerg.log

vga=0x319

# vim /etc/defualt/grub
add GRUB_CMDLINE_LINUX="nomodeset vga=0x319"

# grub-mkconfig -o /boot/grub/grub.cfg

2017年6月8日 星期四

2017年4月12日 星期三

install kernel 4.10 on RHEL 7

http://linux.vbird.org/linux_basic/0540kernel.php
http://elrepo.org/tiki/tiki-index.php
http://elrepo.org/tiki/kernel-ml


rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org (external link)
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

yum --enablerepo=elrepo-kernel install kernel-ml

UEFI多重開機

http://www.rodsbooks.com/refind/

UEFI不支持MBR開機 (只能用GPT)

GPT也不能再Legacy BIOS下開機了

GPT可支持2TB以上

UEFI firmware有boot manager
可在linux下用efibootmgr編輯
windows下用bcdedit編輯

windows下可用diskpart切換MBR & GPT
https://zh.wikipedia.org/wiki/%E4%B8%BB%E5%BC%95%E5%AF%BC%E8%AE%B0%E5%BD%95/

安裝時是UEFI mode就會被用GPT切割硬碟
安裝時是Legacy就是用MBR

UEFI mode安裝完Ubuntu或是RHEL,grub會安裝到 UEFI SYSTEM PARTITION的/boot/efi

Legacy mode安裝完Ubuntu或是RHEL,會安裝grub到MBR (會scan可開機partition在menu)

Edit EFI boot entries in Linux

The utility name is efibootmgr

https://wiki.gentoo.org/wiki/Efibootmgr



2017年4月10日 星期一

Install fio in Ubuntu 16.04

Download the fio from https://github.com/axboe/fio

unzip the fio-master.zip

# apt install gcc

# apt install zlib1g-dev

# apt install libaio1 libaio-dev

goto the unzipped fio-master folder
# ./configure
# make
# sudo make install

2017年3月24日 星期五

ipmi on ubuntu

step1) 安裝OpenIPMI與ipmitool
# sudo apt-get install openipmi
# sudo apt-get install ipmitool

step2) 載入IPMI
# modprobe ipmi_msghandler
# modprobe ipmi_devintf
# modprobe ipmi_si

step3) 看自己local端的BMC資訊
# ipmitool lan print

2017年3月23日 星期四

ubuntu server 16.04.02 install ubuntu-desktop

Add cdrom as a package source (ubuntu-desktop is not included, just add a note)
(Insert the cdrom into system)
# sudo -i
# mount /dev/cdrom /media/cdrom
# apt-cdrom -m -d=/media/cdrom add

Set the LAN setting
# ifconfig -a
Remember the interface name you want to config (enp24s0 for example)
Edit the file /etc/network/interfaces
Add following two line
auto enp24s0
iface enp24s0 inet dhcp

restart the networking service
# systemctl restart networking.service

Add Proxy setting to apt
Add a line in /etc/apt/apt.conf (if the file not exist, create a new one)
Acquire::http::Proxy "http://username:password@proxy.hinet.net:80";

# apt-get update
# apt install --no-install-recommends ubuntu-desktop
# apt-get install gnome-terminal

Allow root login through remote ssh
Edit the file /etc/ssh/sshd_config
PermitRootLogin prohibit-password
to
PermitRootLogin yes

Then restart the sshd service
# systemctl restart sshd.service

Enable sshd
# sysv-rc-conf sshd on

2017年3月17日 星期五

tmux try out

#tmux
#tmux attach

Split window vertically: ctrl+b %
Split window horizontal: ctrl+b "
Switch between pane: ctrl+b q (then press the number key)

Help mode: ctrl+b ?

Copy mode: ctrl+b [
and then use array key to point the start position and ctrl+[space]
and use array key to point the end position and alt+w
Paste the copied content: ctrl+b ]

Create a new window: ctrl+b c
switch to next window: ctrl+b n
switch to previous windows: ctrl+b p
show the window number and switch to it: ctrl+b w
switch to window #: ctrl+b (the window number)
Rename window: ctrl+b ,

Detach tmux: ctrl+b D

http://blog.chh.tw/posts/tmux-terminal-multiplexer/
https://awhan.wordpress.com/2010/06/20/copy-paste-in-tmux/

2017年3月16日 星期四

Bitcoin mining

https://github.com/luke-jr/bfgminer
https://github.com/pooler/cpuminer
http://p2pool.in/
https://github.com/p2pool/p2pool
https://blockchain.info/address/1LYoqQuLuZNzHeZab95jMkwimskVCeh9d

get the MAC address of Network Adapter using ethtool

# ethtool -P enp24s0
# ethtool -P eth0

or
# cat /sys/class/net/enp24s0/address

http://stackoverflow.com/questions/14955504/finding-original-mac-address-from-hardware-itself

2017年1月9日 星期一

VirualBox shared folder permission

Need to add your user into group "vboxsf"
# sudo usermod -aG vboxsf yourusername
and then restart the virtual machine

2017年1月5日 星期四

Resolve the problem of install VirtualBox addon on Fedora

Error message:
Failed to set up service vboxadd, please check the log file
unable to find the sources of your current linux kernel

# sudo dnf update

#sudo dnf install gcc kernel-devel kernel-headers

Restart the Fedora virtual machine and install VirtualBox Addon