作業メモ

WEB検証サーバー構築

2024-10-12 22:09:02
2024-10-13 01:20:48
目次

作業内容

  • QEMUによる仮想化環境で、WEB動作検証のためRHEL8.6の仮想サーバーを準備する

手順1:事前準備

1. OS確認

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini ~ % sw_vers
   ProductName:	macOS
   ProductVersion:	12.6
   BuildVersion:	21G115
   taiyo@Mac-mini ~ % uname -v
   Darwin Kernel Version 21.6.0: Mon Aug 22 20:20:05 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T8101

2. QEMUインストール

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini ~ % brew install qemu
   ==> Downloading https://ghcr.io/v2/homebrew/core/gettext/manifests/0.21
   
   [中略]
   
   ==> Installing dependencies for qemu: gettext, libffi, pcre, python@3.9, glib, gmp, bdw-gc, m4, libtool, libunistring, pkg-config, guile, libidn2, libtasn1, nettle, p11-kit, libevent, libnghttp2, unbound, gnutls, jpeg, libpng, libslirp, libssh, libusb, lzo, ncurses, pixman, snappy and vde
   ==> Installing qemu dependency: gettext
   ==> Pouring gettext--0.21.catalina.bottle.tar.gz
   🍺  /usr/local/Cellar/gettext/0.21: 1,953 files, 19.0MB
   
   [中略]
   
   ==> Installing qemu dependency: vde
   ==> Pouring vde--2.3.2_1.catalina.bottle.tar.gz
   🍺  /usr/local/Cellar/vde/2.3.2_1: 73 files, 1.4MB
   ==> Installing qemu
   ==> Pouring qemu--6.1.0_1.catalina.bottle.tar.gz
   🍺  /usr/local/Cellar/qemu/6.1.0_1: 161 files, 552.5MB
   ==> Running `brew cleanup qemu`...
   Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
   Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

3. QEMUバージョン確認

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini ~ % qemu-system-x86_64 --version
   QEMU emulator version 6.1.0
   Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers

手順2:ゲストOS作成

1. 仮想イメージファイル準備

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini ~ % cd qemu-images 
   taiyo@Mac-mini qemu-images % mkdir 001
   taiyo@Mac-mini qemu-images % cd 001
   taiyo@Mac-mini 001 % qemu-img create qemu-001.img 20G
   Formatting 'qemu-001.img', fmt=raw size=21474836480

2. UEFIファイル準備

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini 001 % cp -a /opt/homebrew/share/qemu/edk2-aarch64-code.fd .
   taiyo@Mac-mini 001 % dd if=/dev/zero conv=sync bs=1m count=64 of=ovmf_vars.fd
   64+0 records in
   64+0 records out
   67108864 bytes transferred in 0.031322 secs (2142547219 bytes/sec)
   taiyo@Mac-mini 001 % ls -l
   total 262176
   -rw-r--r--  1 taiyo  admin     67108864  8 31 01:43 edk2-aarch64-code.fd
   -rw-r--r--  1 taiyo  staff     67108864 10  7 13:41 ovmf_vars.fd
   -rw-r--r--  1 taiyo  staff  21474836480 10  7 13:29 qemu-001.img

3. QEMU起動

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini 001 % /opt/homebrew/bin/qemu-system-aarch64 \
            -name 001_rh8-web-test -display default,show-cursor=on \
            -machine virt,highmem=off \
            -cpu cortex-a76 -smp 2 -m 2G -k ja \
            -device intel-hda -device hda-output \
            -device virtio-gpu-pci \
            -device qemu-xhci -device usb-tablet \
            -device usb-kbd \
            -monitor telnet::46001,server,nowait \
            -net nic,model=virtio \
            -net user,hostfwd=tcp:192.168.20.60:46101-:22 \
            -drive if=pflash,format=raw,file=/Users/taiyo/qemu-images/001/edk2-aarch64-code.fd,readonly=on \
            -drive if=pflash,format=raw,file=/Users/taiyo/qemu-images/001/ovmf_vars.fd \
            -drive if=virtio,format=raw,file=/Users/taiyo/qemu-images/001/qemu-001.img \
            -cdrom /Users/taiyo/Downloads/ISOs/rhel-8.6-aarch64-boot.iso \
            -boot order=d &

4. RHEL8.6をインストール

  • QEMUデフォルト画面に表示されるブートメニューで『Install Red Hat Enterprise Linux 8.6』を選択する

  • 数分後に表示されるインストーラーのメニューに従って、RHELをインストールする

  • ソフトウェア構成は『最小限のインストール』とする

  • インストールが完了した後に『システムの再起動』ボタンを押下する### 手順3:サーバー初期設定

手順3:サーバー初期設定

1. ログインする

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini 001 % ssh -p 46101 taiyo@192.168.20.60
   The authenticity of host '[192.168.20.60]:46101 ([192.168.20.60]:46101)' can't be established.
   ED25519 key fingerprint is SHA256:
   This key is not known by any other names
   Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
   Warning: Permanently added '[192.168.20.60]:46101' (ED25519) to the list of known hosts.
   taiyo@192.168.20.60's password: 
   [taiyo@virt001 ~]$ 

2. sudoユーザー追加

  • ターミナルから下記のコマンド操作を行う

   [taiyo@virt001 ~]$ su -
   パスワード:
   [root@virt001 ~]# usermod -G wheel taiyo
   [root@virt001 ~]# id taiyo
   uid=1000(taiyo) gid=1000(taiyo) groups=1000(taiyo),10(wheel)
   [root@virt001 ~]# cp -a /etc/sudoers /etc/sudoers.`date +%Y%m%d-%H%M%S`
   [root@virt001 ~]# visudo
   [root@virt001 ~]# diff -U 0 /etc/sudoers.20221007-174424 /etc/sudoers
   --- /etc/sudoers.20221007-174424	2021-12-07 20:57:12.000000000 +0900
   +++ /etc/sudoers	2022-10-07 17:55:34.130000000 +0900
   @@ -107,0 +108 @@
   +taiyo	ALL=(ALL)	ALL
   [root@virt001 ~]# ログアウト

3. net-toolsをインストール

  • ターミナルから下記のコマンド操作を行う

   [taiyo@virt001 ~]$ sudo dnf install net-tools -y
   [sudo] taiyo のパスワード:
   サブスクリプション管理リポジトリーを更新しています。
   メタデータの期限切れの最終確認: 3:48:50 時間前の 2022年10月07日 18時00分07秒 に実施しました。
   依存関係が解決しました。
   
   [中略]
   
     準備             :                                                                                                                1/1 
     インストール中   : net-tools-2.0-0.52.20160912git.el8.aarch64                                                                     1/1 
     scriptletの実行中: net-tools-2.0-0.52.20160912git.el8.aarch64                                                                     1/1 
     検証             : net-tools-2.0-0.52.20160912git.el8.aarch64                                                                     1/1 
   インストール済みの製品が更新されています。
   
   インストール済み:
     net-tools-2.0-0.52.20160912git.el8.aarch64                                                                                            
   
   完了しました!

4. ファイアウォールを確認

  • ターミナルから下記のコマンド操作を行う

   [taiyo@virt001 ~]$ sudo firewall-cmd --state
   running
   [taiyo@virt001 ~]$ sudo firewall-cmd --list-all
   public (active)
     target: default
     icmp-block-inversion: no
     interfaces: enp0s1
     sources: 
     services: cockpit dhcpv6-client ssh
     ports: 
     protocols: 
     forward: no
     masquerade: no
     forward-ports: 
     source-ports: 
     icmp-blocks: 
     rich rules: 

5. ファイアウォールを変更

  • ターミナルから下記のコマンド操作を行う

   [taiyo@virt001 ~]$ sudo firewall-cmd --add-service=http
   success
   [taiyo@virt001 ~]$ sudo firewall-cmd --add-service=https
   success
   [taiyo@virt001 ~]$ sudo firewall-cmd --remove-service=cockpit
   success
   [taiyo@virt001 ~]$ sudo firewall-cmd --runtime-to-permanent
   success
   [taiyo@virt001 ~]$ systemctl restart firewalld
   ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ====
   'firewalld.service'を再起動するには認証が必要です。
   Authenticating as: taiyo
   Password: 
   ==== AUTHENTICATION COMPLETE ====
   [taiyo@virt001 ~]$ sudo firewall-cmd --list-services
   dhcpv6-client http https ssh

6. サーバー停止

  • ターミナルから下記のコマンド操作を行う

   [taiyo@virt001 ~]$ sudo poweroff
   [taiyo@virt001 ~]$ Connection to 192.168.20.60 closed by remote host.
   Connection to 192.168.20.60 closed.
   taiyo@Mac-mini 001 % 

7. 手動起動スクリプトを作成

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini 001 % cd ..
   taiyo@Mac-mini qemu-images % vim qemu-001-start.sh 
   taiyo@Mac-mini qemu-images % cat qemu-001-start.sh
   #!/bin/bash
   GUEST_NAME="001_rh8-web-test"
   MONITOR_PORT=46001
   SSH_PORT=46101
   HTTP_PORT=46201
   
   num=$(ps -A | grep -e ${GUEST_NAME} | grep -v "grep" | wc -c)
   
   if [ $num -gt 0 ]; then
     echo "Skip: ${GUEST_NAME} process is alrady exist."
   else
     nohup /opt/homebrew/bin/qemu-system-aarch64 -name ${GUEST_NAME} -display none \
     -machine virt,highmem=off -cpu cortex-a76 -smp 1 -m 768M -k ja \
     -device intel-hda -device hda-output \
     -device virtio-gpu-pci -device qemu-xhci \
     -device usb-tablet -device usb-kbd \
     -monitor telnet::${MONITOR_PORT},server,nowait \
     -net nic,model=virtio \
     -net user,hostfwd=tcp:192.168.20.60:${SSH_PORT}-:22,hostfwd=tcp:192.168.20.60:${HTTP_PORT}-:80,'guestfwd=tcp:10.0.2.15:80-cmd:/usr/bin/nc 192.168.20.60 '${HTTP_PORT} \
     -drive if=pflash,format=raw,file=/Users/taiyo/qemu-images/001/edk2-aarch64-code.fd,readonly=on \
     -drive if=pflash,format=raw,file=/Users/taiyo/qemu-images/001/ovmf_vars.fd \
     -drive if=virtio,format=raw,file=/Users/taiyo/qemu-images/001/qemu-001.img \
     -boot order=d > /tmp/${GUEST_NAME}.log &
   fi

8. 手動停止スクリプトを作成

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini qemu-images % vim qemu-001-stop.sh
   taiyo@Mac-mini qemu-images % cat qemu-001-stop.sh
   #!/bin/bash
   GUEST_NAME="001_rh8-web-test"
   MONITOR_PORT=46001
   
   num=$(ps -A | grep -e ${GUEST_NAME} | grep -v "grep" | wc -c)
   
   if [ $num -gt 0 ]; then
     (echo 'system_powerdown'; sleep 1) | nc localhost ${MONITOR_PORT}
   else
     echo "Skip: ${GUEST_NAME} process is none."
   fi

手順4:動作確認

1. サーバー起動の確認

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini qemu-images % cd ..
   taiyo@Mac-mini ~ % sh qemu-images/qemu-001-start.sh         
   taiyo@Mac-mini ~ % ps -A | grep 001_rh8-web-test
   23932 ttys002    0:03.38 /opt/homebrew/bin/qemu-system-aarch64 -name 001_rh8-web-test 
   -display none -machine virt,highmem=off -cpu cortex-a76 -smp 1 -m 768M -k ja -device intel-hda 
   -device hda-output -device virtio-gpu-pci -device qemu-xhci -device usb-tablet -device usb-kbd 
   -monitor telnet::46001,server,nowait -net nic,model=virtio -net user,hostfwd=tcp:192.168.20.60:46101-:22,
   hostfwd=tcp:192.168.20.60:46201-:80,guestfwd=tcp:10.0.2.15:80-cmd:/usr/bin/nc 192.168.20.60 46201 
   -drive if=pflash,format=raw,file=/Users/taiyo/qemu-images/001/edk2-aarch64-code.fd,readonly=on 
   -drive if=pflash,format=raw,file=/Users/taiyo/qemu-images/001/ovmf_vars.fd 
   -drive if=virtio,format=raw,file=/Users/taiyo/qemu-images/001/qemu-001.img -boot order=d
   23938 ttys002    0:00.00 grep 001_rh8-web-test
   taiyo@Mac-mini ~ % sh qemu-images/qemu-001-start.sh         
   Skip: 001_rh8-web-test process is alrady exist.

2. サーバー停止の確認

  • ターミナルから下記のコマンド操作を行う

   taiyo@Mac-mini ~ % sh qemu-images/qemu-001-stop.sh 
   ????????QEMU 7.1.0 monitor - type 'help' for more information
   (qemu) system_powerdown
   (qemu) %                                                                                                                                
   taiyo@Mac-mini ~ % ps -A | grep 001_rh8-web-test    
   24113 ttys002    0:00.00 grep 001_rh8-web-test
   taiyo@Mac-mini ~ % sh qemu-images/qemu-001-stop.sh          
   Skip: 001_rh8-web-test process is none.

この記事を書いた人

taiyos