【参考】Virtual Desktop on Cloud9

本項目は【オプション】です。この項目の実施目安は約30分です。

Cloud9環境にLinux DesktopとNICE DCVをインストールし、VDIとして利用してみます。

Virtual Desktopの利用

Cloud9の動いている環境に、Linux Desktop環境をインストールし、NICE DCVを使ってWebブラウザからVDI接続します。

この手順は大きく、4つに分かれております。

  1. ディスクサイズの拡張
  2. Linux Desktop環境のインストール
  3. Linux Desktop環境の日本語化
  4. NICE DCVのインストールと設定

1.ディスクサイズの拡張

(1) ターミナルを新しく開きます (通常の操作を参考)。

(2) 一つ前の「【オプション】 パッケージのアップデート」を行っていいない場合には事前に行っておきます。

(3) 下記のコマンドを実行し、ディスクをリサイズするスクリプトを作成します。

cat << 'EOF' >resize.sh
#!/bin/bash

# Specify the desired volume size in GiB as a command line argument. If not specified, default to 20 GiB.
SIZE=${1:-20}

# Get the ID of the environment host Amazon EC2 instance.
INSTANCEID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/')

# Get the ID of the Amazon EBS volume associated with the instance.
VOLUMEID=$(aws ec2 describe-instances \
  --instance-id $INSTANCEID \
  --query "Reservations[0].Instances[0].BlockDeviceMappings[0].Ebs.VolumeId" \
  --output text \
  --region $REGION)

# Resize the EBS volume.
aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE

# Wait for the resize to finish.
while [ \
  "$(aws ec2 describe-volumes-modifications \
    --volume-id $VOLUMEID \
    --filters Name=modification-state,Values="optimizing","completed" \
    --query "length(VolumesModifications)"\
    --output text)" != "1" ]; do
sleep 1
done

#Check if we're on an NVMe filesystem
if [[ -e "/dev/xvda" && $(readlink -f /dev/xvda) = "/dev/xvda" ]]
then
  # Rewrite the partition table so that the partition takes up all the space that it can.
  sudo growpart /dev/xvda 1

  # Expand the size of the file system.
  # Check if we're on AL2
  STR=$(cat /etc/os-release)
  SUB="VERSION_ID=\"2\""
  if [[ "$STR" == *"$SUB"* ]]
  then
    sudo xfs_growfs -d /
  else
    sudo resize2fs /dev/xvda1
  fi

else
  # Rewrite the partition table so that the partition takes up all the space that it can.
  sudo growpart /dev/nvme0n1 1

  # Expand the size of the file system.
  # Check if we're on AL2
  STR=$(cat /etc/os-release)
  SUB="VERSION_ID=\"2\""
  if [[ "$STR" == *"$SUB"* ]]
  then
    sudo xfs_growfs -d /
  else
    sudo resize2fs /dev/nvme0n1p1
  fi
fi

EOF

(4) ディスクサイズの変更をします。下記コマンドを実行します。ここではディスクサイズを20GBとしています。

bash resize.sh 20
sync

2.Linux Desktop環境のインストール

(5) 下記コマンドを実行し、LinuxのDesktop環境をインストールします。選択項目が表示された場合には、lighttdmを選択します。

sudo apt install -y --no-install-recommends ubuntu-desktop lightdm

3 . Linux Desktop環境の日本語化

(6) Cloud9 上のシェルで下記コマンドを順番に実行します。

下記は参考のため、簡易な手順となっており、不要なパッケージが含まれる場合があります。

sudo apt -y install language-pack-ja-base language-pack-ja ibus-anthy
sudo apt -y install language-pack-gnome-ja firefox-locale-ja fonts-noto-cjk-extra fonts-noto-cjk ibus-mozc
sudo apt -y install kasumi
sudo apt -y install fonts-migmix
sudo apt -y autoremove 
sudo update-locale LANG=ja_JP.UTF-8
echo 'export LANG=ja_JP.UTF-8' >> ~/.bashrc
echo 'export LANGUAGE="ja_JP:ja"' >> ~/.bashrc

4.NICE DCVのインストールと設定

(7) 下記コマンドを順番に実行し、NICE DCVをインストールします。ここではUbuntu 18.04の例を示します。途中[y|n]の選択が出た場合には、y を入力します。

wget https://d1uj6qtbmh3dt5.cloudfront.net/NICE-GPG-KEY
gpg --import NICE-GPG-KEY

wget https://d1uj6qtbmh3dt5.cloudfront.net/2022.1/Servers/nice-dcv-2022.1-13300-ubuntu1804-x86_64.tgz
#wget https://d1uj6qtbmh3dt5.cloudfront.net/nice-dcv-ubuntu1804-x86_64.tgz
tar -xvzf nice-dcv-2022.1-13300-ubuntu1804-x86_64.tgz && cd nice-dcv-2022.1-13300-ubuntu1804-x86_64


sudo apt install -y ./nice-dcv-server_2022.1.13300-1_amd64.ubuntu1804.deb
sudo apt install -y ./nice-dcv-web-viewer_2022.1.13300-1_amd64.ubuntu1804.deb
sudo usermod -aG video dcv
sudo apt install -y ./nice-xdcv_2022.1.433-1_amd64.ubuntu1804.deb
sudo apt install -y dkms
sudo dcvusbdriverinstaller

# sudo apt install -y ./nice-dcv-gl_2022.1.973-1_amd64.ubuntu1804.deb
# sudo apt install -y ./nice-dcv-simple-external-authenticator_2022.1.193-1_amd64.ubuntu1804.deb
# sudo apt install -y pulseaudio-utils

(8) 設定ファイルを作成します。下記コマンドを実行します。

cat << 'EOF' > ./dcv.conf
[license]
[log]
[display]
[connectivity]
web-port=8080
web-use-https=false
[security]
authentication="none"
EOF

(9) 設定ファイルを所定の位置に移動します。下記コマンドを実行します。

sudo mv ./dcv.conf /etc/dcv/dcv.conf

(10) 下記コマンドで一度元のディレクトリ ~/environment/ へ戻ります。

cd ~/environment/

(11) 下記コマンドを実行し、スクリプトを作成します。

cat << 'EOF' >> dcvsession
#!/bin/bash
dbus-launch gsettings set org.gnome.desktop.lockdown disable-lock-screen true
dbus-launch gsettings set com.ubuntu.update-notifier no-show-notifications true
dbus-launch gsettings set org.gnome.shell enabled-extensions "['ubuntu-dock@ubuntu.com']"
sudo  /sbin/iptables -A INPUT -p tcp ! -s localhost --dport 8080 -j DROP
sudo systemctl start dcvserver
sudo systemctl enable dcvserver
dcv create-session my-session --owner ubuntu &> /dev/null
sudo apt-get install -y jq &> /dev/null
echo DCV preview URL : https://$C9_PID.vfs.cloud9.$(curl --silent http://169.254.169.254/latest/dynamic/instance-identity/document | jq -r .region).amazonaws.com
EOF

(12) 下記コマンドで、スクリプトに実行属性をつけます。

chmod 755 ./dcvsession

(13) ここで一度 reboot します。

sudo reboot

(14) 再度Cloud9の画面を出し、Cloud9上のコンソールを開きます。

(15) 下記コマンドを実行し、NICE DCVの開始と接続先URLを取得します。

./dcvsession

(16) 上記コマンドで DCV preview URL となっているURLに、Cloud9で接続中のブラウザと同じブラウザから接続します(別タブや別ウィンドウ等)。

ブラウザでクロスサイトスクリプトエラーが出る場合には許可をしてください。また、クリップボードやマイクの許可を求められた場合にも、必要に応じて許可をしてください。

ここで説明している手順では、NICE DCV上で動いているユーザにはパスワードを設定していません。GUI上で管理者パスワードを求められるものに関しては実行できません。

(17) Linux Dekstop がブラウザ内に表示されます。

(18) InputMethodを使う場合、設定->地域と言語->入力ソースから、日本語->日本語(Mozc)を選択します。日本語化をVirtual Desktopの初回起動前に行っていた場合には、既に設定されています。

Cloud9とNICE DCVを利用した、VDIの手順は以上です。