2013/04/30

在 Ubuntu Unity 內加入自訂程式及捷徑

小弟的 NB 時常家裡公司搬來搬去,環境不同有些程式或設定都要重開(設),很麻煩。除了用一個簡單的 script 的方式去設定這些東西,另外我們可以藉由 Unity 來提供更視覺化的界面。

在 Launcher(啟動器) 內加入指令




這個例子(如上圖),是在 Launcher 裡加一個 lock screen 的程式。簡單的說就是寫一個 .desktop,把指令寫在 exec 那行,再把它放在家目錄的 ~/.local/share/applications/ 即可。

$ cat .local/share/applications/lock.desktop
[Desktop Entry]
Name=lock screen
Icon=/usr/share/icons/oxygen/128x128/actions/system-lock-screen.png
Exec=gnome-screensaver-command --lock
Terminal=true
Type=Application
Categories=Settings;DesktopSettings;
StartupNotify=true

加入捷徑並增加右鍵選單




在 Unity 的左邊有一排捷徑,可以用 gsettings get 取得列表,如下:

$ gsettings get com.canonical.Unity.Launcher favorites
['application:///home/dorowu/bin/custom-config.desktop', 'application://firefox.desktop', 'application://terminator.desktop', 'application://gvim.desktop', 'application://nautilus.desktop', 'application://zim.desktop', 'application://gemanx.desktop', 'application://virtualbox.desktop', 'application://shutter.desktop', 'application://google-chrome.desktop', 'application://gnome-control-center.desktop', 'unity://expo-icon', 'unity://devices', 'unity://running-apps']

捷徑可以放在任意位置,我是放在家目錄的 bin,接著再開 nautilus 拖進去就行。如上圖。

$ cat bin/custom-config.desktop
[Desktop Entry]
Encoding=UTF-8
Name=SwitchSetting
Exec=/home/dorowu/bin/config.sh
Icon=/usr/share/icons/Humanity/apps/48/redhat-tools.svg
Terminal=true
Type=Application
StartupNotify=false
X-Ayatana-Desktop-Shortcuts=Test1;Test2;Test3


[Test1 Shortcut Group]
Name=General
Exec=/home/dorowu/bin/config.sh
TargetEnvironment=Unity

[Test2 Shortcut Group]
Name=General_Unity
Exec=/home/dorowu/bin/config.sh 1
TargetEnvironment=Unity

[Test3 Shortcut Group]
Name=Asustor
Exec=/home/dorowu/bin/config.sh 1 asustor
TargetEnvironment=Unity

Reference

2013/04/23

Install Ubuntu Core on NAS




好久之前入手了一台華芸科技 ASUSTOR AS-604T 的 NAS,對於某些玩家來說(像我),不能方便的用 apt 安裝自己想要的軟體實在是很麻煩,還好的是用 Ubuntu Core 來在上面製作一個簡單的 Ubuntu rootfs 是非常容易的。本文最後將在 Ubuntu Core 上安裝另一個 SSH server 做為介紹。

考量到為了以後方便移植軟體進去 NAS,推薦使用跟 AS-604T 相容的 Ubuntu 版本 (11.10) 未來才不會遇到太多函式庫相依的問題。

接著本文將展示
  • 使用 Ubuntu Core
  • Hacking ASUSTOR NAS

安裝 Ubuntu Core

$ ssh root@192.168.0.50 # ssh to your NAS
$ # Download Ubuntu Core 11.10
$ cd /volume1/ 
$ wget http://cdimage.ubuntu.com/ubuntu-core/releases/11.10/release/ubuntu-core-11.10-core-amd64.tar.gz
$ # Extract it to ubuntu
$ mkdir ubuntu && tar xvf ubuntu-core-11.10-core-amd64.tar.gz -C ubuntu/

mount.sh

$ cat << END > mount.sh
#!/bin/sh
ubuntu=/volume1/ubuntu
if [ "\$1" == "umount" ]; then
    if grep -q " \$ubuntu/" /proc/mounts; then
        awk -v ubuntu=$ubuntu '$2 ~ /ubuntu\// { print \$2 }' /proc/mounts | sort -u | xargs umount
    else
     true
    fi
elif [ "\$1" == "mount" ]; then
    grep -q " \$ubuntu/proc " /proc/mounts || mount -o bind /proc \$ubuntu/proc
    grep -q " \$ubuntu/sys " /proc/mounts || mount -o bind /sys \$ubuntu/sys
    grep -q " \$ubuntu/mnt" /proc/mounts || mount -o bind /volume1 \$ubuntu/mnt
    grep -q " \$ubuntu/dev/pts" /proc/mounts || mount -t devpts /dev/pts \$ubuntu/dev/pts
else
    awk -v ubuntu=\$ubuntu '\$2 ~ /ubuntu\// { print \$2 }' /proc/mounts | sort -u
fi
END
$ chmod +x mount.sh

chroot.sh

$ cat << END > chroot.sh
#!/bin/sh
if [ \$# == "0" ] ; then
    chroot /volume1/ubuntu /bin/bash
else
    chroot /volume1/ubuntu \$*
fi
END
$ chmod +x chroot.sh

安裝基本工具 and 切換到 Ubuntu

$ echo nameserver 8.8.8.8 >> /etc/resolv.conf
$ cat << END >> /etc/apt/source.list
deb http://tw.archive.ubuntu.com/ubuntu/ precise universe
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise universe
deb http://tw.archive.ubuntu.com/ubuntu/ precise-updates universe
deb-src http://tw.archive.ubuntu.com/ubuntu/ precise-updates universe
END
$ ./mount.sh mount # 掛載 filesystems,每次開機只要做一次
$ ./chroot.sh #切換到 Ubuntu
$ apt-get update
$ apt-get install command-not-found less vim x11-apps
至此我們已經有一個 Ubuntu 可以使用了。

安裝另一個 OpenSSH server

$ apt-get install openssh-server
$ passwd root
$ sed -i 's/^Port 22$/Port 2222/' /etc/ssh/sshd_config
$ /etc/init.d/ssh start
從別台電腦登入新開的 ssh server,順便啟用 X11 Forwarding
$ xhost +
$ ssh -X root@192.168.0.50 -p 2222
$ # 設 local ip:display number
$ export DISPLAY=192.168.0.100:0
$ xclock
結果如同第一張圖所示,我們新開了一個 ssh service 在 2222 埠,連進去後 X11 Forwarding 也能順利使用(原先 ASUSTOR NAS 的 ssh server 不提供 X11 forwarding)。

順便提供自動設 DISPLAY 參數的 script,可以加在 ~/.bashrc,以後就不用每次 export DISPLAY=...
# DISPLAY
remote_ip=`env | awk '$0 ~ /^SSH_CLIENT=/ {print substr($1, index($1, "=") + 1)}'`
if [ ! -z "$remote_ip" ] ; then
    export DISPLAY=$remote_ip:0
    echo DISPLAY=$DISPLAY
fi

ASUSTOR NAS 程式自動啟動


可以在 /usr/local/AppCentral/*/CONTROL/start-stop.sh 找到一些 ASUSTOR NAS 的啟動 script。我隨便挑了一個 python 的 start-stop.sh 來偷加入剛才的 ssh server 啟動結束指令,如下列第 11 及 17 行

1 #!/bin/sh
     2
     3 EASY_INSTALL_BIN=/usr/local/AppCentral/python/bin/easy_install
     4 EASY_INSTALL_LINK=/usr/local/bin/easy_install
     5
     6 case $1 in
     7
     8  start)
     9   echo "Starting python..."
    10   ln -sf $EASY_INSTALL_BIN $EASY_INSTALL_LINK
    11   /volume1/mount.sh mount && /volume1/chroot.sh /etc/init.d/ssh start
    12   ;;
    13
    14  stop)
    15   echo "Stopping python..."
    16   rm -rf $EASY_INSTALL_LINK
    17   /volume1/chroot.sh /etc/init.d/ssh stop && /volume1/mount.sh umount
    18   ;;
    19
    20  *)
    21   echo "usage: $0 {start|stop}"
    22   exit 1
    23   ;;
    24
    25 esac
    26 exit 0

(為什麼 NAS 系統不直接以 Ubuntu/Debian/... 當做 base system 就好)

2013/04/18

gksudo & pkexec (PolicyKit)


gksudo 和 pkexec 是二種有圖形化介面取得 root 權限執行程式的方式。gksudo 是 sudo 的 GTK+ frontent,也因此它與 sudo 是用相同的權限管理設定。

pkexec 是基於 PolicyKit,比起 sudo(gksudo),它提供更彈性權限管理與介面客製化的設定。在 Ubuntu 12.04 上可以在以下地方找到它的設定檔:
  • Actions: /usr/share/polkit-1
  • Local Authorities: /etc/polkit-1
  • 3rd party Authorities: /var/lib/polkit-1
Actions 是設定行為,舉例來說 gparted 的執行權限如下:
$ cat /usr/share/polkit-1/actions/com.ubuntu.pkexec.gparted.policy
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>

  <action id="com.ubuntu.pkexec.gparted">
    <message gettext-domain="gparted">Authentication is required to run the GParted Partition Editor</message>
    <icon_name>gparted</icon_name>
    <defaults>
      <allow_any>auth_admin</allow_any>
      <allow_inactive>auth_admin</allow_inactive>
      <allow_active>auth_admin</allow_active>
    </defaults>
    <annotate key="org.freedesktop.policykit.exec.path">/usr/sbin/gparted</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
  </action>

</policyconfig>

allow_* 則可根據不同的使用情境設定授權方式,像 auth_admin 就是要求授權對象(user)需有管理權限才能執行,換句話說就是要輸入密碼。詳細不同點可參考下方引用
allow_any, allow_inactive, and allow_active. Inactive sessions are generally remote sessions (SSH, VNC, etc.) whereas active sessions are logged directly into the machine on a TTY or an X display. Allow_any is the setting encompassing both scenarios.
For each of these settings the following options are available:
  • no: The user is not authorized to carry out the action. There is therefore no need for authentification.
  • yes: The user is authorized to carry out the action without any authentification.
  • auth_self: Authentication is required but the user need not be an administrative user.
  • auth_admin: Authentication as an administrative user is require.
  • auth_self_keep: The same as auth_self but, like sudo, the authorization lasts a few minutes.
  • auth_admin_keep: The same as auth_admin but, like sudo, the authorization lasts a few minutes.
 annotate key 的那 2 行則是設定程式路徑及是否允許 GUI。

什麼叫做管理權限呢?在 /etc/polkit-1/localauthority.conf.d/51-ubuntu-admin.conf 設定了若該位使用者為 sudo 或 admin group 便能算是有管理權限
$ cat /etc/polkit-1/localauthority.conf.d/51-ubuntu-admin.conf
[Configuration]
AdminIdentities=unix-group:sudo;unix-group:admin
當然你也可以根據不同 action 設定不同的管理權限及反應。像是在 /var/lib/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla 裡有一段
[Disable hibernate by default]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=no
裡頭設定了所有使用者(unix-user:*)都不行(ResultActive)用 org.freedesktop.upower.hibernate。

參考資料