顯示具有 Linux-System-Admin 標籤的文章。 顯示所有文章
顯示具有 Linux-System-Admin 標籤的文章。 顯示所有文章

2015年11月26日 星期四

mariadb + python on synology nas

紀錄一下
一般而言
大部分的python mysql connector (例如MySQLdb)在安裝的時候都需要找到一個mysql_config程式
偏偏這玩意在synology上搭配的mariadb就是沒有
所以套件安裝過程中會失敗
看到的error會是"EnvironmentError: mysql_config not found"


一般ubuntu等linux套件可以很簡單地利用套件管理安裝libmysqlclient-dev或是libmariadbclient-dev來補上MySQL_config程式
但是synology的OS裏頭沒有方便的套件管理
不過好家在mysql本身也有出python connector (ref https://dev.mysql.com/doc/connector-python/en/connector-python-installation-source.html )
把tarball抓下來在synology NAS上跑python setup.py install基本上一定會成功

然後import mysql.connector再參考https://dev.mysql.com/doc/connector-python/en/connector-python-examples.html 看這套件的API怎麼使用即可.



2015年2月3日 星期二

makefile的四種等於 =, :=, +=, ?=


筆記一下makefile的四種等於的差異

雖然平常工作比較少從零開始寫markfile

但偶而還是需要修改一下既有的檔案

 

makefile總共有四種等於 先從最簡單的開始

  1. ?= 

這個是適用於想表達如果?=左邊的變數從未被設定過的話 就讓它設定成右邊的內容、數值

如果已經有值了 就不進行任何變動

  1. =

makefile官方文件中 使用=的時機是希望讓左邊的變數成為"recursively expanded variable"  也就是當變數真的被使用到的時候 在進行遞迴展開

所以就可以先定義 後續再補數值

以下提供一個例子

CFLAGS = $(include_dirs) -O

include_dirs = -Ifoo -Ibar

如果使用以上兩行的定義 CFLAGS真的要被使用時就會展開成 -lfoo -lbar -O  所以可以後定義、補充include_dirs的內容

然後這樣子的特性可能會帶來兩個壞處

第一個是有可能造成無限遞迴 以下就是一個例子 不過這個makefile會提示bug 所以倒也還好修正

CFLAGS = $(CFLAGS) -O

CFLAGS要被使用時會造成無限迴圈

另外一個壞處是跟效能有關的:

因為遞迴展開也適用於函式 所以函式的展開執行也會發生在變數要被使用的時候 也就是說如果變數要被重複使用幾次 函式就會展開跑幾次

所以效能就可能不太好

  1. := 

markfile官方文件中 使用:=的時機是希望讓左邊的變數成為"Simply expanded variable" 也就是當這一行敘述被執行時 就馬上進行展開

所以一旦跑完:= 就不再存在任何reference在變數中

  1. +=

最後一個是+= 用來增加敘述在原有已經定義的變數中

如果+=左邊的變數還沒定義過 +=執行的效果就會跟=一樣 也就是recursively-expanded variable.

如果變數曾經定義過 +=跑起來的效果就會視前一次定義是用=或者:=來決定跑完後是recursively-expanded variable或者是simply expanded variable

2014年7月27日 星期日

ubuntu hibernation(休眠)設定

紀錄分享一下此次讓linux桌機成功休眠的步驟

首先列一下環境:
我使用的是ubuntu 14.04
一開始安裝的時候沒設定swap區域

後來想到要斷電休眠已經為時已晚
雖然linux可以使用file來當swap區域
但是似乎相容性並不加

所以一開始我得找出usb開機碟
利用GParted軟體重新在我的硬碟上拉出一塊16GB的分割區
並且指定為swap

重開機後
先修改/etc/fstab把swap磁區掛上去
e.g. UUID=2fb3f377-e7c0-4e2c-bfb2-c0459ef4bfca    none    swap    sw    0   0
(可能要先用blkid查一下磁區的UUID 而不要用/dev/sdXX的方式掛會比較好)

基本上做到這裡的時候
跑pm-hibernate就會成功關機了

但是
但是
但是
開機後
還是一般的開機全部重來
調閱kmesg來看
就發現了
"PM: Hibernation image not present or could not be loaded"
這個的原因是因為即使有在/etc/fstab裡頭寫了swap
但是在bootloader端也就是grub還是不知道他要從哪裡把記憶體裡頭的內容讀回來
必須修改grub的開機指令 讓她知道開機去要看哪個地方知道休眠喚醒從哪裡讀資料
在grub的語法下 為resume=磁區位置
所以我修改了/etc/default/grub檔案
在GRUB_CMDLINE_LINUX_DEFAULT=中加入了"resume=UUID=2fb3f377-e7c0-4e2c-bfb2-c0459ef4bfca"
接著執行update-grub (或是update-grub2如果你是用grub2)指令 讓這個修改落實到grub設定檔中
然後重開機後 cat /proc/cmdline就會發現resume=XXXX進去了

再來就是實際測試了
打pm-hibernate指令讓電腦睡下去
然後在開機 終於大功告成

總結一下
讓休眠正常運作的關鍵因素
1. swap空間要大於記憶體的量 (swap檔案的支援度可能有問題 還是要有真的分割區)
2. 讓grub知道swap磁區的位置
3. 出問題的時候可以cat /var/log/pm-suspend.log還有kmesg看一下為何沒法睡著或是開機沒成功

2014年4月7日 星期一

活用binutils中的addr2line和objdump來加速crash debug....

雖然日常工作上 處理crash不是我的本業
但有時無可奈何 還是會收到很多crash dump....(這類型的問題 最近佔據愈來愈多工作時間了QQ)
所以還是把常用的一些工具和參數做個筆記 省得每次遇到都要問谷哥


GCC binutility裡頭有很多工具
除了ar as cpp ld這類型產生object/library的工具
還有一些是為了runtime或者offline分析使用
例如gdb就是runtime時很好用的debugger
無奈在Android的環境裡 gdb實在是不易使用
漸漸地 以前學生時代的功力都沒了
本篇文章主要想筆記兩個offline分析工具
分別是objdump和addr2line


首先看看objdump
Manual的簡短說明一語道破這玩意的功能--display information from object files
基本上所有object檔案裡的資訊可以被反解成人眼較易讀的資訊
個人最愛用的就是-S和-l參數
-S會讓組語跟C/C++原始碼交雜在dump中
-l則是會加上原始碼的行號、檔案資訊 增加可讀性

範例:
objump -S -v vmlinux      這樣就會吐出夭壽長的kernel dump.....


接著就是addr2line
一樣manual一語道破--convert addresses into file names and line numbers.
常用的參數搭配是-C -f -e


-f是要求除了行號和檔名外 也吐出函示名稱

-C是parse C++必用的參數 讓function name可以恢復正常
舉個例子 parse libEGL.so某function時
沒加上-C就會得到_ZN7android6Loader4openEPNS_16egl_connection_tE這種很難看懂得名字
有加上就會拿到android::Loader::open(android::egl_connection_t*)
天差地遠阿~~

-e則是目標的object file 沒加的話 基本上addr2line就會試著抓a.out來parse.....

範例:
addr2line -C -f -e vmlinux XXXX          <-- address="" div="" nbsp="">





P.S.1 注意make時 要開-g的cflags才會把debug symbol放進去
P.S.2 針對android userspace的object file  可以去out/target/product/ARCH/symbols 裡頭去撈!!!!


2014年1月24日 星期五

透過putty調整backspace key map

筆記一下

每次換公司、換電腦後 從M$ windows用putty連工作站常常遇到一件很討厭的事
那就是backspace鍵在vim裡會打出^? 而不是一般預期的退一位行為
這件事可以透過改vim key map來處理

但最近也看到一個更容易的做法--從putty之類的連線軟體去著手
以下的圖例就是putty可以修改的地方
只要把預設的Control-? (127)換成Control-H就可以解決這個問題.


2013年9月20日 星期五

增加web server php模組使用的記憶體上限 increase php memory limit

just find php.ini and modify it with below property  (for example, 512MB memory)
   memory_limit = 512M

2013年6月30日 星期日

apache 利用.htaccess來控管目錄存取

做法是使用rewrite module把網頁存取的行為 導到php來檢查權限


1. load module
     可以在/etc/apache2/httpd.conf中加入以下這一行就可以把rewrite module弄起來 (/usr/lib/apache2/module/mod_rewrite.so可能隨著系統不同路徑會不同)
     LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
2. 注意AllowOverride屬性
     如果要管控特定目錄 必須要確保該目錄上層的.htaccess有開放Override 不然就有可能白寫.htaccess
3. 在想要管控的目錄中的 .htaccess 加入以下幾行
     Options +FollowSymLinks
     RewriteEngine on
     RewriteRule ^(.*)$ ../authorize.php?file=$1
   
    藉著以上三行 一但有任何存取該目錄的行為 就會跑進上一層目錄下的 authorize.php



以下則是authorize.php的範例
採用wordpress的登入與否來決定是否開放權限給人下載

++require('../wp-blog-header.php');
+
+
+if(is_user_logged_in()){
+       header('Content-type:application/force-download');
+       header('Content-Transfer-Encoding: Binary');
+       header('Content-Disposition:attachment;filename='.$_REQUEST['file']);
+       readfile($_REQUEST['folder'].'/'.$_REQUEST['file']);
+}
+else{
+       echo "Please login before access this file\n";
+}
+?>



2011年10月16日 星期日

repo forall

repo這個小script是sync Android code base必備的玩意
通常比較常用的指令會是repo init用以設定repo database
還有repo sync用以synchronize database

但是最近愛上了repo forall這個指令
我的工作常常需要在同一個database下進行branch的跳躍
但是呢
一但有稍微改一些測試codes或者安插printk或者logcat message後
要進行branch的跳躍就會遇到有尚未committed changes的error
此時打一行repo forall -c 'git checkout -f'
就可以自動在數百個git projects目錄下跑git checkout -f的指令
非常地方便
以下附上一些可以加在' '裏頭跑的變數
讓整個操作更加多元

Environment
-----------
pwd is the project's working directory. If the current client is a
mirror client, then pwd is the Git repository.

REPO_PROJECT is set to the unique name of the project.
REPO_PATH is the path relative the the root of the client.
REPO_REMOTE is the name of the remote system from the manifest.
REPO_LREV is the name of the revision from the manifest, translated to a
local tracking branch. If you need to pass the manifest revision to a
locally executed git command, use REPO_LREV.
REPO_RREV is the name of the revision from the manifest, exactly as
written in the manifest.
shell positional arguments ($1, $2, .., $#) are set to any arguments
following .
Unless -p is used, stdin, stdout, stderr are inherited from the terminal
and are not redirected.

2011年9月27日 星期二

enable SSH console without SFTP feature

有些時候需要開放SSH權限讓使用者可以連進去機器開console
但是又不希望他利用SFTP亂抓資料

這時可以設著改變/usr/lib/sftp-server的權限
例如改成700時就只有root可以用了之類的

2011年5月27日 星期五

vimdiff 實用指令

  • do - Get changes from other window into the current window.
  • dp - Put the changes from current window into the other window.
  • ]c - Jump to the next change.
  • [c - Jump to the previous change.
  • Ctrl W + Ctrl W - Switch to the other split window.
  • :diffupdate – diff update
  • :syntax off – syntax off
  • zo – open folded text
  • zc – close folded text
http://blog.longwin.com.tw/2009/11/vimdiff-vs-git-diff-2009/

2011年5月17日 星期二

linux的screen

這是一個蠻好用的功能
常常連上server事情做到一半不小心斷線
或者是檔案開了一堆明天想繼續之類的
就可以用screen達成雖然登出 但是開啟的檔案都還在的效果
screen外的常用指令
  1. screen 叫出一個screen process 連帶有一頁視窗
  2. screen -ls 可以看目前開出來的screen有多少個
  3. screen -r 回復screen 如果開多個screen的話 就要打上screen -ls看到的pid了
screen內的常用指令
  1. ctrl+a+c 新增一頁視窗
  2. ctrl+a+n 跳至下一頁視窗 (next)
  3. ctrl+a+p 跳至上一頁視窗 (previous)
  4. ctrl+a+數字 跳至第幾個視窗
  5. ctrl+a+w 秀出目前所開的視窗
  6. ctrl+a+k 關掉目前的視窗
  7. ctrl+a+d 把screen背景執行擺著
  8. ctrl+a+h 把該視窗跳出過的訊息記綠成檔案
  9. ctrl+a+A 更改視窗名稱 這個在tab模式下很實用
  10. ctrl+a+[ 進入buffer mode, 這樣才可以pagedown pageup去看之前跑的訊息 (參考設定3可以調整buffer大小)
有關screen的設定
  1. 不要閃爍 可以修改$HOME/.screenrc 加入vbell off
  2. tab秀出視窗 可以修改$HOME/.screenrc 加入
    caption always "%{= kw}%-w%{= BW}%n %t%{-}%+w %-= @%H - %LD %d %LM - %c"

3. 增加screen buffer size 可以修改$HOME/.screenrc加入defscrollback 10000或是其他數字 就是buffer的行數

reference

2011年5月16日 星期一

ubuntu檢查版本的方式

cat /etc/issue
就會看到他的版本啦

2010年10月28日 星期四

Ctags

find DIR1 DIR2 -name *.h > FILE
find DIR1 DIR2 -name *.c >> FILE
ctags -L FILE

2010年2月27日 星期六

Mozilla Firefox JRE/Java plugin on Ubuntu

Recently I installed Ubuntu 9.10 on my desktop computer.
When I wanted to login my web bank service written in Java, it could not  display information as well as it could with M$-based OS.
Even I've installed JRE/JDK, Mozilla Firefox still couldn't show Java pages functionally.

Fortunately, I used the well-known package management software "synaptic package manager" to search all SunMicro-related packages and found a package named "sun-java6-plugin".
The following is the information provided by "Synaptic package manager"
The Java(TM) Plug-in, Java SE 6
Java Plug-in enables applets written to the Java Platform 6
specification to be run in Mozilla and other web browsers.
Java Plug-in comes with the Java Runtime Environment (JRE).

This is a metapackage containing dependencies for running Java in
various browsers.

After installed this package, Mozilla Firefox started to work well.
How awesome it is !!!!

2010年2月17日 星期三

Grub2 下修改開機順序的方法

日前將家中的PC灌成了Ubuntu 9.10
由於家人偶而還是得使用windows
所以就稍微看了一下文件了解一下grub2如何改變開機順序

主要參考的文件為GRUB 2 中文指南

要修改的檔案在/etc/grub.d裡頭
裡頭會有數個檔案 e.g. 00_header, 10_linux, 30_os-prober, etc.
每個數字都代表了在開機時grub選單出現的順序
例如我希望windows擺在比較前面
我就稍微動個手腳把30_os-prober改成05_os-prober
這樣開機時windows的部份就會出現在ubuntu之前了
把順序的數字更動完畢後
得做一個更新的動作 執行 update-grub2
這個動作執行後會去修改/boot/grub/grub.cfg這個檔案 (預設為唯讀)
就達到我們希望更改開機順序的目標了

2009年9月1日 星期二

DHCP with specified DNS servers

Sometimes the nameserver dispatched from the DHCP server doesn't work well.
In that case, you may want to add some nameservers so that the name-resolving task can perform normally.

There are two ways to achieve this objective:
1. Use the Network-Manager via a friendly GUI (We don't discuss it here)
2. Edit /etc/dhcp3/dhcpclient.conf and append one line to last
"prepend domain-name-servers XXX.XXX.XXX.XXX,XXX.XXX.XXX.XXX;"
/etc/init.d/networking restart

Test Distribution: Ubuntu 9.04
Reference: http://hi.baidu.com/nullspace/blog/item/ab37e6506165b35b1138c263.html