0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

kvmにおいて、vm に spice tls接続するまで

Posted at

kvmにおいて、vm に spice tls接続するまで

なにをするか

自己署名証明書(いわゆるオレオレ証明書)を作って、kvmのvmにtlsにより暗号化された状態でspiceプロトコルを使い画面に接続したい.

おおまかな流れ

  1. vmの設定を変更
  2. qemu.conf を変える
  3. 認証局、証明書などを準備
  4. 再起動
  5. remote-viewerで接続

環境

  • Ubuntu 24.04.1 LTS
  • libvirtを使用
  • vmの名前はtestSpice

vmの設定ファイルを編集

libvirtでは設定ファイルはxml形式.
libvirt domain xml settingsを読んでください.

testSpice.xml
<graphics type='spice' autoport='yes' listen='your ip address'>
      <listen type='address' address='your ip address'/>
</graphics>

参考:設定例

qemu.confを編集

/etc/libvirt/qemu.confの#spice_tls = 1の部分のコメントを外す.

qemu.conf
# Enable use of TLS encryption on the SPICE server.
#
# It is necessary to setup CA and issue a server certificate
# before enabling this.
#
spice_tls = 1

証明書などを置くディレクトリをspice_tls_x509_cert_dirに代入する. 設定しなかったらデフォルトで決まる.

qemu.conf
# In order to override the default TLS certificate location for
# spice certificates, supply a valid path to the certificate directory.
# If the provided path does not exist, libvirtd will fail to start.
# If the path is not provided, but spice_tls = 1, then the
# default_tls_x509_cert_dir path will be used.
spice_tls_x509_cert_dir = "/etc/pki/libvirt-spice"

参考:qemu.confの設定

証明書など

スクリプトを参考にした. 証明書もろもろを生成するスクリプト. このままだと、動かなかったので少し変更した.

変更点

  • keyを生成するときのbitサイズを1024から2048に変更(creating a key for our ca と create server key)
  • csrを作成するときにホスト名を変更(create a certificate signing request (csr))
#!/bin/bash

SERVER_KEY=server-key.pem

# creating a key for our ca
if [ ! -e ca-key.pem ]; then
    openssl genrsa -des3 -out ca-key.pem 2048
fi
# creating a ca
if [ ! -e ca-cert.pem ]; then
    openssl req -new -x509 -days 1095 -key ca-key.pem -out ca-cert.pem -utf8 -subj "/C=IL/L=Raanana/O=Red Hat/CN=my CA"
fi
# create server key
if [ ! -e $SERVER_KEY ]; then
    openssl genrsa -out $SERVER_KEY 2048
fi
# create a certificate signing request (csr)
if [ ! -e server-key.csr ]; then
    openssl req -new -key $SERVER_KEY -out server-key.csr -utf8 -subj "/C=IL/L=Raanana/O=Red Hat/CN=yourhostname"
fi
# signing our server certificate with this ca
if [ ! -e server-cert.pem ]; then
    openssl x509 -req -days 1095 -in server-key.csr -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
fi

# now create a key that doesn't require a passphrase
openssl rsa -in $SERVER_KEY -out $SERVER_KEY.insecure
mv $SERVER_KEY $SERVER_KEY.secure
mv $SERVER_KEY.insecure $SERVER_KEY

# show the results (no other effect)
openssl rsa -noout -text -in $SERVER_KEY
openssl rsa -noout -text -in ca-key.pem
openssl req -noout -text -in server-key.csr
openssl x509 -noout -text -in server-cert.pem
openssl x509 -noout -text -in ca-cert.pem

これをqemu.confのspice_tls_x509_cert_dirで設定されたディレクトリの下で実行する.
参考:証明書生成スクリプト

再起動

libvirtとvmを再起動.

$ virsh shutdown spiceTest
$ systemctl restart spiceTest
$ virsh start spiceTest

virsh restart spiceTestではlibvirtの設定が読み込まれない.

remote-viewer

スクリプトで作ったca-cert.pemをclient側にコピーする.

remote-viewer spice://yourhostname?tls-port=5901  --spice-ca-file=/path/to/ca-cert.pem

参考になったサイト

tls

spice

最後に

間違いなどがあったら教えてほしいです.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?