KVM使い方(Ubuntu 22.04)

Ubuntu 20.04からのアップグレードでvirshが動かなくなった。修復するには次のパッケージのインストールが必要。

% sudo apt install libvirt-daemon-system

KVM使い方(Ubuntu 18.04)

Ubutnu18.04でネットワーク周りの設定に変更があった。それも含めて設定方法を書いているリンクを以下にペースト。

DHCPで固定IPが割り振られる時は、br0のmacaddressを実際のネットワークデバイス(以下の場合eno1)のものにする必要がある。

network:
  version: 2
  ethernets:
    eno1:
      dhcp4: false
      dhcp6: false
  bridges:
    br0:
      dhcp4: true
      macaddress: xx:xx:xx:xx:xx:xx
      interfaces:
      - eno1

ufwでNATの設定をしている場合は、/etc/ufw/before.rulesのデバイス名の変更(-> br0)が必要。

KVM使い方(Ubuntu 16.04)

virt-installVMの作成、virshでVMの管理ができる。

KVMのインストール

% sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils virtinst

ubuntu22.04の場合

% sudo apt install qemu-kvm libvirt-daemon-system virtinst libvirt-clients bridge-utils

ブリッジネットワークデバイスの作成と物理ネットワークデバイスとの関連付け

ホストと同じネットワークの固定IPをふるためにはブリッジを作る必要があるらしい。

auto eth0
iface eth0 inet manual

iface br0 inet static
address 192.168.1.xxx
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers xxx.xxx.xxx.xxx
bridge_ports eth0
bridge_stp off
auto br0

ネットワークの再起動後、仮想ブリッジの確認

% brctl show
bridge name bridge id       STP enabled interfaces
br0     8000.6805ca2f9aab   no      eth0
virbr0      8000.5254006ccac0   yes     virbr0-nic

/etc/sysctl.confで

net.ipv4.ip_forward=1

として、再起動(再起動したほうがいいと思う)。Ubuntu 16.04 desktopではこれを設定しないとホスト・ゲスト間でしたコミュニケーションができない。同serverではこれは設定していなくても動いている(今はこの設定が必要2019-1-10)。

VM作成

vncで接続できる状態でのインストール。Ubuntu20.04サーバのインストールCDイメージ(ubuntu-20.04.6-live-server-amd64.iso)がカレントディレクトリにあることを想定。

% sudo virt-install --name vmname --memory 4096 --disk size=50,path=/home/kvm_images/vmname.qcow2 --vcpus 1 --os-variant ubuntu20.04 --network bridge=virbr0 --noautoconsole --cdrom ubuntu-20.04.6-live-server-amd64.iso --graphics vnc,listen=0.0.0.0,password=vnc

size=50は50GB。virbr0はbr0の場合もあり。環境設定の書かれたファイルは

/etc/libvirt/qemu/vmname.xml

に作成されるが、通常はsudo virsh edit vmnameでアクセスする。

ブート時に/dev/vda1 ...だけ表示されフリーズする場合、もしかしたら以下のようにして解決できるかもしれない。 まず、vmの起動時にshiftを連打してrecoverty modeで起動できるようならば、/etc/default/grubを以下のようにしてupdate-grubする。

GRUB_CMDLINE_LINUX_DEFAULT=""

これでrebootして普通にブートすればよし。"quiet splash"のどちらか、もしくは両方が悪さをしているということになる。

Ubuntuのインストール

UbuntuのインストールはVNCを設定したならVNC経由で行える。KVMVNCの使い方は下の通り。

KVMVNC

一つ目のVMvncのデフォルトポートは5900。パスワードは次のコマンドで確認できる。

% virsh edit vmname

以下のような感じでpasswdが設定されている。

<graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0' passwd='hoge'/>

二つ目以降順次5901, 5902となっていく模様。5900 + xのxは次のコマンドで確認できる。

% virsh vncdisplay vmname
:1

の場合、5901がポート番号。MacOSの場合、Finder->Go->Connect to serverから

vnc://hostmachine:5901

のようにして接続できる。アクセスを拒否される場合はsshkvmサーバにX forwardして、kvmサーバ内でvinagreなんかを起動する方が良いかも。

コンソールアクセスによるVMのインストール

% virt-install --name vmname --memory 4096 \
--disk path=/vmdir/vmname.qcow2,size=50,format=qcow2 \
--vcpus 1 --os-type linux --os-variant ubuntu16.04 \
--network bridge=br0 --location ubuntu-16.04.5-server-amd64.iso \
--extra-args 'console=ttyS0,115200n8 serial' \
--console pty,target_type=serial \
--graphics vnc,listen=0.0.0.0,password=vnc
% virsh console vmname

KVMメモリサイズ変更

% virsh edit mydata

KVM仮想マシンをプライベートネットワークに入れる(NAT)

virsh editbridgevirbr0にする。普通はIPアドレスDHCPで配られるようになっている。KVM仮想マシンのプライベートネットワーク内でのIPアドレスDHCPで固定で振るにはvirsh net-edit defaultmacアドレスipアドレスを設定すれば良い。以下のように書き換える。

    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
      <host mac='xx:xx:xx:xx:xx:xx' name='hostname' ip='192.168.122.2'/>
    </dhcp>

設定後は次のようにしてKVMネットワークの再起動が必要。

virsh net-destroy default
virsh net-start default

この場合、全てのVMを止める必要がある。追加するだけなら次のようにしてシステムを止めずに設定することができる。

sudo virsh net-update default add-last ip-dhcp-host '<host mac="xx:xx:xx:xx:xx:xx" name="addedhostname" ip="192.168.122.8"/>' --live --config --parent-index 0

KVM仮想マシンの削除

Undefineしたのちimageファイルをrmで削除。

sudo virsh undefine <vm-name>

仮想マシンがものすごく遅い。

virsh edit vmnameで一番最初の行がこうなっているか確認。

<domain type='kvm'>

ものすごく遅い仮想マシンではこうなっていた。

<domain type='qemu'>

kvmに書き換えて仮想マシンを起動しようとすると、Check that virtualization is enabled in the host BIOS、というメッセージがでた。そういうことなのか。

rootでvirshが使えない

/etc/libvirt/qemu.confをいじる。

user = "root"
group = "root"

既存KVMイメージのインポート

sudo virt-install --name mynewvm --ram 8192 --disk path=/home/kvm_images/myimage.qcow2 --vcpus 2 --os-type linux --os-variant ubuntu18.04 --network bridge=virbr0 --noautoconsole --graphics vnc,listen=0.0.0.0,password=vnc --import

VirtualboxからKVMへの移行

参考

qemu-img convert -O raw vmname.vdi vmname.img
% sudo virt-install \
--name vmname \ 
--ram 1024 \
--disk path=/var/lib/libvirt/images/vmname.img,bus=sata,format=raw \
--vcpus 1 \
--os-type linux \
--os-variant ubuntutrusty \
--network bridge=br0 \
--noautoconsole \
--graphics vnc,listen=0.0.0.0,password=vnc \
--import

VMをコピーして別のVMとして使う

  • virt-cloneする(次のセクション参照)
  • DHCPサーバから見えてるならDHCPにする。virt-edit -d vmname /etc/netplan/01-netcfg.yamlVMイメージ内のファイルを編集する。virt-editに関しては↓。
  • virsh net-dhcp-leases defaultIPアドレスをチェックしてsshログインする。sshが通らない場合はvncで様子をチェックする。
  • IPアドレスが振られていない場合はデバイス名が変わっている可能性あり。virt-edit -d vmname /etc/netplan/01-netcfg.yamlVMイメージ内のファイルを編集する。virt-editに関しては↓。
  • DHCP固定でIPアドレスを振りたい場合は、virsh net-edit defaultする。この場合は全VMを止める必要がある。virsh net-update default add ip-dhcp-hostというのもある。これなら他のVMを止める必要がない。virsh edit vmnamemac addressを変えるのもあり。

VMファイルシステムをホストから編集する(virt-edit)

virt-editという便利コマンドがあるらしい。libguestfs-toolsをインストールすれば良い。

IP addressは以下のような方法で探すことができる。

VMの移行 or コピー

virt-cloneというコマンドがあるらしい。

% sudo virt-clone --original myoldvm --name mynewvm --file /kvm_images/mynewvm.qcow2

他の方法。

数値のrounding

floatをintにするには、numpy.rint(x).astype(int)でOKらしい。
numpy.rintのほうがnumpy.roundより速いらしい。
http://stackoverflow.com/questions/10873824/how-to-convert-2d-float-numpy-array-to-2d-int-numpy-array

追記(2020/06/29)

以下のリンクの情報によると、exact representationだからオッケーということらしい。

math - Safest way to convert float to integer in python? - Stack Overflow

Cluster report of ganglia on ubuntu 18.04

Ubuntu 18.04にgangliaをインストールした。gmond, gmetadは正しく動いているようだが、host reportは出るが、cluster reportで各hostの情報がが表示されない。host達が羅列されるのがとても便利なので、これは痛い。そういう仕様変更かと思ってganglia-webのバージョンを変えてみたりあれこれ頑張ったが修正されず。Ubuntu 16.04の時は動いていたので、なんでかなーっと思ってたら、Ubuntu 18.04のバグのようだ。かなり時間を使ってしまった。

Bug #1822048 “cluster report not displayed” : Bugs : ganglia-web package : Ubuntu

ついでにインストールメモを残す。といっても、以下のサイトに従えばすんなり入る。

How to Install Ganglia Monitoring Server on Ubuntu Linux - Geek Rewind

上記サイトはリンク切れ。以下のPR通りに直せば良い。

cluster_view.php :: context_metrics is array by adriansev · Pull Request #309 · ganglia/ganglia-web · GitHub

Ganglia webに関する情報は以下に少し書いてある。

ganglia-webの設定はUbuntu 18.04の場合、/etc/ganglia-webfrontend/conf.phpに書くと良い。load_oneよりcpu_reportの方が好きなので、こんなのを書いている。

<?php
$conf['default_metric'] = "cpu_report";
?>

Ubuntu 18.04へのインテル関連ソフトウエアインストール

Intel® Software License Managerインストール

Intel® Software License Manager Getting Started

インストール前に。

apt install lsb lib32stdc++6 libc6-dev-i386

Intel® Parallel Studio XE Cluster Edition for Linuxインストール

グラフィカルインターフェースがあるものを外すと、GTKなど不要なライブラリのインストールが不要になる。

Ubuntu18.04のLDAP認証libnss-ldapd

設定ずみのnslcd.confを先に/etc/にコピーしておいて、

apt install -y libnss-ldapd ldap-auth-config auth-client-config
auth-client-config -t nss -p lac_ldap
pam-auth-update

設定やり直し。

dpkg-reconfigure ldap-auth-config

Ubuntu18.04のgridengineのインストール

Debianのパッケージをインストールする。 Installing (Son of) Grid Engine on Ubuntu 18.04 LTS – Muddy Boots

参照先が消えると困るのでコピーを残す。

sudo cat > /etc/apt/sources.list.d/debian.list <<EOL
deb http://ftp.debian.org/debian/ stretch main contrib non-free
deb http://security.debian.org/debian-security/ stretch/updates main contrib non-free
EOL
sudo cat > /etc/apt/preferences.d/debian <<EOL
Package: gridengine-*
Pin: release o=Debian
Pin-Priority: 1000

Package: *
Pin: release o=Debian
Pin-Priority: 10
EOL
sudo apt-get update
sudo apt-get install debian-archive-keyring
apt-key add /usr/share/keyrings/debian-archive-keyring.gpg
apt install gridengine-client gridengine-common gridengine-master gridengine-qmon

Ubuntuが異常終了した時、次のブート時にタイムアウトするようにする(古い記事を転記)

Ubuntu 18.04でも必要か? たぶん不要)

以前は00_headerを直接書き換えていたが、いまはもう少し賢く設定可能。

/etc/default/grubに次の行を追加

GRUB_RECORDFAIL_TIMEOUT=${GRUB_TIMEOUT}

そして

% sudo update-grub

http://bneijt.nl/blog/post/ubuntu-server-continue-boot-after-failure/

USBメモリを使ったインストールをpreseedingである程度自動化(昔書いたやつをまとめて転記)

この記事はUbuntu 18.04では使えない。preseed.txtをUSBメモリに書き込めないから。回避策を探している。

インストール作業環境・モチベーション

  • コンピュータの素人がコンピュータクラスタを管理している。Ubuntuのインストールが仕事の中心ではない。
  • たくさんのインストール作業を行うわけではない。数ヶ月に一度、例えば10台程度。
  • コンピュータの横にいて様子を見ながら作業する。
  • インストール中に本を読んだりしたいので、Ubuntuのインストールで、途中での入力作業を減らしたい。

参考にしたサイト

一般

preseedingの設定ファイル

preseedingのブートオプション

ssh-serverのrootログインの設定

d-i preseed/late_command string \
    in-target sed -i 's/PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config

手順

Ubuntu 14.04.1 server 64bit環境でテストしました。手持ちのコンピュータではUbuntu (14.04.2|14.04.3) server 64bitではそもそもUSBメモリをブート可能なデバイスとして認識しなかった。

  1. preseedingの設定をpreseed.cfgなどに保存し、ubuntuのUSBインストーラのトップディレクトリに置く。
  2. USBメモリを挿して、電源ON
  3. ブートの選択画面で、ESC,F6,ESCとキーボードを叩き、ブートオプションを書き込める状態にする。
  4. 次の文字列を加え、リターンキーを叩いて起動する。
 priority=critical locale=en_US file=/cdrom/preseed.cfg

preseedingの設定ファイルに間違いがあり途中でインストールがこけた場合は、手動でのインストールに切りかわる。

HDD(/dev/sdaなど)にすでにパーティションがあってマウントされてしまう場合

テストしてないけどメモを残す。

d-i preseed/early_command string umount /media || true

preseed.cfg

Ubuntu 14.04の時のもの。

boot option

ESC --> F6 --> ESC のあと、次の文字列を追加する。

priority=critical locale=en_US file=/cdrom/preseed.cfg

preseed.cfg (https://help.ubuntu.com/lts/installation-guide/example-preseed.txt)

# https://help.ubuntu.com/lts/installation-guide/example-preseed.txt
d-i debian-installer/locale string en_US.UTF-8
d-i debian-installer/splash boolean false
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/xkb-keymap select us
#d-i    preseed/early_command string killall.sh dhclient ; netcfg
d-i netcfg/choose_interface select auto
#d-i netcfg/dhcp_timeout string 30
# If you prefer to configure the network manually, uncomment this line and
# the static network configuration below.
#d-i netcfg/use_autoconfig boolean false 
#d-i netcfg/disable_autoconfig boolean true 
#d-i netcfg/disable_dhcp boolean true
#d-i netcfg/choose_interface select eth0
#d-i netcfg/disable_autoconfig boolean true
#d-i netcfg/dhcp_failed note
#d-i netcfg/dhcp_options select Configure network manually
#d-i netcfg/get_nameservers string 192.168.1.254
#d-i netcfg/get_ipaddress string 192.168.1.33
#d-i netcfg/get_netmask string 255.255.255.0
#d-i netcfg/get_gateway string 192.168.1.1
#d-i netcfg/confirm_static boolean true
d-i netcfg/get_hostname string hoge33
d-i netcfg/get_domain string localdomain

d-i mirror/http/mirror select jp.archive.ubuntu.com
d-i mirror/country string manual
d-i mirror/http/hostname string jp.archive.ubuntu.com
d-i mirror/http/directory string /ubuntu
d-i mirror/http/proxy string
d-i partman-auto/method string regular
#d-i partman-lvm/device_remove_lvm boolean true
#d-i partman-lvm/confirm boolean true
d-i partman/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/default_filesystem string ext4
d-i clock-setup/utc boolean false
d-i time/zone string Japan
d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server string ntp.ubuntu.com
d-i base-installer/kernel/image string linux-generic
d-i passwd/root-login boolean true
d-i passwd/root-password-crypted password {MD5 hash}
d-i passwd/make-user boolean false
d-i user-setup/encrypt-home boolean false
d-i apt-setup/services-select multiselect security
d-i apt-setup/security_host string security.ubuntu.com
d-i apt-setup/security_path string /ubuntu
d-i debian-installer/allow_unauthenticated string false
d-i pkgsel/upgrade  select safe-upgrade
d-i pkgsel/language-packs multiselect 
d-i pkgsel/update-policy select none
d-i pkgsel/updatedb boolean true
d-i grub-installer/skip boolean false
d-i lilo-installer/skip boolean false
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true
d-i finish-install/keep-consoles boolean false
d-i finish-install/reboot_in_progress note 
d-i cdrom-detect/eject boolean true
d-i debian-installer/exit/halt boolean false
d-i debian-installer/exit/poweroff boolean false
d-i pkgsel/include string openssh-server
d-i preseed/late_command string \
    in-target sed -i 's/PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config