2008年9月10日 星期三

如何讓系統自動掛載usb儲存裝置

在Gnome或者是KDE環境下,當一個USB儲存裝置插入到USB槽時,這些桌面管理程式會幫忙使用者做掛載的動作。但是如果我想要在console模式中就可以自動掛載USB隨身碟時,又要如何解決?

其中一種辦法是使用autofs,但是這種辦法是屬於比較被動的掛載。它是在有人訪問到某個資料匣時才會嘗試去檢查是否有可東西可以掛載在這個資料匣下。要更讓掛載更加自動化,可以改為使用udev的rules。

首先於 /dev/udev/rules.d 下建立檔案 99-usb-automount.rules:
KERNEL=="sd[a-z]1", RUN+="/usr/local/bin/usb_mount", ENV{REMOVE_CMD}="/usr/local/bin/usb_mount"


其中 /usr/local/bin/usb_mount 是一個shellscript,主要作用為依照udev所給的資訊進行掛載與卸載的動作。內容如下:
#!/bin/bash
# $Id$
#
# Script to automaticlly mount usb devices. This script is designed to work
# with udev
#

#commands used in the script
MOUNT="/bin/mount"
UMOUNT="/bin/umount -l"
MKDIR="/bin/mkdir -p"
RMDIR="/bin/rmdir"

#the root of where all auto mouted devices will be mounted to.
MOUNT_ROOT=/mnt
MNT_POINT=$MOUNT_ROOT/${DEVNAME##/dev/}

logger "running $0"
logger "ACTION=$ACTION ID_BUS=$ID_BUS DEVNAME=$DEVNAME
ID_FS_TYPE=$ID_FS_TYPE"

# check we are adding a usb device
if [ "$ID_BUS" != "usb" ]
then
logger "ignoring non usb device"
exit 0
fi

#check the action to process
case "$ACTION" in
add )
logger "mounting new device"

#create the directory we will mount to
$MKDIR "$MNT_POINT"

#
#mount the usb device with the following options
#
# ro - read only
# noexec - Do not allow direct execution of any binaries on the mounted file system
# nodev - Do not interpret character or block special devices on the file system
#
#$MOUNT -t $ID_FS_TYPE $DEVNAME $MNT_POINT -o ro,noexec,nodev
$MOUNT -t $ID_FS_TYPE $DEVNAME $MNT_POINT -o noexec,nodev

#check if mount suceeded
if [ $? -eq 0 ]
then
# sucess, we mounted ok.
logger "mounted $DEVNAME at $MNT_POINT"
else
# we failed to mount so remove the mount point
logger "failed to mount $DEVNAME at $MNT_POINT"
$RMDIR "$MNT_POINT"
fi
;;

remove )

logger "unmounting existing device"

# check if the device is really mounted
#if [ (grep -q "^$DEVNAME" /proc/mounts || grep -q "^$DEVNAME" /etc/mtab) ]
#then
# logger "$DEVNAME not mouted"
#endif

#unmount the device. We will refer using the mount point not the device name
#becase there is a posibility the device could be removed before we
#do the umount.
$UMOUNT "$MNT_POINT"

#remove the mount point
$RMDIR "$MNT_POINT"

logger "unmounted $DEVNAME and removed $MNT_POINT"
;;

* )
logger "ignoring unkown action"
;;

esac

這樣當USB儲存裝置一插入usb差槽後就可以看到裝置出現在 /mnt 下面。

原始文章來源在此
(想複製此script的人記得貼上後要檢查一下指令與註解有沒有因為html顯示的關係被斷行喔!)

2008年9月3日 星期三

OpenOffice中無法使用scim輸入法的解決方法

OpenOffice在Linux中使用XIM(X Input Method)作為輸入中文的方法,scim即是架構在XIM上。

為了要讓OpenOffice知道可以用scim輸入,需要在環境變數中加入下面這些項目。這幾行可以放在xorg啟動時會讀取的 ~/.xsession 之中,或是設定成全域變數。
export XMODIFIERS=@im=SCIM
export GTK_IM_MODULE="scim"
export QT_IM_MODULE="scim"

最後再用 "scim -d" 啟動輸入法即可。

如果這樣還不能在OpenOffice中啟動scim。則檢查一下在其他程式,如gnome-terminal中是否可以啟動scim。如果其他程式裡面可以啟動scim的話,代表OpenOffice有問題。可以到 ~/.xsession-error 中搜尋 "soffice" 看看是否有錯誤訊息。如果看到如下面所列的錯誤訊息:
(soffice:8397): Gtk-WARNING **: Loading IM context type 'scim' failed

(soffice:8397): Gtk-WARNING **: usr/lib/openoffice/program/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by /usr/lib/gtk-2.0/immodules/im-scim.so)

代表OpenOffice內附的libstdc++.so與libgcc_s.so與scim所編譯的版本不匹配。解決方法就是找出系統上的libstdc++.so與libgcc_s.so並且做個symbolic link過去。在一個使用4.3.1版gcc的gentoo linux中,指令會像下面這樣:
ln -s /usr/lib/gcc/i686-pc-linux-gnu/4.3.1/libstdc++.so.6 /usr/lib/openoffice/program/libstdc++.so.6
ln -s /usr/lib/gcc/i686-pc-linux-gnu/4.3.1/libgcc_s.so.1 /usr/lib/openoffice/program/libgcc_s.so.1