vsftpdをxinetdで動作させる
vistaから検証環境へのFTP接続をする機会はそうそう多くない。だから常時起動しているのはリソース的にもったいない。でもたまに必要。そんな時にいつも検証環境に一度ログインをして、rootになって、vsftpdのデーモンを起動するのにはちょっと手間がかかる。そういった『あまり頻繁には必要ではないけれども、必要な時は手間を掛けないで起動したい』と言ったものはxinetdで起動してしまおう。
xinetd
スーパーサーバと呼ばれ、とあるポートに対するアクセスがあった場合に、各ポートに対応するデーモンを起動する。
vsftpdの自動起動設定を削除
vsftpdが自動で起動されてしまう設定を削除しておく。
[worker@CentOS-5.2 ~]$ su - パスワード: [root@CentOS-5.2 ~]# [root@CentOS-5.2 ~]# chkconfig --list vsftpd vsftpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@CentOS-5.2 ~]# chkconfig --del vsftpd [root@CentOS-5.2 ~]# chkconfig --list vsftpd サービス vsftpd は chkconfig をサポートしますが実行レベルで参照されていません (run 'chkconfig --add vsftpd') [root@CentOS-5.2 ~]#
chkconfigコマンドで/etc/rc.d/?.d 配下のシンボリックリンクは削除してしまうが、/etc/rc.d/init.d/vsftpd自体は必要になるかもしれないのでファイルを削除はしない。
vsftpd設定変更
vsftpdをデーモン(サービス)で起動していた場合、下記の設定が有効になっていたはずである。
/etc/vsftpd/vsftpd.conf listen=YES
これはvsftpdがスタンドアローンで動作するという設定なので、xinetd経由で起動する場合には上記設定を変更する。コメントアウトで構わない。
/etc/vsftpd/vsftpd.conf # listen=YES
vsftpdのxinetd用ファイルを準備する
vsftpdをRPMでインストールしている場合には、xinetd用のファイルが既に準備されているのでコピーをする。もちろん下記のファイルを自身で記述しても問題ない。
[root@CentOS-5.2 ~]# cp /usr/share/doc/vsftpd-2.0.5/vsftpd.xinetd /etc/xinetd.d/vsftpd [root@CentOS-5.2 ~]#
ファイルを編集する。
1 : # default: off 2 : # description: The vsftpd FTP server serves FTP connections. It uses \ 3 : # normal, unencrypted usernames and passwords for authentication. 4 : service ftp 5 : { 6 : socket_type = stream 7 : wait = no 8 : user = root 9 : server = /usr/sbin/vsftpd 10: server_args = /etc/vsftpd/vsftpd.conf 11: nice = 10 12: disable = no 13: flags = IPv4 14: }
12行目の『disable』の値を『yes』から『no』へ編集する。
xinetd設定ファイル再読込
xinetdの設定ファイルを再読込みさせる。xinetdの再起動でも設定ファイルは再読込されるので、どちらでも構わない。
[root@CentOS-5.2 ~]# service xinetd reload 設定を再読み込み: [ OK ] [root@CentOS-5.2 ~]#
xinetdベースのサービス確認
下記コマンドでxinetdベースのサービスとして設定されているかを確認する。
[root@curious-52 ~]# chkconfig --list : xinetd ベースのサービス: : vsftpd: on
『xinetdベースのサービス』にvsftpdが入っている事、状態が『on』になっている事を確認する。状態が『off』の場合は/etc/xinetd.d/vsftpdファイルのdisable欄の編集が正常にされていないという事なので要確認。
ローカルから接続確認
vistaから接続ができればOK。
[root@CentOS-5.2 ~]# ps -ef | grep vsftpd root 7925 22855 0 21:04 pts/0 00:00:00 grep vsftpd [root@CentOS-5.2 ~]# ps -ef | grep vsftpd worker 7928 1 0 21:04 ? 00:00:00 vsftpd /etc/vsftpd/vsftpd.conf root 7930 22855 0 21:04 pts/0 00:00:00 grep vsftpd [root@CentOS-5.2 ~]# ps -ef | grep vsftpd root 7932 22855 0 21:05 pts/0 00:00:00 grep vsftpd [root@CentOS-5.2 ~]#
1回目のpsコマンド:vistaからFTP接続をする前
2回目のpsコマンド:vistaからFTP接続中
3回目のpsコマンド:vistaからのFTP接続を切断
上記を見てもらえればわかるように、必要な時にだけvsftpdが起動している状態になってくれた。満足♪
コマンド/ファイル
- コマンド
- /bin/su
- /sbin/chkconfig
- /sbin/service
- ファイル
- /etc/vsftpd/vsftpd.conf
- /etc/xinetd.d/vsftpd
RPMパッケージからファイルを取り出す
RPMパッケージの中から1つのファイルだけが欲しい!と言う事が(滅多に無いが極たまに)ある。そんな時はこの方法。
RPMパッケージはcpio形式で圧縮されているファイルのようだ。cpioファイルに変換してしまえば後は思うがまま。
[worker@CentOS-5.2 ~]$ rpm2cpio postgresql-server-8.1.11-1.el5_1.1.i386.rpm > postgresql-server-8.1.11-1.el5_1.1.i386.cpio
だが、cpioファイルにするまでもなく『|』を使う方法もある。
[worker@CentOS-5.2 ~]$ rpm2cpio postgresql-server-8.1.11-1.el5_1.1.i386.rpm | cpio --list :
これで該当RPMパッケージにどんなファイルが同梱されているかがわかる。これは下記コマンドと同等か。
[worker@CentOS-5.2 ~]$ rpm -qlp postgresql-server-8.1.11-1.el5_1.1.i386.rpm
取り出したいファイル名がわかればあとは簡単。
もっと簡単な方法があるのかもしれないけど、cpioコマンドをほとんど使わない自分はここまでしかわからなかった。
[worker@CentOS-5.2 ~]$ rpm2cpio postgresql-server-8.1.11-1.el5_1.1.i386.rpm | cpio -id '*postgresql'
これで『postgresql』で終わっているファイル(パス含む)が解凍される。
改善できればなぁというのが下記の内容
上記2点を改善できるcpioコマンドマスターのかた教えてください♪
自動起動スクリプト(rc)
PostgreSQLの自動起動スクリプト(rc)が欲しい。というわけで先日インストールしたソースの配下を漁ってみる。
…がいくら漁ってみても見つからない。で、googlingしてみたら皆さん自作しているようだ。でもgoogleで引っかかったものは正直なところ『う〜ん…』といった内容のもの。いやそんなに見てないけどさ。最低限の事はやってるけどあとちょっと足りないという感じ。そこでピンッと思いついた。RPMでもそもそもあるんだから自動起動スクリプトだって用意されているはずだよね。だったらそこから引っこ抜いてくれば良いじゃん!って。というわけで自動起動スクリプト(rc)を引っこ抜いてみる事にする。
RPMパッケージからファイルを取り出す
http://d.hatena.ne.jp/sin-j/20080812/1218527559
取り出してはみたものの、ふと思う。これって勝手に編集して使い回しても良いのかしら?んー…きっと大丈夫だよね。というわけで取り出したファイルを元に大幅に編集して検証環境用の自動起動スクリプトを作成する。
スクリプト
#!/bin/sh # postgresql This is the init script for starting up the PostgreSQL # server # # chkconfig: - 64 36 # description: Starts and stops the PostgreSQL backend daemon that handles \ # all database requests. # processname: postmaster # pidfile: /var/run/postmaster.pid # PGVERSION is the full package version, e.g., 7.4.7 PGVERSION=8.3.3 # PGMAJORVERSION is major version, e.g., 7.4 (this should match PG_VERSION) PGMAJORVERSION=`echo "$PGVERSION" | sed 's/^\([0-9]*\.[0-9]*\).*$/\1/'` # Source function library. . /etc/rc.d/init.d/functions # Get function listing for cross-distribution logic. TYPESET=`typeset -f|grep "declare"` # Get config. . /etc/sysconfig/network # Find the name of the script NAME=`basename $0` if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ] then NAME=${NAME:3} fi # For SELinux we need to use 'runuser' not 'su' if [ -x /sbin/runuser ] then SU=runuser else SU=su fi # Set defaults for configuration variables PGENGINE=/usr/local/postgresql-8.3.3/bin PGPORT=5432 PGDATA=/opt/postgresql-8.3.3 PGLOG=/opt/postgresql-8.3.3/pgstartup.log # Override defaults from /etc/sysconfig/pgsql if file is present [ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME} export PGDATA export PGPORT # Check that networking is up. # Pretty much need it for postmaster. [ "${NETWORKING}" = "no" ] && exit 1 [ -f "$PGENGINE/postmaster" ] || exit 1 script_result=0 start(){ PSQL_START=$"Starting ${NAME} service: " # Make sure startup-time log file is valid if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ] then touch "$PGLOG" || exit 1 chown postgres:postgres "$PGLOG" chmod go-rwx "$PGLOG" [ -x /usr/bin/chcon ] && /usr/bin/chcon -u system_u -r object_r -t postgresql_log_t "$PGLOG" 2>/dev/null fi # Check for the PGDATA structure if [ -f "$PGDATA/PG_VERSION" ] && [ -d "$PGDATA/base" ] then # Check version of existing PGDATA if [ x`cat "$PGDATA/PG_VERSION"` != x"$PGMAJORVERSION" ] then SYSDOCDIR="(Your System's documentation directory)" if [ -d "/usr/doc/postgresql-$PGVERSION" ] then SYSDOCDIR=/usr/doc fi if [ -d "/usr/share/doc/postgresql-$PGVERSION" ] then SYSDOCDIR=/usr/share/doc fi if [ -d "/usr/doc/packages/postgresql-$PGVERSION" ] then SYSDOCDIR=/usr/doc/packages fi if [ -d "/usr/share/doc/packages/postgresql-$PGVERSION" ] then SYSDOCDIR=/usr/share/doc/packages fi echo echo $"An old version of the database format was found." echo $"You need to upgrade the data format before using PostgreSQL." echo $"See $SYSDOCDIR/postgresql-$PGVERSION/README.rpm-dist for more information." exit 1 fi # No existing PGDATA! Initdb it. else echo -n $"Initializing database: " if [ ! -e "$PGDATA" -a ! -h "$PGDATA" ] then mkdir -p "$PGDATA" || exit 1 chown postgres:postgres "$PGDATA" chmod go-rwx "$PGDATA" fi # Clean up SELinux tagging for PGDATA [ -x /sbin/restorecon ] && /sbin/restorecon "$PGDATA" # Initialize the database $SU -l postgres -c "$PGENGINE/initdb --pgdata='$PGDATA' --auth='ident sameuser'" >> "$PGLOG" 2>&1 < /dev/null # Create directory for postmaster log mkdir "$PGDATA/pg_log" chown postgres:postgres "$PGDATA/pg_log" chmod go-rwx "$PGDATA/pg_log" [ -f "$PGDATA/PG_VERSION" ] && echo_success [ ! -f "$PGDATA/PG_VERSION" ] && echo_failure echo fi echo -n "$PSQL_START" $SU -l postgres -c "$PGENGINE/postmaster -p '$PGPORT' -D '$PGDATA' ${PGOPTS} &" >> "$PGLOG" 2>&1 < /dev/null sleep 2 pid=`pidof -s "$PGENGINE/postmaster"` if [ $pid ] && [ -f "$PGDATA/postmaster.pid" ] then success "$PSQL_START" touch /var/lock/subsys/${NAME} head -n 1 "$PGDATA/postmaster.pid" > "/var/run/postmaster.${PGPORT}.pid" echo else failure "$PSQL_START" echo script_result=1 fi } stop(){ echo -n $"Stopping ${NAME} service: " $SU -l postgres -c "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" > /dev/null 2>&1 < /dev/null ret=$? if [ $ret -eq 0 ] then echo_success else echo_failure script_result=1 fi echo rm -f "/var/run/postmaster.${PGPORT}.pid" rm -f "/var/lock/subsys/${NAME}" } restart(){ stop start } condrestart(){ [ -e /var/lock/subsys/${NAME} ] && restart } condstop(){ [ -e /var/lock/subsys/${NAME} ] && stop } reload(){ $SU -l postgres -c "$PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null } # This script is slightly unusual in that the name of the daemon (postmaster) # is not the same as the name of the subsystem (postgresql) # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status postmaster script_result=$? ;; restart) restart ;; condrestart) condrestart ;; condstop) condstop ;; reload|force-reload) reload ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|condstop|reload|force-reload}" exit 1 esac exit $script_result
正直なところここまでいらないなぁと思いつつも引っこ抜くのが面倒だったのでこれだけ。元からのやつにほとんど手を入れていませぬ。でも起動・停止ともにできたのでOKでしょ。
自動起動(rc)に登録しておく
[root@CentOS-5.2 ~]# chkconfig --list postgresql サービス postgresql は chkconfig をサポートしますが実行レベルで参照されていません (run 'chkconfig --add postgresql') [root@CentOS-5.2 ~]# chkconfig --add postgresql [root@CentOS-5.2 ~]# chkconfig --list postgresql postgresql 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@CentOS-5.2 ~]# chkconfig postgresql on [root@CentOS-5.2 ~]# chkconfig --list postgresql postgresql 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@CentOS-5.2 ~]#
確認。
[root@CentOS-5.2 ~]# ls -l /etc/rc.d/rc?.d/*postgresql lrwxrwxrwx 1 root root 20 8月 13 01:41 /etc/rc.d/rc0.d/K36postgresql -> ../init.d/postgresql lrwxrwxrwx 1 root root 20 8月 13 01:41 /etc/rc.d/rc1.d/K36postgresql -> ../init.d/postgresql lrwxrwxrwx 1 root root 20 8月 13 01:41 /etc/rc.d/rc2.d/S64postgresql -> ../init.d/postgresql lrwxrwxrwx 1 root root 20 8月 13 01:41 /etc/rc.d/rc3.d/S64postgresql -> ../init.d/postgresql lrwxrwxrwx 1 root root 20 8月 13 01:41 /etc/rc.d/rc4.d/S64postgresql -> ../init.d/postgresql lrwxrwxrwx 1 root root 20 8月 13 01:41 /etc/rc.d/rc5.d/S64postgresql -> ../init.d/postgresql lrwxrwxrwx 1 root root 20 8月 13 01:41 /etc/rc.d/rc6.d/K36postgresql -> ../init.d/postgresql [root@CentOS-5.2 ~]#
ちなみに…このK○○とS○○は、
- K:kill - 停止
- S:start - 起動
であり、番号は起動もしくは停止していく順番である。小さい番号から起動・停止を行っていく。この番号とrunlevelについてはそのうち。
追記
コメントを頂いたので追記。
ソースを展開したディレクトリ配下
contrib/start-scripts
[postgres@CentOS-5.2 start-scripts]$ find /usr/local/src/postgresql-8.3.3/contrib/start-scripts/ -type f /usr/local/src/postgresql-8.3.3/contrib/start-scripts/freebsd /usr/local/src/postgresql-8.3.3/contrib/start-scripts/linux /usr/local/src/postgresql-8.3.3/contrib/start-scripts/osx/install.sh /usr/local/src/postgresql-8.3.3/contrib/start-scripts/osx/PostgreSQL /usr/local/src/postgresql-8.3.3/contrib/start-scripts/osx/README /usr/local/src/postgresql-8.3.3/contrib/start-scripts/osx/StartupParameters.plist [postgres@CentOS-5.2 start-scripts]$
ほほぅ。freebsd, linux, osx用のモノが既に用意されているとのこと。linuxファイルを確認すると…こっちの方がすっきりしてる!RPMのパッケージから拾ってきた方は最初の起動等を考えられているのか(?)ディレクトリが無かったら作ったりなんかいろいろなチェックが入っているけど、starts-scripts/linuxの方はとってもスッキリ。検証サーバもスクリプトを置き換えだ!prefix, PGDATAのパスを書き換えて、serviceコマンドで動作確認。
> ishikawa84g 様
ありがとうございました。とーってもお勉強になりました。