2010年10月16日

regeditのGUI画面以外でレジストリを変更する方法

regeditのGUI画面以外でレジストリを変更する手順例。

確認環境
  • Windows XP Pro SP3
手順例
お題
レジストリ設定にもいろいろあるが、本手順例では、以下のように「LANにプロキシサーバーを使用する」のON/OFF方法を扱う。
image
↑↓
image
方法1.レジストリファイル(*.reg)を使う方法
ONにする場合
以下の内容のテキストファイル"proxyon.reg"を作成し、作成したファイルをダブルクリックする。
Windows Registry Editor Version 5.00      
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]       
"ProxyEnable"=dword:00000001
OFFにする場合
以下の内容のテキストファイル"proxyoff.reg"を作成し、作成したファイルをダブルクリックする。
Windows Registry Editor Version 5.00      
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]       
"ProxyEnable"=dword:00000000
方法2.バッチプログラムから行う(*.regファイル編)
方法1の*.regファイルを前提に以下で実現可能。
ONにする場合
以下コマンドをバッチプログラムに組み込む
regedit /s proxyon.reg
OFFにする場合
以下コマンドをバッチプログラムに組み込む
regedit /s proxyoff.reg
方法3.バッチプログラムから行う(REGコマンドを使う編)
ONにする場合
以下コマンドをバッチプログラムに組み込む
reg add "HKEY_CURRENT_USER\Software\Microsoft\Wi      
ndows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
OFFにする場合
以下コマンドをバッチプログラムに組み込む
reg add "HKEY_CURRENT_USER\Software\Microsoft\Wi      
ndows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f 
方法4.VBスクリプト(WSH)から行う
ONにする場合
以下の内容のテキストファイル"proxyon.vbs"を作成し、作成したファイルをダブルクリックする。
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable","1", "REG_DWORD" 
OFFにする場合
以下の内容のテキストファイル"proxyoff.vbs"を作成し、作成したファイルをダブルクリックする。
Set WshShell=WScript.CreateObject("WScript.Shell") 
WshShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable","1", "REG_DWORD 
注意事項
  • レジストリ直接変更の結果をIEに反映するには、IEの再起動が必要
  • "Windows Registry Editor Version 5.00"の文字列は正確に記載が必要
    (無い、不正確などであると、レジストリファイルでは無いとはじかれる。)
以上

2010年10月14日

独自認証局でサイト証明書を作成する

独自認証局でサイト証明書を作成する手順例を示す。


前提

 
以下で作成した独自認証局を前提。
 
 

確認環境

 
       
  • Debian(etch)
  •    
  • openssl 0.9.8c-4etch4
 

手順

 
証明要求の受領とサイト証明書の発行
 
       
  • 証明要求をscp等で以下に持ってくる。※証明要求の発行の仕方はこちらを参照のこと。      
    ~# pwd
    
    /root
    
    ~# ls ./work/site_csr.pem
    
    ./work/site_csr.pem

     

  • サイト証明書を作成する。
       
    ~# openssl ca -out ./work/site_cert.pem -infiles ./work/site_csr.pem
    
    ...
    
    Enter pass phrase for ./demoCA/private/cakey.pem:
    
    ...
    
    Sign the certificate? [y/n]:y
    
    ...
    
    1 out of 1 certificate requests certified, commit? [y/n]y
    
    Write out database with 1 new entries
    
    Data Base Updated
    
    ~#

     

  • サイト証明書を確認する。
       
    ~# openssl x509 -in ./work/site_cert.pem -text

     

サイト証明書の送付
サイト証明書を設定したいWWWサーバにscp等で送付する。

スタックオーバーフロー

"DEBUG HACKS"記載のスタックオーバーフロー事例の追試結果を示す。

 

 

       
  • sum.cを準備する(ソースは参考文献の"DEBUG HACKS"を確認のこと)
  •    
  • コンパイルする      
    $ make sum
    cc     sum.c   -o sum

     
  •  

  • 実行する
       
    $ uname -a
    Linux localhost.localdomain 2.6.18-53.el5PAE #1 SMP Wed Oct 10 16:48:18 EDT 2007 i686 athlon i386 GNU/Linux $ ulimit -c unlimited
    $ ./sum
    セグメンテーション違反です (core dumped)
    $ ls core*
    core.16163

     
  •  

  • gdbで読み込む
       
    $ gdb -c core.16163
    ...
    Program terminated with signal 11, Segmentation fault.
    #0  0x08048454 in ?? ()
    (gdb) p $sp
    $1 = (void *) 0xbf3c0000
    (gdb) info files
    Local core dump file:
            `/home/nekonoshin/proc/sum/core.16163', file type elf32-i386.
            0x00314000 - 0x00315000 is load1
            0x008f4000 - 0x008f5000 is load3
            0x008f5000 - 0x008f6000 is load4
            0x00a32000 - 0x00a34000 is load6
            0x00a34000 - 0x00a35000 is load7
            0x00a35000 - 0x00a38000 is load8
            0x08049000 - 0x0804a000 is load10
            0xb7fef000 - 0xb7ff1000 is load11
            0xbf3c0000 - 0xbffc0000 is load12
    (gdb)

     

load12(stack領域のダンプ)が0xbf3c0000 - 0xbffc0000。スタックポインター($sp)の値、0xbf3c0000はstack領域の範囲外。

参考文献

     
  • DEBUG HACKS, P.53, "HACK#9 デバッグに必要なスタックの基礎知識"吉岡弘隆, 大和一洋, 大岩尚宏, 安部東洋, 吉田俊輔, 株式会社オライリー・ジャパン, ISBN978-4-87311-4040    

gdb - リモートデバッグ

gdbを使ってリモートデバッグを行う手順例を示す。


 

ロスプラットフォームでの開発や、CUIアプリの実際の実行画面を確認しながらのデバッグを行いたい場合にリモートデバッグを使う。

 

環境

 
+-------+    +---------+
|デバッグ|--->|デバッグ  |
|ホスト  |    |ターゲット|
+-------+    +---------+

実行例

デバッグターゲットとデバッグホストが同じだが、2つの端末を用いた例を解説。例は、端末1と端末2の2つの端末が同一マシンの同一ユーザにログインしている状態とする。

     
  • 端末1(デバッグターゲット)でテストプログラムを作成    
    guest@debian5:~$ cat tst.c
    #include <stdio.h>

    int main()
    {
            printf("h"); fflush(stdout);
            printf("e"); fflush(stdout);
            printf("l"); fflush(stdout);
            printf("l"); fflush(stdout);
            printf("o"); fflush(stdout);
            printf(" "); fflush(stdout);
            printf("w"); fflush(stdout);
            printf("o"); fflush(stdout);
            printf("r"); fflush(stdout);
            printf("l"); fflush(stdout);
            printf("d"); fflush(stdout);
            printf("\n"); fflush(stdout);

            return 0;
    }

    guest@debian5:~$ gcc -g tst.c -o tst
    guest@debian5:~$


     

     
  • 端末1(デバッグターゲット)でgdbserverを起動    
    guest@debian5:~$ gdbserver localhost:1234 tst
    Process tst created; pid = 3181
    Listening on port 1234
     
     
  • 端末2(デバッグホスト)でgdbを起動    
    guest@debian5:~$ gdb ./tst
    GNU gdb 6.8-debian
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i486-linux-gnu"...
    (gdb) break main
    Breakpoint 1 at 0x8048415: file tst.c, line 5.
    (gdb) target remote localhost:1234
    Remote debugging using localhost:1234
    [New Thread 3181]
    0xb7f07810 in ?? () from /lib/ld-linux.so.2
    (gdb) c
    Continuing.

    Breakpoint 1, main () at tst.c:5
    5               printf("h"); fflush(stdout);
    (gdb) s
    6               printf("e"); fflush(stdout);
    (gdb)
    7               printf("l"); fflush(stdout);
    (gdb)
    8               printf("l"); fflush(stdout);
    (gdb)
    9               printf("o"); fflush(stdout);
    (gdb)
    10              printf(" "); fflush(stdout);
    (gdb)
    11              printf("w"); fflush(stdout);
    (gdb)
    12              printf("o"); fflush(stdout);
    (gdb)
    13              printf("r"); fflush(stdout);
    (gdb)
    14              printf("l"); fflush(stdout);
    (gdb)
    15              printf("d"); fflush(stdout);
    (gdb)
    16              printf("\n"); fflush(stdout);
    (gdb)
    18              return 0;
    (gdb)
    19      }
    (gdb)
    0xb7dbb455 in __libc_start_main () from /lib/i686/cmov/libc.so.6
    (gdb) c
    Continuing.

    Program exited normally.
    (gdb)


     

     
  • 端末1(デバッグターゲット)の表示に以下が追加される(特に"hello world"が端末2のステップ実行に合わせて1文字づつ表示されている状況が必見)    
    Remote debugging from host 127.0.0.1
    hello world

    Child exited with retcode = 0

    Child exited with status 0
    GDBserver exiting
    guest@debian5:~$


     

     
  • 因みに、リモートデバッグをやらずにローカルで直接デバッグすると以下の様になる。 ※"hello world"がデバッガの出力と混ざってしまっている。    
    guest@debian5:~$ gdb ./tst
    GNU gdb 6.8-debian
    Copyright (C) 2008 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "i486-linux-gnu"...
    (gdb) break main
    Breakpoint 1 at 0x8048415: file tst.c, line 5.
    (gdb) r
    Starting program: /home/guest/tst

    Breakpoint 1, main () at tst.c:5
    5               printf("h"); fflush(stdout);
    (gdb) s
    h6              printf("e"); fflush(stdout);
    (gdb)
    e7              printf("l"); fflush(stdout);
    (gdb)
    l8              printf("l"); fflush(stdout);
    (gdb)
    l9              printf("o"); fflush(stdout);
    (gdb)
    o10             printf(" "); fflush(stdout);
    (gdb)
    11             printf("w"); fflush(stdout);
    (gdb)
    w12             printf("o"); fflush(stdout);
    (gdb)
    o13             printf("r"); fflush(stdout);
    (gdb)
    r14             printf("l"); fflush(stdout);
    (gdb)
    l15             printf("d"); fflush(stdout);
    (gdb)
    d16             printf("\n"); fflush(stdout);
    (gdb)

    18              return 0;
    (gdb)
    19      }
    (gdb)
    0xb7e6f455 in __libc_start_main () from /lib/i686/cmov/libc.so.6
    (gdb) c
    Continuing.

    Program exited normally.
    (gdb)


     

備考

     
  • RHELをターゲットにリモートデバッグを行うとSIGTRAPが大量に発生する。正しい対処法かどうかはわからないが、以下で抑制可能。    
    (gdb) handle SIGTRAP nostop noprint nopass
    SIGTRAP is used by the debugger.
    Are you sure you want to change it? (y or n) y

    Signal        Stop      Print   Pass to program Description
    SIGTRAP       No        No      No              Trace/breakpoint trap
    (gdb)


     

参考資料

     
  • "info gdb"の"Remote Debugging"

CentOSでcrashコマンドを試してみる

CentOSでcrashコマンドでカーネルデバッグできる環境を構築する手順例を示す。


まず何も考えずcrashコマンドを叩いてみる。   

# crash

crash 4.0-5.0.3.el5.centos
Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008  Red Hat, Inc.
Copyright (C) 2004, 2005, 2006  IBM Corporation
Copyright (C) 1999-2006  Hewlett-Packard Co
Copyright (C) 2005, 2006  Fujitsu Limited
Copyright (C) 2006, 2007  VA Linux Systems Japan K.K.
Copyright (C) 2005  NEC Corporation
Copyright (C) 1999, 2002, 2007  Silicon Graphics, Inc.
Copyright (C) 1999, 2000, 2001, 2002  Mission Critical Linux, Inc.
This program is free software, covered by the GNU General Public License,
and you are welcome to change it and/or distribute copies of it under
certain conditions.  Enter "help copying" to see the conditions.
This program has absolutely no warranty.  Enter "help warranty" for details.

crash: cannot find booted kernel -- please enter namelist argument

Usage:
  crash [-h [opt]][-v][-s][-i file][-d num] [-S] [mapfile] [namelist] [dumpfile]

Enter "crash -h" for details.
#

なにもエラーが出なければ既に設定はされており、手順はここで終了。

上記の様にエラーがでるなら、http://debuginfo.centos.org/からkernel-debuginfoをダウンロードしてくる。

例では以下をダウンロードしたした。※現在動作中のカーネル版数と同じにすること。

インストールから動作確認まで、以下手順で行う。

# rpm -ihv kernel-debuginfo-common-2.6.18-92.1.1.el5.i686.rpm
# rpm -ihv kernel-debuginfo-2.6.18-92.1.22.el5.i686.rpm
# crash

crash 4.0-5.0.3.el5.centos
Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008  Red Hat, Inc.
Copyright (C) 2004, 2005, 2006  IBM Corporation
Copyright (C) 1999-2006  Hewlett-Packard Co
Copyright (C) 2005, 2006  Fujitsu Limited
Copyright (C) 2006, 2007  VA Linux Systems Japan K.K.
Copyright (C) 2005  NEC Corporation
Copyright (C) 1999, 2002, 2007  Silicon Graphics, Inc.
Copyright (C) 1999, 2000, 2001, 2002  Mission Critical Linux, Inc.
This program is free software, covered by the GNU General Public License,
and you are welcome to change it and/or distribute copies of it under
certain conditions.  Enter "help copying" to see the conditions.
This program has absolutely no warranty.  Enter "help warranty" for details.

GNU gdb 6.1
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...

      KERNEL: /usr/lib/debug/lib/modules/2.6.18-92.1.22.el5/vmlinux
    DUMPFILE: /dev/crash
        CPUS: 1
        DATE: Thu Mar  5 12:22:00 2009
      UPTIME: 02:24:35
LOAD AVERAGE: 2.14, 0.97, 0.65
       TASKS: 159
    NODENAME: localhost.localdomain
     RELEASE: 2.6.18-92.1.22.el5
     VERSION: #1 SMP Tue Dec 16 12:03:43 EST 2008
     MACHINE: i686  (1728 Mhz)
      MEMORY: 256 MB
         PID: 7203
     COMMAND: "crash"
        TASK: c75ef550  [THREAD_INFO: c6d1a000]
         CPU: 0
       STATE: TASK_RUNNING (ACTIVE)

crash>

完了。

gdbでcore dumpをいろんなパターンで調べてみる

gdbの使い方を知るため、各種試行した内容を以下に残す。


 

※具体的な各ファイルの読み込ませ方まで記載していない。後日改善予定。

 

試行1.core dumpのみでgdbを起動してみる。 

 
$ gdb -c core.7030 
GNU gdb Red Hat Linux (6.5-37.el5_2.2rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
(no debugging symbols found)
Using host libthread_db library "/lib/libthread_db.so.1".
Core was generated by `./test_core'.
Program terminated with signal 8, Arithmetic exception.
#0  0x0012f3c3 in ?? ()
(gdb) where
#0  0x0012f3c3 in ?? ()
#1  0x00000001 in ?? ()
#2  0xbffedfc0 in ?? ()
#3  0x0012f3ac in ?? ()
#4  0x00207658 in ?? ()
#5  0xbffedfa8 in ?? ()
#6  0x0804845a in ?? ()
#7  0x001fa5d0 in ?? ()
#8  0xbffedfc0 in ?? ()
#9  0xbffee018 in ?? ()
#10 0x0021fdec in ?? ()
#11 0x00206ca0 in ?? ()
#12 0x08048480 in ?? ()
#13 0xbffee018 in ?? ()
#14 0x0021fdec in ?? ()
#15 0x00000001 in ?? ()
#16 0xbffee044 in ?? ()
#17 0xbffee04c in ?? ()
#18 0x00207810 in ?? ()
#19 0x00000000 in ?? ()
(gdb)

とりあえず、アドレス0x0012f3c3で落ちたのが分かる。

ただ、この状態だとマシン語を逆アセンブルしながら追わないことには分からない。

試行2.core dumpファイルとexeファイルを指定してみる。

$ gdb test_core -c core.7030 
GNU gdb Red Hat Linux (6.5-37.el5_2.2rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

warning: Can't read pathname for load map: 入力/出力エラーです.
Error while mapping shared library sections:
./liba.so.1: そのようなファイルやディレクトリはありません.
Error while reading shared library symbols:
./liba.so.1: そのようなファイルやディレクトリはありません.
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Error while reading shared library symbols:
./liba.so.1: そのようなファイルやディレクトリはありません.
Core was generated by `./test_core'.
Program terminated with signal 8, Arithmetic exception.
#0  0x0012f3c3 in ?? ()
(gdb) where
#0  0x0012f3c3 in ?? ()
#1  0x00000001 in ?? ()
#2  0xbffedfc0 in ?? ()
#3  0x0012f3ac in ?? ()
#4  0x00207658 in _r_debug ()
#5  0xbffedfa8 in ?? ()
#6  0x0804845a in main () at test_core.c:8
(gdb)

test_core.cの8行目から呼び出されてとんだ先の0x0012f3c3で落ちたのは分かるようになった。0x0012f3c3に何の処理があるのか分からない。情報が少なくてデバッグにはならない。

試行3.core dumpファイルとexeファイルとliba.so.1(自分で作った共有ライブラリ)で調べてみる 

$ gdb test_core -c core.7030 
GNU gdb Red Hat Linux (6.5-37.el5_2.2rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

warning: Can't read pathname for load map: 入力/出力エラーです.
Reading symbols from /home/takeshi/temp/liba.so.1...done.
Loaded symbols for ./liba.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./test_core'.
Program terminated with signal 8, Arithmetic exception.
#0  0x0012f3c3 in liba () at liba.c:6
6       liba.c: そのようなファイルやディレクトリはありません.
        in liba.c
(gdb) where
#0  0x0012f3c3 in liba () at liba.c:6
#1  0x0804845a in main () at test_core.c:8
(gdb)

liba.cの6行目の処理で落ちたことが分かるようになった。

試行4.さらにソースコードも読み込ませてみる

$ gdb test_core -c core.7030 
GNU gdb Red Hat Linux (6.5-37.el5_2.2rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

warning: Can't read pathname for load map: 入力/出力エラーです.
Reading symbols from /home/takeshi/temp/liba.so.1...done.
Loaded symbols for ./liba.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./test_core'.
Program terminated with signal 8, Arithmetic exception.
#0  0x0012f3c3 in liba () at liba.c:6
6               int i = 10 / 0;
(gdb) where
#0  0x0012f3c3 in liba () at liba.c:6
#1  0x0804845a in main () at test_core.c:8
(gdb)

liba.cの6行目のint i = 10 /0;の処理で落ちたことが分かる。0での除算は致命的エラーなので、落ちて当たり前だ。

おまけ.ソースコードliba.cの6行目を変更してみる

$ gdb test_core -c core.7030 
GNU gdb Red Hat Linux (6.5-37.el5_2.2rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

warning: Can't read pathname for load map: 入力/出力エラーです.
Reading symbols from /home/takeshi/temp/liba.so.1...done.
Loaded symbols for ./liba.so.1
Reading symbols from /lib/libc.so.6...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./test_core'.
Program terminated with signal 8, Arithmetic exception.
#0  0x0012f3c3 in liba () at liba.c:6

warning: Source file is more recent than executable.
6               /* comment add for test */
(gdb) where
#0  0x0012f3c3 in liba () at liba.c:6
#1  0x0804845a in main () at test_core.c:8
(gdb)

表示されるソースが/* comment add for test */に変わった。バイナリとソースのバージョンは正確に一致していないとデバッグは出来ないようだ。

2010年10月13日

ポートを開いているプロセスを特定する(Linux)

TCPおよびUDPポートを占有しているプロセスを特定する方法を示す。


[root@localhost ~]# netstat -nap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PI
D/Program name
tcp        0      0 0.0.0.0:961                 0.0.0.0:*                   LISTEN      24
75/rpc.statd
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      24
50/portmap
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      27
49/cupsd
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      27
81/sendmail: acce
tcp        0      0 :::22                       :::*                        LISTEN      27
38/sshd
tcp        0    148 ::ffff:192.168.201.129:22   ::ffff:192.168.201.1:3255   ESTABLISHED 31
42/1
udp        0      0 0.0.0.0:955                 0.0.0.0:*                               24
75/rpc.statd
udp        0      0 0.0.0.0:958                 0.0.0.0:*                               24
75/rpc.statd
udp        0      0 0.0.0.0:68                  0.0.0.0:*                               23
64/dhclient
udp        0      0 0.0.0.0:5353                0.0.0.0:*                               28
96/avahi-daemon:
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               24
50/portmap
udp        0      0 0.0.0.0:36469               0.0.0.0:*                               28
96/avahi-daemon:
udp        0      0 0.0.0.0:631                 0.0.0.0:*                               27
49/cupsd
udp        0      0 :::51878                    :::*                                    28
96/avahi-daemon:
udp        0      0 :::5353                     :::*                                    28
96/avahi-daemon:
...

>

2010年10月12日

ファイルを開いているプロセスを特定する方法(Linux)

ファイルを開いているプロセスを特定する方法を示す。

  • fuserで/dev/nullをオープンしているプロセスを特定
    [root@localhost ~]# fuser /dev/null
    /dev/null:             568  2364  2450  2475  2508  2588  2599  2603  2665  2684  2704  27
    24  2738  2761  2781  2789  2812  2845  2856  2866  2896  2897  2908  2909  2918  2922  29
    31  3002  3004  3006  3017  3112  3114  3138  3142
  • lsofで/dev/nullをオープンしているプロセスを特定
    [root@localhost ~]# lsof /dev/null
    COMMAND    PID      USER   FD   TYPE DEVICE SIZE NODE NAME
    udevd      568      root    0u   CHR    1,3      1446 /dev/null
    udevd      568      root    1u   CHR    1,3      1446 /dev/null
    udevd      568      root    2u   CHR    1,3      1446 /dev/null
    dhclient  2364      root    0u   CHR    1,3      1446 /dev/null
    dhclient  2364      root    1u   CHR    1,3      1446 /dev/null
    dhclient  2364      root    2u   CHR    1,3      1446 /dev/null
    portmap   2450       rpc    0u   CHR    1,3      1446 /dev/null
    portmap   2450       rpc    1u   CHR    1,3      1446 /dev/null
    portmap   2450       rpc    2u   CHR    1,3      1446 /dev/null
    rpc.statd 2475      root    0u   CHR    1,3      1446 /dev/null
    rpc.statd 2475      root    1u   CHR    1,3      1446 /dev/null
    rpc.statd 2475      root    2u   CHR    1,3      1446 /dev/null
    rpc.idmap 2508      root    0u   CHR    1,3      1446 /dev/null
    rpc.idmap 2508      root    1u   CHR    1,3      1446 /dev/null
    rpc.idmap 2508      root    2u   CHR    1,3      1446 /dev/null
    rpc.idmap 2508      root    3u   CHR    1,3      1446 /dev/null
    dbus-daem 2588      dbus    0u   CHR    1,3      1446 /dev/null
    dbus-daem 2588      dbus    1u   CHR    1,3      1446 /dev/null
    dbus-daem 2588      dbus    2u   CHR    1,3      1446 /dev/null
    dbus-daem 2588      dbus    4u   CHR    1,3      1446 /dev/null
    hcid      2599      root    0u   CHR    1,3      1446 /dev/null
    hcid      2599      root    1u   CHR    1,3      1446 /dev/null
    hcid      2599      root    2u   CHR    1,3      1446 /dev/null
    sdpd      2603      root    0u   CHR    1,3      1446 /dev/null
    sdpd      2603      root    1u   CHR    1,3      1446 /dev/null
    sdpd      2603      root    2u   CHR    1,3      1446 /dev/null
    pcscd     2665      root    0u   CHR    1,3      1446 /dev/null
    pcscd     2665      root    1u   CHR    1,3      1446 /dev/null
    pcscd     2665      root    2u   CHR    1,3      1446 /dev/null
    hidd      2684      root    0u   CHR    1,3      1446 /dev/null
    hidd      2684      root    1u   CHR    1,3      1446 /dev/null
    hidd      2684      root    2u   CHR    1,3      1446 /dev/null
    automount 2704      root    0u   CHR    1,3      1446 /dev/null
    automount 2704      root    1u   CHR    1,3      1446 /dev/null
    automount 2704      root    2u   CHR    1,3      1446 /dev/null
    acpid     2724      root    0r   CHR    1,3      1446 /dev/null
    sshd      2738      root    0u   CHR    1,3      1446 /dev/null
    sshd      2738      root    1u   CHR    1,3      1446 /dev/null
    sshd      2738      root    2u   CHR    1,3      1446 /dev/null
    xinetd    2761      root    0r   CHR    1,3      1446 /dev/null
    xinetd    2761      root    1r   CHR    1,3      1446 /dev/null
    xinetd    2761      root    2r   CHR    1,3      1446 /dev/null
    sendmail  2781      root    0r   CHR    1,3      1446 /dev/null
    sendmail  2781      root    1w   CHR    1,3      1446 /dev/null
    sendmail  2781      root    2w   CHR    1,3      1446 /dev/null
    sendmail  2789     smmsp    0r   CHR    1,3      1446 /dev/null
    sendmail  2789     smmsp    1w   CHR    1,3      1446 /dev/null
    sendmail  2789     smmsp    2w   CHR    1,3      1446 /dev/null
    crond     2812      root    0u   CHR    1,3      1446 /dev/null
    crond     2812      root    1u   CHR    1,3      1446 /dev/null
    crond     2812      root    2u   CHR    1,3      1446 /dev/null
    xfs       2845       xfs    0u   CHR    1,3      1446 /dev/null
    xfs       2845       xfs    1u   CHR    1,3      1446 /dev/null
    xfs       2845       xfs    2u   CHR    1,3      1446 /dev/null
    anacron   2856      root    0r   CHR    1,3      1446 /dev/null
    anacron   2856      root    1w   CHR    1,3      1446 /dev/null
    anacron   2856      root    2w   CHR    1,3      1446 /dev/null
    atd       2866      root    0u   CHR    1,3      1446 /dev/null
    atd       2866      root    1u   CHR    1,3      1446 /dev/null
    atd       2866      root    2u   CHR    1,3      1446 /dev/null
    avahi-dae 2896     avahi    0r   CHR    1,3      1446 /dev/null
    avahi-dae 2896     avahi    1w   CHR    1,3      1446 /dev/null
    avahi-dae 2896     avahi    2w   CHR    1,3      1446 /dev/null
    avahi-dae 2897     avahi    0r   CHR    1,3      1446 /dev/null
    avahi-dae 2897     avahi    1w   CHR    1,3      1446 /dev/null
    avahi-dae 2897     avahi    2w   CHR    1,3      1446 /dev/null
    hald      2908 haldaemon    0u   CHR    1,3      1446 /dev/null
    hald      2908 haldaemon    1u   CHR    1,3      1446 /dev/null
    hald      2908 haldaemon    2u   CHR    1,3      1446 /dev/null
    hald-runn 2909      root    0r   CHR    1,3      1446 /dev/null
    hald-runn 2909      root    1u   CHR    1,3      1446 /dev/null
    hald-runn 2909      root    2u   CHR    1,3      1446 /dev/null
    hald-addo 2918 haldaemon    0r   CHR    1,3      1446 /dev/null
    hald-addo 2918 haldaemon    1u   CHR    1,3      1446 /dev/null
    hald-addo 2918 haldaemon    2u   CHR    1,3      1446 /dev/null
    hald-addo 2922 haldaemon    0r   CHR    1,3      1446 /dev/null
    hald-addo 2922 haldaemon    1u   CHR    1,3      1446 /dev/null
    hald-addo 2922 haldaemon    2u   CHR    1,3      1446 /dev/null
    hald-addo 2931      root    0r   CHR    1,3      1446 /dev/null
    hald-addo 2931      root    1u   CHR    1,3      1446 /dev/null
    hald-addo 2931      root    2u   CHR    1,3      1446 /dev/null
    yum-updat 3002      root    0u   CHR    1,3      1446 /dev/null
    yum-updat 3002      root    1u   CHR    1,3      1446 /dev/null
    yum-updat 3002      root    2u   CHR    1,3      1446 /dev/null
    gam_serve 3004      root    0r   CHR    1,3      1446 /dev/null
    gam_serve 3004      root    1w   CHR    1,3      1446 /dev/null
    gam_serve 3004      root    2w   CHR    1,3      1446 /dev/null
    smartd    3006      root    0u   CHR    1,3      1446 /dev/null
    smartd    3006      root    1u   CHR    1,3      1446 /dev/null
    smartd    3006      root    2u   CHR    1,3      1446 /dev/null
    gdm-binar 3017      root    0u   CHR    1,3      1446 /dev/null
    gdm-binar 3017      root    1w   CHR    1,3      1446 /dev/null
    gdm-binar 3017      root    2w   CHR    1,3      1446 /dev/null
    gdm-binar 3112      root    0r   CHR    1,3      1446 /dev/null
    gdm-binar 3112      root    1u   CHR    1,3      1446 /dev/null
    gdm-binar 3112      root    2u   CHR    1,3      1446 /dev/null
    gdm-rh-se 3114      root    0r   CHR    1,3      1446 /dev/null
    gdm-rh-se 3114      root    2w   CHR    1,3      1446 /dev/null
    gdmgreete 3138       gdm    2u   CHR    1,3      1446 /dev/null
    sshd      3142      root    0u   CHR    1,3      1446 /dev/null
    sshd      3142      root    1u   CHR    1,3      1446 /dev/null
    sshd      3142      root    2u   CHR    1,3      1446 /dev/null
    [root@localhost ~]#

LVM上のファイルシステム(ext3)の拡張

LVM上のファイルシステム(ext3)の拡張する手順例を示す。

確認環境

  • RHEL 5.4 on VMware Server 2

  • ファイルシステム(ext3)をアンマウントする
    [root@localhost ~]# df
    Filesystem           1K-ブロック    使用   使用可 使用% マウント位置
    /dev/sda2             10154020   3557116   6072784  37% /
    /dev/sda1               256666     37292    206122  16% /boot
    tmpfs                    62192         0     62192   0% /dev/shm
    /dev/mapper/VG0001-LV0001
                             51559      4956     43941  11% /mnt
    [root@localhost ~]# umount /mnt
  • LV(論理ボリューム)を拡張する
    [root@localhost ~]# lvextend -L+12M /dev/VG0001/LV0001
      Extending logical volume LV0001 to 64.00 MB
      Logical volume LV0001 successfully resized
    [root@localhost ~]# vgdisplay -v VG0001
        Using volume group(s) on command line
        Finding volume group "VG0001"
      --- Volume group ---
      VG Name               VG0001
      System ID
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  5
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                1
      Open LV               0
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size               7.80 GB
      PE Size               4.00 MB
      Total PE              1998
      Alloc PE / Size       16 / 64.00 MB
      Free  PE / Size       1982 / 7.74 GB
      VG UUID               Fusrtt-uqYJ-uNdZ-fepf-5hpt-554b-zKQuIj
    
      --- Logical volume ---
      LV Name                /dev/VG0001/LV0001
      VG Name                VG0001
      LV UUID                V6JgvS-CD33-WvBo-TfE2-qRu2-765V-0YAhyR
      LV Write Access        read/write
      LV Status              available
      # open                 0
      LV Size                64.00 MB
      Current LE             16
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:0
    
      --- Physical volumes ---
      PV Name               /dev/sdb1
      PV UUID               EhMTvI-k3fq-eoRV-Hwkd-iA7G-p3Lk-QjSOsC
      PV Status             allocatable
      Total PE / Free PE    999 / 983
    
      PV Name               /dev/sdb2
      PV UUID               zbmif7-hWP2-2VA2-sFNp-vbSw-O7Ce-rMErmL
      PV Status             allocatable
      Total PE / Free PE    999 / 999
    
    [root@localhost ~]#
  • ファイルシステム(ext3)を拡張する
    [root@localhost ~]# e2fsck -nfv /dev/VG0001/LV0001
    e2fsck 1.39 (29-May-2006)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    
          11 inodes used (0.08%)
           1 non-contiguous inode (9.1%)
             # of inodes with ind/dind/tind blocks: 0/0/0
        6645 blocks used (12.48%)
           0 bad blocks
           0 large files
    
           0 regular files
           2 directories
           0 character device files
           0 block device files
           0 fifos
           0 links
           0 symbolic links (0 fast symbolic links)
           0 sockets
    --------
           2 files
    [root@localhost ~]# e2fsck -f /dev/VG0001/LV0001
    e2fsck 1.39 (29-May-2006)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    /dev/VG0001/LV0001: 11/13328 files (9.1% non-contiguous), 6645/53248 blocks
    [root@localhost ~]# resize2fs /dev/VG0001/LV0001
    resize2fs 1.39 (29-May-2006)
    Resizing the filesystem on /dev/VG0001/LV0001 to 65536 (1k) blocks.
    The filesystem on /dev/VG0001/LV0001 is now 65536 blocks long.
    
    [root@localhost ~]#
  • 再度マウントする
    [root@localhost ~]# mount /dev/VG0001/LV0001 /mnt
    [root@localhost ~]# df
    Filesystem           1K-ブロック    使用   使用可 使用% マウント位置
    /dev/sda2             10154020   3557124   6072776  37% /
    /dev/sda1               256666     37292    206122  16% /boot
    tmpfs                    62192         0     62192   0% /dev/shm
    /dev/mapper/VG0001-LV0001
                             63605      5163     55821   9% /mnt
    [root@localhost ~]#

LVM上のファイルシステム(ext3)の縮小

LVM上のファイルシステム(ext3)を縮小する手順例を示す。

確認環境

  • RHEL 5.4 on VMware Server 2

  • ファイルシステム(ext3)をアンマウントする
    [root@localhost ~]# df
    Filesystem           1K-ブロック    使用   使用可 使用% マウント位置
    /dev/sda2             10154020   3557124   6072776  37% /
    /dev/sda1               256666     37292    206122  16% /boot
    tmpfs                    62192         0     62192   0% /dev/shm
    /dev/mapper/VG0001-LV0001
                             63605      5163     55821   9% /mnt
    [root@localhost ~]# umount /mnt
    [root@localhost ~]#
  • ファイルシステム(ext3)を縮小する
    [root@localhost ~]# e2fsck -nfv /dev/VG0001/LV0001
    e2fsck 1.39 (29-May-2006)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    
          11 inodes used (0.07%)
           1 non-contiguous inode (9.1%)
             # of inodes with ind/dind/tind blocks: 0/0/0
        7094 blocks used (10.82%)
           0 bad blocks
           0 large files
    
           0 regular files
           2 directories
           0 character device files
           0 block device files
           0 fifos
           0 links
           0 symbolic links (0 fast symbolic links)
           0 sockets
    --------
           2 files
    [root@localhost ~]# e2fsck -f /dev/VG0001/LV0001
    e2fsck 1.39 (29-May-2006)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information
    /dev/VG0001/LV0001: 11/15232 files (9.1% non-contiguous), 7094/65536 blocks
    [root@localhost ~]# resize2fs /dev/VG0001/LV0001 53248
    resize2fs 1.39 (29-May-2006)
    Resizing the filesystem on /dev/VG0001/LV0001 to 53248 (1k) blocks.
    The filesystem on /dev/VG0001/LV0001 is now 53248 blocks long.
    
    [root@localhost ~]#
  • LV(論理ボリューム)を縮小する
    [root@localhost ~]# lvreduce -L-12M /dev/VG0001/LV0001
      WARNING: Reducing active logical volume to 52.00 MB
      THIS MAY DESTROY YOUR DATA (filesystem etc.)
    Do you really want to reduce LV0001? [y/n]: y
      Reducing logical volume LV0001 to 52.00 MB
      Logical volume LV0001 successfully resized
    [root@localhost ~]# vgdisplay -v VG0001
        Using volume group(s) on command line
        Finding volume group "VG0001"
      --- Volume group ---
      VG Name               VG0001
      System ID
      Format                lvm2
      Metadata Areas        2
      Metadata Sequence No  6
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                1
      Open LV               0
      Max PV                0
      Cur PV                2
      Act PV                2
      VG Size               7.80 GB
      PE Size               4.00 MB
      Total PE              1998
      Alloc PE / Size       13 / 52.00 MB
      Free  PE / Size       1985 / 7.75 GB
      VG UUID               Fusrtt-uqYJ-uNdZ-fepf-5hpt-554b-zKQuIj
    
      --- Logical volume ---
      LV Name                /dev/VG0001/LV0001
      VG Name                VG0001
      LV UUID                V6JgvS-CD33-WvBo-TfE2-qRu2-765V-0YAhyR
      LV Write Access        read/write
      LV Status              available
      # open                 0
      LV Size                52.00 MB
      Current LE             13
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:0
    
      --- Physical volumes ---
      PV Name               /dev/sdb1
      PV UUID               EhMTvI-k3fq-eoRV-Hwkd-iA7G-p3Lk-QjSOsC
      PV Status             allocatable
      Total PE / Free PE    999 / 986
    
      PV Name               /dev/sdb2
      PV UUID               zbmif7-hWP2-2VA2-sFNp-vbSw-O7Ce-rMErmL
      PV Status             allocatable
      Total PE / Free PE    999 / 999
    
    [root@localhost ~]# e2fsck -nfv /dev/VG0001/LV0001
    e2fsck 1.39 (29-May-2006)
    Pass 1: Checking inodes, blocks, and sizes
    Pass 2: Checking directory structure
    Pass 3: Checking directory connectivity
    Pass 4: Checking reference counts
    Pass 5: Checking group summary information 
    
          11 inodes used (0.08%)
           1 non-contiguous inode (9.1%)
             # of inodes with ind/dind/tind blocks: 0/0/0
        6645 blocks used (12.48%)
           0 bad blocks
           0 large files
    
           0 regular files
           2 directories
           0 character device files
           0 block device files
           0 fifos
           0 links
           0 symbolic links (0 fast symbolic links)
           0 sockets
    --------
           2 files
    [root@localhost ~]#
  • 再度マウントする
    [root@localhost ~]# mount /dev/VG0001/LV0001 /mnt
    [root@localhost ~]# df
    Filesystem           1K-ブロック    使用   使用可 使用% マウント位置
    /dev/sda2             10154020   3557132   6072768  37% /
    /dev/sda1               256666     37292    206122  16% /boot
    tmpfs                    62192         0     62192   0% /dev/shm
    /dev/mapper/VG0001-LV0001
                             51559      4956     45006  10% /mnt
    [root@localhost ~]#

LVMの削除

一度作成したLVを完全に削除する方法を示す。

確認環境

  • RHEL5.4 on VMware Server 2

  • ファイルシステムをアンマウントする
    • [root@localhost ~]# df
      Filesystem           1K-ブロック    使用   使用可 使用% マウント位置
      /dev/sda2             10154020   3557092   6072808  37% /
      /dev/sda1               256666     37292    206122  16% /boot
      tmpfs                    62192         0     62192   0% /dev/shm
      /dev/mapper/VG0001-LV0001
                               51559      4956     43941  11% /mnt
      [root@localhost ~]# umount /mnt
      [root@localhost ~]# 
  • LVを削除する
    • [root@localhost ~]# lvremove /dev/VG0001/LV0001
      Do you really want to remove active logical volume LV0001? [y/n]: y
        Logical volume "LV0001" successfully removed
      [root@localhost ~]#
  • VGを削除する
    • [root@localhost ~]# vgremove /dev/VG0001
        Volume group "VG0001" successfully removed
      [root@localhost ~]#
  • PVを削除する
特に手順はない。あえて書くなら、parted等でパーティションを消してしまうのが手順。

bashで無限ループ

bashシェルスクリプトで無限ループを書く方法を示す。
止めるまで何かの情報を表示し続けたいときなどに便利。



  • 1行無限ループ
[root@localhost ~]# while :; do echo hello; sleep 10; done
hello
hello
[root@localhost ~]#
  • スクリプトで無限ループ
[root@localhost ~]# cat mugen.sh
#!/bin/bash
while :
do
        echo hello
        sleep 10
done
[root@localhost ~]# sh ./mugen.sh
hello
hello
[root@localhost ~]#

2010年10月11日

KVM仮想マシンのVNCサーバ設定

KVM仮想マシンにVNCサーバを設定する方法を示す。

確認環境

  • CentOS5.5(Intel64)

設定手順

※KVM仮想マシン'test'に設定すると仮定。作業中はKVM仮想マシンは停止のこと。

1. /etc/libvirt/qemu/test.xml 定義ファイル編集

ファイル中の<graphicsで始まる行を以下のように編集する。
[編集前]
<graphics type='vnc' port='-1' autoport='yes' keymap='ja'/>
[編集後]
<graphics type='vnc' port='5950' autoport='no' listen='0.0.0.0' keymap='ja'/>

2. 定義の反映
# virsh define  /etc/libvirt/qemu/test.xml

以上