2020年9月29日火曜日

Splunk 8.0.5をCentOS 7にインストールしてみた② (Universal Forwarderのインストール)

前回、Splunkの本体のインストール手順を記載した。

★前回の記事はこちら↓

本体のインストールが完了したので、次はログ送信側の設定を行う。Splunkではログ送信を行うエージェントとして、「Universal Forwarder」、「Light Forwarder」、「Heavy Forwarder」の3種類がある。それぞれの違いはSplunkの以下マニュアルにて記載されている。

ログを送信する用途だけであればUniversal Forwarderを、ログ送信の際にログの分析・加工が必要な場合はLight Forwarder、Heavy Forwarderを利用するようだ。なお、Universal Forwarderのインストーラは個別で用意されているが、Light Forwarder、Heavy Forwarderのインストーラは本体と同じインストーラを利用し、Splunk本体の機能の中からログ送信の機能のみ有効にする形で利用する。

今回は、Universal ForwarderをCentOSにインストールし、Splunkの検索画面にてログ検索ができることを確認する。

環境

  • OS : CentOS 7.6
  • インストール状態 : 最小限のインストール

Universal Forwarderインストール手順

1. インストーラのダウンロード

インストーラは以下URLからダウンロードできる。ダウンロードするためにはSplunkサイトのユーザ登録が必要となるので注意

2. 事前準備

インストール手順簡略化のため、firewalldとSELinuxは停止しておく。

# systemctl stop firewalld
# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux

3. インストール

Spulunk本体と同様にインストールはtgzファイルを解凍するだけよい。追加でパッケージインストールなども不要であり、インストール自体は極めてシンプルになるよう設計されている。

# tar xvzf splunkforwarder-8.0.5-a1a6394cc5ae-Linux-x86_64.tgz -C /opt

Universal Forwarderは「/opt/splunkforwarder」に解凍される。以下ディレクトリでよく使うものは以下となる。

ディレクトリ 説明
bin 実行ファイルが配置されている。特にsplunkコマンドは起動・停止や設定確認などで頻繁に使用する。
etc Universal Forwarderの設定ファイルとなるconfファイルが配置されている。CLIによる設定を行った内容は、本ディレクトリ配下のconfファイルに反映され保存される。なお、直接confファイルに設定追加することも可能。
# ls -l /opt/splunkforwarder/
合計 156
-r--r--r--.  1 10777 10777   841  7月  8 16:52 README-splunk.txt
drwxr-xr-x.  3 10777 10777  4096  7月  8 17:13 bin
-r--r--r--.  1 10777 10777    57  7月  8 16:49 copyright.txt
drwxr-xr-x. 13 10777 10777  4096  7月  8 17:10 etc
-rw-r--r--.  1 10777 10777     0  7月  8 17:10 ftr
drwxr-xr-x.  2 10777 10777    27  7月  8 17:10 include
drwxr-xr-x.  5 10777 10777  4096  7月  8 17:13 lib
-r--r--r--.  1 10777 10777 85709  7月  8 16:49 license-eula.txt
drwxr-xr-x.  3 10777 10777    58  7月  8 17:10 openssl
drwxr-xr-x.  4 10777 10777    63  7月  8 17:10 share
-r--r--r--.  1 10777 10777 50969  7月  8 17:13 splunkforwarder-8.0.5-a1a6394cc5ae-linux-2.6-x86_64-manifest

4. Universal Forwarderの初回起動と初期設定

「/opt/splunkforwarder/bin」ディレクトリにUniversal Forwarder本体の実行ファイルがあるので、以下の通り実行することで、初回起動時の以下処理が実行される。

  • ライセンス条項に同意
  • 管理ユーザ名の設定 (デフォルトはadmin)
  • 管理ユーザのパスワードの設定
# cd /opt/splunkforwarder/bin/
# ./splunk start

SPLUNK GENERAL TERMS

Last updated: February 13, 2020

These Splunk General Terms ("General Terms") between
Splunk Inc., a Delaware corporation, with its principal place
of business at 270 Brannan Street, San Francisco,
California 94107, U.S.A ("Splunk" or "we" or "us" or "our")
and you ("Customer" or "you" or "your") apply to the
purchase of licenses and subscriptions for Splunk's
Offerings. By clicking on the appropriate button, or by
downloading, installing, accessing or using the Offerings,
you agree to these General Terms. If you are entering into
these General Terms on behalf of Customer, you represent
that you have the authority to bind Customer. If you do not
agree to these General Terms, or if you are not authorized
to accept the General Terms on behalf of the Customer, do
not download, install, access, or use any of the Offerings.

See the General Terms Definitions Exhibit attached for
definitions of capitalized terms not defined herein.

1. License Rights

~(中略)~

"Statement of Work" means the statements of work and/or any all
applicable Orders that describe the specific services to be performed by
Splunk, including any materials and deliverables to be delivered by
Splunk.


SPLUNK GENERAL TERMS (v1.2020)


Do you agree with this license? [y/n]: y   ←★"y"を入力

This appears to be your first time running this version of Splunk.

Splunk software must create an administrator account during startup. Otherwise, you cannot log in.
Create credentials for the administrator account.
Characters do not appear on the screen when you type in credentials.

Please enter an administrator username: admin ←★管理者ユーザ名を入力
Password must contain at least:
   * 8 total printable ASCII character(s).
Please enter a new password:   ←★パスワードを入力
Please confirm new password:   ←★パスワードを再入力

Splunk> All batbelt. No tights.

Checking prerequisites...
        Checking mgmt port [8089]: open
                Creating: /opt/splunkforwarder/var/lib/splunk
                Creating: /opt/splunkforwarder/var/run/splunk
                Creating: /opt/splunkforwarder/var/run/splunk/appserver/i18n
                Creating: /opt/splunkforwarder/var/run/splunk/appserver/modules/static/css
                Creating: /opt/splunkforwarder/var/run/splunk/upload
                Creating: /opt/splunkforwarder/var/run/splunk/search_telemetry
                Creating: /opt/splunkforwarder/var/spool/splunk
                Creating: /opt/splunkforwarder/var/spool/dirmoncache
                Creating: /opt/splunkforwarder/var/lib/splunk/authDb
                Creating: /opt/splunkforwarder/var/lib/splunk/hashDb
New certs have been generated in '/opt/splunkforwarder/etc/auth'.
        Checking conf files for problems...
        Done
        Checking default conf files for edits...
        Validating installed files against hashes from '/opt/splunkforwarder/splunkforwarder-8.0.5-a1a6394cc5ae-linux-2.6-x86_64-manifest'
        All installed files intact.
        Done
All preliminary checks passed.

Starting splunk server daemon (splunkd)...
Done
                                                           [  OK  ]

5. 自動起動設定

tgzファイルを解凍しただけなので、これだけではサーバ再起動時に自動起動してくれないため、以下コマンドで自動起動するよう設定する。

# cd /opt/splunkforwarder/bin/
# ./splunk enable boot-start
Init script installed at /etc/init.d/splunk.
Init script is configured to run at boot.

6. 送信先のサーバ (インデクサー) を設定

Splunkのログ受信および解析を行う機能を「インデクサー」と呼ぶ。Universal Forwarderのログ送信先としてインデクサーを指定する必要があるため、splunk add forward-server <インデクサーのIPアドレス or ホスト名>:<ポート番号>コマンドにて行う。

# cd /opt/splunkforwarder/bin/
# ./splunk add forward-server 192.168.11.71:9997
Splunk username: admin
Password:
Added forwarding to: 192.168.11.71:9997.
# ./splunk list forward-server
Active forwards:
        None
Configured but inactive forwards:
        192.168.11.71:9997

本設定は、「/opt/splunkforwarder/etc/system/local/outputs.conf」に記述される。

# cat /opt/splunkforwarder/etc/system/local/outputs.conf
[tcpout]
defaultGroup = default-autolb-group

[tcpout:default-autolb-group]
server = 192.168.11.71:9997

[tcpout-server://192.168.11.71:9997]

7. Splunk本体 (インデクサー) 側で受信設定を追加

Splunkはデフォルトでは受信設定がされていない。Splunk本体の管理GUIにログインし、「設定」→「転送と受信」→「データの受信」を開いたのち、ポート番号「9997」にて新規作成を行う。

設定後に、Splunk本体で設定したポートでListenしていることを確認しておこう。

# ss -nl | grep 9997
tcp    LISTEN     0      128       *:9997                  *:*

8. モニター対象のログを追加

今回は例として/var/log配下のログをすべて監視対象として、Splunk本体のインデクサーに送信する。splunk add monitor <モニター対象のファイル or ディレクトリ>コマンドで設定する。

# cd /opt/splunkforwarder/bin/
# ./splunk add monitor /var/log
Added monitor of '/var/log'.

splunk list monitorで設定確認を行う。

# ./splunk list monitor
Monitored Directories:
        $SPLUNK_HOME/var/log/splunk
                /opt/splunkforwarder/var/log/splunk/btool.log
                /opt/splunkforwarder/var/log/splunk/first_install.log
                /opt/splunkforwarder/var/log/splunk/splunkd-utility.log
        $SPLUNK_HOME/var/log/splunk/splunkd.log
                /opt/splunkforwarder/var/log/splunk/splunkd.log
        $SPLUNK_HOME/var/log/watchdog/watchdog.log*
        $SPLUNK_HOME/var/run/splunk/search_telemetry/*search_telemetry.json
        $SPLUNK_HOME/var/spool/splunk/...stash_new
Monitored Files:
        $SPLUNK_HOME/etc/splunk.version
        /var/log   ←★対象が追加されている

設定の反映のため、Universal Forwarderをリスタートする。再起動は30秒ほど要したが問題なく完了した。

# ./splunk restart
Stopping splunkd...
Shutting down.  Please wait, as this may take a few minutes.
.............                                              [  OK  ]
Stopping splunk helpers...
                                                           [  OK  ]
Done.

Splunk> All batbelt. No tights.

Checking prerequisites...
        Checking mgmt port [8089]: open
        Checking conf files for problems...
        Done
        Checking default conf files for edits...
        Validating installed files against hashes from '/opt/splunkforwarder/splunkforwarder-8.0.5-a1a6394cc5ae-linux-2.6-x86_64-manifest'
        All installed files intact.
        Done
All preliminary checks passed.

Starting splunk server daemon (splunkd)...
Done
                                                           [  OK  ]

再度splunk list monitorで設定確認を行うと、/var/log配下のログファイルがファイル単位でモニター対象となっていることがわかる。

# ./splunk list monitor
Your session is invalid.  Please login.
Splunk username: admin
Password:
Monitored Directories:

~(中略)~

        /var/log
                /var/log/anaconda
                /var/log/anaconda/anaconda.log
                /var/log/anaconda/ifcfg.log
                /var/log/anaconda/journal.log
                /var/log/anaconda/ks-script-wAP9r2.log
                /var/log/anaconda/packaging.log
                /var/log/anaconda/program.log
                /var/log/anaconda/storage.log
                /var/log/anaconda/syslog
                /var/log/anaconda/X.log
                /var/log/audit
                /var/log/audit/audit.log
                /var/log/boot.log
                /var/log/btmp
                /var/log/chrony
                /var/log/cron
                /var/log/dmesg
                /var/log/dmesg.old
                /var/log/firewalld
                /var/log/grubby_prune_debug
                /var/log/lastlog
                /var/log/maillog
                /var/log/messages
                /var/log/rhsm
                /var/log/secure
                /var/log/spooler
                /var/log/tallylog
                /var/log/tuned
                /var/log/tuned/tuned.log
                /var/log/vmware-network.log
                /var/log/vmware-vgauthsvc.log.0
                /var/log/vmware-vmsvc.log
                /var/log/wtmp
Monitored Files:
        $SPLUNK_HOME/etc/splunk.version

以上で、Universal Forwarderの設定は完了となる。

Splunkにてログを検索してみる

実際に取得したログを検索してみよう。Splunkはログを解析するための設定として、「ソースタイプ」があり、ログの日付フォーマットやログのフィールドの内容 (たとえば、syslogなら日付情報の次にホスト名が来るなど) が定義されている。

以下マニュアルに記載の通り、Splunkはログの内容を分析し、適切なソースタイプが決定されるようだ。

Splunk software next attempts to use automatic source type recognition to match similar-looking files and assign a source type.
たとえば、以下のようにファイルごとにソースタイプが設定される。

実際の環境で確認すると、以下のように/var/log配下のログのソースタイプは、特にソースタイプを指定しなくとも自動的に以下のように適切に設定されていることがわかる。

ログファイル ソースタイプ
/var/log/audit/audit.log linux_audit
/var/log/messages syslog
/var/log/secure linux_secure


Universal Forwarderの起動・停止

インストール自体は完了しているが、Universal Forwarderの起動・停止のコマンドも紹介する。

コマンド 説明
/opt/splunkforwarder/bin/splunk start 起動
/opt/splunkforwarder/bin/splunk stop 停止
/opt/splunkforwarder/bin/splunk restart 再起動
/opt/splunkforwarder/bin/splunk status ステータス確認

以下実行例となる。

まとめ

以上でUniversal Forwarderインストールは完了となる。Splunk本体同様、tgzファイルを解凍して、いくつか設定をするだけですぐに利用することができる。一度設定手順を経験しておけば、次回以降は容易にインストール作業をすることができるだろう。

2020年9月26日土曜日

Splunk 8.0.5をCentOS 7にインストールしてみた① (Splunk本体のインストール)

ログ管理ツール (SIEM : Security Information and Event Management) で有名なソフトウェアである「Splunk Enterprise (以降、Splunk) 」は、インストール後60日間は無料トライアル版による全機能の利用が可能であり、さらに60日後は一部機能限定とはなるものの、そのまま「Splunk Free」として利用することができる (当然ライセンスを購入すれば商用版として利用が可能)。

各製品の機能比較は以下URLに記載がされている。

Splunkは、Windows、Linux、Mac OSと幅広いプラットフォームをサポートしている。今回はCentOS 7環境にSplunkを導入する手順を紹介する。

なお、今回はSplunk本体 (ログ受信側) のインストールまでを記載し、次回以降で「Splunk Universal Forwarder」を利用したログ送信およびSplunk本体にてログ受信を行う設定手順を記載する。

★↓Splunk Universal Forwarderのインストールと設定手順はこちら。

環境

  • OS : CentOS 7.6
  • インストール状態 : 最小限のインストール

Splunkインストール手順

1. インストーラのダウンロード

インストーラは以下URLからダウンロードできる。無料トライアル版もFree版も同じものがダウンロードできる。ダウンロードするためにはSplunkサイトのユーザ登録が必要となるので注意

.deb、.tgz、.rpmの3種類が用意されているが、ディストリビューションが変わっても手順に応用が利くことから、今回は「.tgz版」をダウンロードする。

2. 事前準備

インストール手順簡略化のため、firewalldとSELinuxは停止しておく。

# systemctl stop firewalld
# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux

3. インストール

インストールはtgzファイルを解凍するだけよい。追加でパッケージインストールなども不要であり、インストール自体は極めてシンプルになるよう設計されている。

# tar xvzf splunk-8.0.5-a1a6394cc5ae-Linux-x86_64.tgz -C /opt

Splunkは「/opt/splunk」に解凍される。以下ディレクトリでよく使うものは以下となる。

ディレクトリ 説明
bin 実行ファイルが配置されている。特にsplunkコマンドは起動・停止や設定確認などで頻繁に使用する。
etc Splunkの設定ファイルとなるconfファイルが配置されている。今回は詳しくは説明しないが、Web管理画面で設定追加されたものは、すべて本ディレクトリ配下のconfファイルに設定がテキストで書き込まれると考えて差し支えない。直接confファイルに設定追加することも可能。
# ls -l /opt/splunk/
合計 2916
-r--r--r--.  1 10777 10777     841  7月  8 16:52 README-splunk.txt
drwxr-xr-x.  4 10777 10777    4096  7月  8 17:12 bin
-r--r--r--.  1 10777 10777      57  7月  8 16:49 copyright.txt
drwxr-xr-x. 15 10777 10777    4096  7月  8 17:10 etc
-rw-r--r--.  1 10777 10777       0  7月  8 17:10 ftr
drwxr-xr-x.  4 10777 10777      62  7月  8 17:10 include
drwxr-xr-x.  8 10777 10777    4096  7月  8 17:13 lib
-r--r--r--.  1 10777 10777   85709  7月  8 16:49 license-eula.txt
drwxr-xr-x.  3 10777 10777      58  7月  8 17:10 openssl
drwxr-xr-x.  4 10777 10777     108  7月  8 17:10 share
-r--r--r--.  1 10777 10777 2875790  7月  8 17:13 splunk-8.0.5-a1a6394cc5ae-linux-2.6-x86_64-manifest

4. Splunkの初回起動と初期設定

「/opt/splunk/bin」ディレクトリにSplunk本体の実行ファイルがあるので、以下の通り実行することで、初回起動時の以下処理が実行される。

  • ライセンス条項に同意
  • 管理ユーザ名の設定 (デフォルトはadmin)
  • 管理ユーザのパスワードの設定
# cd /opt/splunk/bin/
# ./splunk start

SPLUNK GENERAL TERMS

Last updated: February 13, 2020

These Splunk General Terms ("General Terms") between
Splunk Inc., a Delaware corporation, with its principal place
of business at 270 Brannan Street, San Francisco,
California 94107, U.S.A ("Splunk" or "we" or "us" or "our")
and you ("Customer" or "you" or "your") apply to the
purchase of licenses and subscriptions for Splunk's
Offerings. By clicking on the appropriate button, or by
downloading, installing, accessing or using the Offerings,
you agree to these General Terms. If you are entering into
these General Terms on behalf of Customer, you represent
that you have the authority to bind Customer. If you do not
agree to these General Terms, or if you are not authorized
to accept the General Terms on behalf of the Customer, do
not download, install, access, or use any of the Offerings.

See the General Terms Definitions Exhibit attached for
definitions of capitalized terms not defined herein.

1. License Rights

~(中略)~

"Statement of Work" means the statements of work and/or any all
applicable Orders that describe the specific services to be performed by
Splunk, including any materials and deliverables to be delivered by
Splunk.


SPLUNK GENERAL TERMS (v1.2020)


Do you agree with this license? [y/n]: y   ←★"y"を入力

This appears to be your first time running this version of Splunk.

Splunk software must create an administrator account during startup. Otherwise, you cannot log in.
Create credentials for the administrator account.
Characters do not appear on the screen when you type in credentials.

Please enter an administrator username: admin ←★管理者ユーザ名を入力
Password must contain at least:
   * 8 total printable ASCII character(s).
Please enter a new password:   ←★パスワードを入力
Please confirm new password:   ←★パスワードを再入力
Copying '/opt/splunk/etc/openldap/ldap.conf.default' to '/opt/splunk/etc/openldap/ldap.conf'.
Generating RSA private key, 2048 bit long modulus
.........................................+++++
..............................+++++
e is 65537 (0x10001)
writing RSA key

~(中略)~

If you get stuck, we're here to help.
Look for answers here: http://docs.splunk.com

The Splunk web interface is at http://t1071splk:8000

5. 自動起動設定

tgzファイルを解凍しただけなので、これだけではサーバ再起動時に自動起動してくれないため、以下コマンドで自動起動するよう設定する。

# /opt/splunk/bin/splunk enable boot-start
Init script installed at /etc/init.d/splunk.
Init script is configured to run at boot.

試しに再起動を行ったのちステータスを確認したところ、問題なくSplunkが「running」ステータスとなっていた。

# /opt/splunk/bin/splunk status
splunkd is running (PID: 1616).
splunk helpers are running (PIDs: 1620 1635 1711 1831).

6. 管理Web画面にアクセス

以上を実施したのち「http://<インストールしたホスト名 or IPアドレス>:8000」にアクセスすると、管理画面が表示されるはずなので、初回起動時に設定した管理者ユーザとパスワードでログインしてみよう。

初回ログイン時のみ以下2つの警告が表示されるが、どちらも「了解です!」、「再度表示しない」を選択しておけばよい。


問題なければ以下のようなSplunkの管理画面が表示される。


Splunkの起動・停止

インストール自体は完了しているが、Splunkの起動・停止のコマンドも紹介する。

コマンド 説明
/opt/splunk/bin/splunk start 起動
/opt/splunk/bin/splunk stop 停止
/opt/splunk/bin/splunk restart 再起動
/opt/splunk/bin/splunk status ステータス確認

以下実行例となる。

停止

# /opt/splunk/bin/splunk stop
Stopping splunkd...
Shutting down.  Please wait, as this may take a few minutes.
..                                                         [  OK  ]
Stopping splunk helpers...
                                                           [  OK  ]
Done.

起動

# /opt/splunk/bin/splunk start

Splunk> All batbelt. No tights.

Checking prerequisites...
        Checking http port [8000]: open
        Checking mgmt port [8089]: open
        Checking appserver port [127.0.0.1:8065]: open
        Checking kvstore port [8191]: open
        Checking configuration... Done.
        Checking critical directories...        Done
        Checking indexes...
                Validated: _audit _internal _introspection _metrics _metrics_rollup _telemetry _thefishbucket history main summary
        Done
        Checking filesystem compatibility...  Done
        Checking conf files for problems...
        Done
        Checking default conf files for edits...
        Validating installed files against hashes from '/opt/splunk/splunk-8.0.5-a1a6394cc5ae-linux-2.6-x86_64-manifest'
        All installed files intact.
        Done
All preliminary checks passed.

Starting splunk server daemon (splunkd)...
Done
                                                           [  OK  ]

Waiting for web server at http://127.0.0.1:8000 to be available. Done


If you get stuck, we're here to help.
Look for answers here: http://docs.splunk.com

The Splunk web interface is at http://t1071splk:8000

ステータス確認

# /opt/splunk/bin/splunk status
splunkd is running (PID: 3105).
splunk helpers are running (PIDs: 3112 3126 3254 3308 3418).

まとめ

以上でSplunkインストールは完了となる。この状態からログ受信の設定を少し設定するだけでログ収集が開始できる。実施する前は、Splunkのインストールはもっと手間のかかるものと予想していたが、極めて容易かつ短時間でインストールできてしまうことに驚いた。

次回は「Splunk Universal Forwarder」をインストールし、LinuxのログをSplunkに送信し、ログ検索ができることまでを確認する。

2020年9月21日月曜日

【RHEL】コピーしたディスクを別のサーバにマウントする手順

前回、RHELにてコピーしたディスクを自分自身にマウントする手順を記事にした。

コピーしたディスクを自分自身にマウントする場合は、LVMやXFSのUUIDが重複するため、UUIDの変更を実施する必要があり、手順として手数の多いものになっていた。

今回は、自分自身ではなくコピーしたディスクを別のサーバにマウントする手順を記載する。一部手順は同一となっており、XFSのUUID変更が不要となるため、比較的シンプルな手順となる。

環境

  • 仮想環境 : ESXi 6.7
  • コピー元OS : RHEL 7
  • コピー元ディスク : OSインストール領域 /dev/sda2 16GB
  • マウント先OS : RHEL 8
  • 複製ディスク : /dev/sdb2

コピーしたディスクをマウントする手順

1. ディスクの状態確認

ディスクの状態確認として、PV、VG、LVの状態をそれぞれ確認してみる。別のサーバのディスクなので、VG名が重複するものの、UUIDは重複しないため警告メッセージは表示されない。

# pvs
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda3  rhel lvm2 a--   14.41g    0
  /dev/sdb2  rhel lvm2 a--  <15.00g    0

# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  rhel   1   2   0 wz--n- <15.00g    0
  rhel   1   2   0 wz--n-  14.41g    0

# lvs
  LV   VG   Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel -wi-------  13.39g                                                  
  root rhel -wi-ao---- <12.81g                                                  
  swap rhel -wi-------   1.60g                                                  
  swap rhel -wi-ao----   1.60g                                                  

2. vgimportcloneにて重複したVGをインポート

VG名を変更するため、前回同様、vgimportcloneにてVGのインポートを行う。インポート後、VG名が修正 (末尾に"1"が付与) されていれば問題ない。

# vgs
  VG   #PV #LV #SN Attr   VSize   VFree
  rhel   1   2   0 wz--n- <15.00g    0
  rhel   1   2   0 wz--n-  14.41g    0

# vgimportclone /dev/sdb2

# vgs
  VG    #PV #LV #SN Attr   VSize   VFree
  rhel    1   2   0 wz--n-  14.41g    0
  rhel1   1   2   0 wz--n- <15.00g    0

3. VGのアクティブ化

vgimportcloneを実行後のVGは非アクティブとなっている。非アクティブとなっていることは、vgdisplay -A表示されないことで確認できる。

# vgdisplay -A
  --- Volume group ---
  VG Name               rhel
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
~(以下略)~

VGを使用できるようにするため、以下コマンドでアクティブにする。

# vgchange -ay rhel1
  2 logical volume(s) in volume group "rhel1" now active

再度vgdisplay -Aで確認すると、VGが表示される (アクティブ化されている) ことがわかる。

# vgdisplay -A
  --- Volume group ---
  VG Name               rhel1
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
~(中略)~

  --- Volume group ---
  VG Name               rhel
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
~(以下略)~

4. マウントして確認

この段階でマウントできるようになっている。実際にマウントして確認した結果は以下の通り。

# mount /dev/mapper/rhel1-root /mnt/
# ls -l /mnt/
合計 12
lrwxrwxrwx.  1 root root    7  8月 30 03:25 bin -> usr/bin
drwxr-xr-x.  2 root root    6  8月 30 03:24 boot
drwxr-xr-x.  2 root root    6  8月 30 03:24 dev
drwxr-xr-x. 77 root root 8192  8月 30 03:31 etc
drwxr-xr-x.  2 root root    6 12月 14  2017 home
lrwxrwxrwx.  1 root root    7  8月 30 03:25 lib -> usr/lib
lrwxrwxrwx.  1 root root    9  8月 30 03:25 lib64 -> usr/lib64
drwxr-xr-x.  2 root root    6 12月 14  2017 media
drwxr-xr-x.  2 root root    6 12月 14  2017 mnt
drwxr-xr-x.  2 root root    6 12月 14  2017 opt
drwxr-xr-x.  2 root root    6  8月 30 03:24 proc
dr-xr-x---.  2 root root  135  8月 30 03:29 root
drwxr-xr-x.  2 root root    6  8月 30 03:24 run
lrwxrwxrwx.  1 root root    8  8月 30 03:25 sbin -> usr/sbin
drwxr-xr-x.  2 root root    6 12月 14  2017 srv
drwxr-xr-x.  2 root root    6  8月 30 03:24 sys
drwxrwxrwt.  9 root root  202  8月 30 03:31 tmp
drwxr-xr-x. 13 root root  155  8月 30 03:25 usr
drwxr-xr-x. 19 root root  267  8月 30 03:28 var

まとめ

自分自身のマウントする場合と異なり、他サーバへのマウントはvgimportcloneによるVGインポートとVGのアクティブ化のみでマウントできるようになる。そのため、手順としても比較的シンプルになった。

2020年9月20日日曜日

【RHEL】コピーしたディスクを自分自身にマウントする手順

最近のストレージは、ディスクボリュームのデータ静止点をスナップショットにて即座に取得する機能が備わっている。さらにそのスナップショットを使って別のボリュームをコピー (クローンともいう) することも簡単にできる。これらのスナップショットの機能は、個人向けのQNAPのNASやFreeNASでさえも使用できる機能となっている

ただし、スナップショットからコピーしたボリュームは原則ストレージ上でファイルの中身を確認することができないため、一度ファイルシステムを閲覧できるサーバにマウントする必要がある。

Windows Serverであれば深く考えずにコピーしたディスクを接続するだけで問題なくマウントできる。しかし、RHELの場合は、LVMやXFSを使用している関係から、ひと手間が必要となる。

今回は、RHELの環境において、コピーしたディスクを別のマウントポイントにマウントするための手順を記載する

なお、コピーしたディスクを別のサーバにマウントする手順は以下を参照すること。


環境

  • 仮想環境 : ESXi 6.7
  • OS : RHEL 7
  • ディスク : OSインストール領域 /dev/sda2 16GB
  • 複製ディスク : /dev/sdb2

なお、複製したディスクは、ESXiのvmkfstoolsを使用して作成した。参考までに実行コマンドを記載しておく。

# vmkfstools -i /vmfs/volumes/ssd_local_02/rhel7/rhel7.vmdk /vmfs/volumes/ssd_local_02/rhel7/rhel7-clone.vmdk
Destination disk format: VMFS zeroedthick
Cloning disk '/vmfs/volumes/ssd_local_02/rhel7/rhel7.vmdk'...
Clone: 100% done.

コピーしたディスクは以下の通りUUIDが同じとなっている。

#  vmkfstools -J getuuid /vmfs/volumes/ssd_local_02/rhel7/rhel7.vmdk
UUID is 60 00 C2 97 d9 eb af 77-9a b3 ed 24 0e 90 3b 52
#  vmkfstools -J getuuid /vmfs/volumes/ssd_local_02/rhel7/rhel7-clone.vmdk
UUID is 60 00 C2 9c 45 ba 73 2a-f0 2e 18 ca 9b db 42 42

なお、今回はvmdkファイルのUUIDが重複していても仮想マシンに既存のディスクとして問題なく追加することができたが、もし失敗する場合は、以下コマンドでUUIDを変更してリトライするとよいだろう。

#  vmkfstools -J setuuid /vmfs/volumes/ssd_local_02/rhel7/rhel7-clone.vmdk

コピーしたディスクをマウントする手順

1. ディスクの状態確認

ディスクの状態確認として、PV、VG、LVの状態をそれぞれ確認してみる。PVの情報が重複していることにより、「duplicate PVs were found」のメッセージが表示されていることがわかる。

# pvs
  WARNING: Not using lvmetad because duplicate PVs were found.
  WARNING: Use multipath or vgimportclone to resolve duplicate PVs?
  WARNING: After duplicates are resolved, run "pvscan --cache" to enable lvmetad.
  WARNING: Not using device /dev/sdb2 for PV FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey.
  WARNING: PV FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey prefers device /dev/sda2 because device is used by LV.
  PV         VG   Fmt  Attr PSize   PFree
  /dev/sda2  rhel lvm2 a--  <15.00g    0

# vgs
  WARNING: Not using lvmetad because duplicate PVs were found.
  WARNING: Use multipath or vgimportclone to resolve duplicate PVs?
  WARNING: After duplicates are resolved, run "pvscan --cache" to enable lvmetad.
  WARNING: Not using device /dev/sdb2 for PV FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey.
  WARNING: PV FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey prefers device /dev/sda2 because device is used by LV.
  VG   #PV #LV #SN Attr   VSize   VFree
  rhel   1   2   0 wz--n- <15.00g    0

# lvs
  WARNING: Not using lvmetad because duplicate PVs were found.
  WARNING: Use multipath or vgimportclone to resolve duplicate PVs?
  WARNING: After duplicates are resolved, run "pvscan --cache" to enable lvmetad.
  WARNING: Not using device /dev/sdb2 for PV FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey.
  WARNING: PV FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey prefers device /dev/sda2 because device is used by LV.
  LV   VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel -wi-ao---- 13.39g
  swap rhel -wi-ao----  1.60g

2. vgimportcloneにて重複したVGをインポート

重複したLVM情報を修正し使用できるようにするコマンドとしてvgimportcloneがある。以下、man vgimportcloneの結果から引用となるが、「vgimportcloneは、ハードウェアスナップショットによって既存のPVから作成したことで重複しているPVから、VGをインポートする」と記載されており、完全に今回の用途のみに特化したコマンドとなる。

DESCRIPTION
vgimportclone imports a VG from duplicated PVs, e.g. created by a hardware snapshot of existing PVs.

A duplicated VG cannot used until it is made to coexist with the original VG. vgimportclone renames the VG associated with the specified PVs and changes the associated VG and PV UUIDs.

実際にvgimportcloneを実行してみよう。UUIDが変わることを確認するため、前後でblkidコマンドを実行している。

# blkid | grep UUID | grep sdb2
/dev/sdb2: UUID="FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey" TYPE="LVM2_member"

# vgimportclone /dev/sdb2
  WARNING: Not using device /dev/sdb2 for PV FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey.
  WARNING: PV FbbJX5-u5fM-JLHS-nV4k-2kEn-k1CO-d9fUey prefers device /dev/sda2 because device is used by LV.

# blkid | grep UUID | grep sdb2
/dev/sdb2: UUID="z6QKgZ-kaFi-egjF-0GBo-cLHd-fmKc-HNb6ZP" TYPE="LVM2_member"

vgimportcloneを実行したことによって重複していたPVのUUIDの変更とVG名が修正 (末尾に"1"が付与) されており、LVM情報を出力した際に表示されていた警告メッセージも解消されていることがわかる。

# pvs
  PV         VG    Fmt  Attr PSize   PFree
  /dev/sda2  rhel  lvm2 a--  <15.00g    0
  /dev/sdb2  rhel1 lvm2 a--  <15.00g    0

# vgs
  VG    #PV #LV #SN Attr   VSize   VFree
  rhel    1   2   0 wz--n- <15.00g    0
  rhel1   1   2   0 wz--n- <15.00g    0

# lvs
  LV   VG    Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root rhel  -wi-ao---- 13.39g
  swap rhel  -wi-ao----  1.60g
  root rhel1 -wi------- 13.39g
  swap rhel1 -wi-------  1.60g

3. VGのアクティブ化

vgimportcloneを実行後のVGは非アクティブとなっている。非アクティブとなっていることは、vgdisplay -A表示されないことで確認できる。

# vgdisplay -A
  --- Volume group ---
  VG Name               rhel
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
~(以下略)~

VGを使用できるようにするため、以下コマンドでアクティブにする。

# vgchange -ay rhel1
  2 logical volume(s) in volume group "rhel1" now active

再度vgdisplay -Aで確認すると、VGが表示される (アクティブ化されている) ことがわかる。

# vgdisplay -A
  --- Volume group ---
  VG Name               rhel1
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
~(中略)~

  --- Volume group ---
  VG Name               rhel
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
~(以下略)~

4. xfsファイルシステムのUUID変更

RHEL 7以降は標準でXFSが使用されているが、XFSにもUUIDが存在しており、重複したままではマウントすることができない。XFSのUUID変更は、xfs_admin -U generate <ストレージデバイス>にて行う。

# xfs_admin -U generate /dev/mapper/rhel1-root
Clearing log and setting UUID
writing all SBs
new UUID = aea68ec1-63cc-445a-90d2-a37f2b0df0c9

上記コマンドを実行した際に、以下エラーで失敗する場合がある。

# xfs_admin -U generate /dev/mapper/rhel1-root
ERROR: The filesystem has valuable metadata changes in a log which needs to
be replayed.  Mount the filesystem to replay the log, and unmount it before
re-running xfs_admin.  If you are unable to mount the filesystem, then use
the xfs_repair -L option to destroy the log and attempt a repair.
Note that destroying the log may cause corruption -- please attempt a mount
of the filesystem before doing this.

いろいろメッセージが書いてあるが、記載されている通り、xfs_repair -Lを使うことで対処する。問題なく修復できたら、再度xfs_adminコマンドにてUUID変更を実施すればよい。

# xfs_repair -L /dev/mapper/rhel1-root
Phase 1 - find and verify superblock...
Phase 2 - using internal log
        - zero log...
ALERT: The filesystem has valuable metadata changes in a log which is being
destroyed because the -L option was used.
        - scan filesystem freespace and inode maps...
agi unlinked bucket 27 is 422747 in ag 1 (inode=4617051)
agi unlinked bucket 30 is 422750 in ag 1 (inode=4617054)
sb_fdblocks 1350116, counted 1358308
        - found root inode chunk

~(中略)~

Phase 7 - verify and correct link counts...
Maximum metadata LSN (7:9383) is ahead of log (1:2).
Format log to cycle 10.
done

5. マウントして確認

ここまで対処することで、ようやくファイルシステムをマウントすることができるので、実際にマウントしてファイルが閲覧できることを確認してみよう。

# mount /dev/mapper/rhel1-root /mnt/
# ls -l /mnt/
合計 12
lrwxrwxrwx.  1 root root    7  8月 30 16:25 bin -> usr/bin
drwxr-xr-x.  2 root root    6  8月 30 16:24 boot
drwxr-xr-x.  2 root root    6  8月 30 16:24 dev
drwxr-xr-x. 77 root root 8192  8月 30 16:31 etc
drwxr-xr-x.  2 root root    6 12月 15  2017 home
lrwxrwxrwx.  1 root root    7  8月 30 16:25 lib -> usr/lib
lrwxrwxrwx.  1 root root    9  8月 30 16:25 lib64 -> usr/lib64
drwxr-xr-x.  2 root root    6 12月 15  2017 media
drwxr-xr-x.  2 root root    6 12月 15  2017 mnt
drwxr-xr-x.  2 root root    6 12月 15  2017 opt
drwxr-xr-x.  2 root root    6  8月 30 16:24 proc
dr-xr-x---.  2 root root  135  8月 30 16:29 root
drwxr-xr-x.  2 root root    6  8月 30 16:24 run
lrwxrwxrwx.  1 root root    8  8月 30 16:25 sbin -> usr/sbin
drwxr-xr-x.  2 root root    6 12月 15  2017 srv
drwxr-xr-x.  2 root root    6  8月 30 16:24 sys
drwxrwxrwt.  9 root root  202  8月 30 16:31 tmp
drwxr-xr-x. 13 root root  155  8月 30 16:25 usr
drwxr-xr-x. 19 root root  267  8月 30 16:28 var

まとめ

以上で、RHELにおいてコピーしたディスクをマウントすることができた。しかし、実施しなければならない手順が多く、vgimportcloneのコマンドの存在を知らないとそもそもマウントさせることも難しいので、可能なら、コピーしたディスクは同じOSではなく別のOSなどにマウントする方が、手順としては簡単になると感じた。

2020年9月19日土曜日

QNAP NASのiSCSIボリュームをクローンして別のVMFSデータストアとしてマウントさせる手順

最近のストレージはスナップショット機能が充実しており、QNAPのNASのようなコンシューマー向けのNASですらスナップショット機能が使えるようになっている。

さらにスナップショットからクローンしたボリュームを作成する機能もあり、単純にスナップショットから元のボリュームをリストアするだけでなく、クローンしたボリュームから特定のファイル、あるいは仮想ハードディスク(vmdkファイル)だけをリストアするといった用途にも使用することができる。

vSphere環境でも、データストアとして利用しているボリュームをコピーし、別データストアとしてマウントすることで、特定の仮想マシンのvmdkファイルのみリストアするといった対応が可能であり、今回その手順を確認してみた。

なお、この手順はESXi単体では実施することができずvCenter Serverが必要となるため、注意すること。

QNAP NASでiSCSIボリュームをクローンする

1. スナップショットの作成

「ストレージ&スナップショット」を開き、「ストレージ」→「ストレージ/スナップショット」を選択する。


ボリュームの一覧が表示されるので、スナップショットを取得したいボリューム( 今回は「iscsi_qnap_01」)を右クリックし、「スナップショットを撮る」を選択する。


スナップショット取得時には、「読み取り/書き込み性能が永続的に低下する」旨のメッセージが表示される。これは、スナップショットを取得することで、ボリュームの差分ブロックの管理のオーバーヘッドが発生するため、影響が劣化することを示すメッセージとなる。


2. スナップショットタイプの選択

スナップショット取得時は、スナップショットタイプとして、以下2つが選択可能となる。
  • クラッシュコンシステント
  • アプリケーションコンシステント
アプリケーションコンシステントは、OSにエージェントの導入(vSphere連携の場合は、vCenter Serverとの連携設定)が必要となるため、今回は、「クラッシュコンシステント」を選択する。


3.  スナップショットのクローンを作成

スナップショット取得後、対象のボリュームのカメラマークをクリックすると、「スナップショットマネージャー」が起動する。


スナップショットマネージャーにて、クローンを行う対象のスナップショットを選択し、「クローン」ボタンを選択する。


クローン作成時に「クローンLUNのマッピング」にチェックすることで、マッピングも併せて実施することができる。今回は、iSCSIとしてマッピングが必要となるため、マッピングにチェックしてクローンを作成する。


4. クローンが作成されたことを確認

しばらく待つと、スナップショットからクローンが作成される。


コピーしたデータストアを別のデータストアとしてマウント

1. ESXiよりデバイスの確認

vCenter Serverにログインし、マウント対象の任意のESXiから「設定」→「ストレージデバイス」を選択し、クローンしたボリュームの認識状況を確認する。


私が検証した際は自動で認識していた。もし認識していないようであれば、ディスクの再スキャンをすること。

2. データストアのマウント

クローンしたボリュームをデータストアしてマウントするため、ストレージツリーを表示させ、右クリック→「ストレージ」→「新しいデータストア」を選択する。


新しいデータストアの設定ウィザードが開くので、以下を設定する。
  • タイプ:VMFS
  • データストア名:Datastore ※後で自動で設定されるためデフォルトのままでOK
  • LUN:クローンしたボリューム



    3. データストアの署名の変更

    マウントオプションで データストアの署名に関する設定を選択できる。
    署名 xxx を持つ未解決の VMFS ボリュームがこのディスク上で検出されました。
    
    検出された VMFS ボリュームを同じ署名でマウントするのか、新しい署名でマウントするのか、またはディスクをフォーマットするのかを指定してください。
    
    新しい署名を割り当て
    ディスク上のデータは保持されます。新しい署名がデータストアに割り当てられて、仮想マシン構成ファイルから既存の署名へのリファレンスが更新されます。 データストアは、元の名前を使用してマウントされます。
    
    既存の署名を保持
    ディスク上のデータは保持されます。データストアは同じ署名を使用してマウントされます。 データストアは、元の名前を使用してマウントされます。
    
    ディスクをフォーマット
    現在のディスク レイアウトが破棄され、すべてのデータが完全に削除されます。
    
    「既存の署名を保持」を選んでしまうと、同じ署名を持つデータストアが重複することにより、以下のメッセージが表示されマウントに失敗する。
    操作に失敗しました。
    ホストの設定中にエラーが発生しました
    操作に失敗しました。診断レポート: Unable to mount this VMFS volume due to the original volume is still online
    


    そのためクローンしたボリュームの場合は、「新しい署名を割り当て」を選択する


    4. ウィザードの終了

    残りのウィザードはデフォルトを選択し終了させる。



    ウィザードを終了させると、「未解決のVMFSボリュームの再署名」というタスクが実行させる。再署名タスク終了後、「VMFSの再スキャン」が自動で実行される。


    5. マウント状態の確認

    ここまで問題なくできれば、ストレージツリーに「snap-xxxxxxxx-<元のデータストア名>」といった名前でデータストアが作成される。データストアの中身を確認すると、きちんと仮想マシンのvmdkファイルを認識していることがわかる。


    まとめ

    以上でクローンしたボリュームを別のデータストアとしてESXiにマウントすることができた。さらにここから、vmdkファイルを仮想マシンに接続することで、スナップショット取得タイミングのファイルなどを確認することができる。

    ただし、RHELなどでは同じUUIDを持つディスクは単純にはマウントできないため、その手順はなかなか複雑なものになる。その手順については、改めて別の記事することにする。

    参考

    人気の投稿