この記事では、以下の3つの方法を紹介します。
vitt-install で debian on kvm+qemu on uefi をやりたい。
- virt-install で仮想マシインストールをターミナルから起動
- preseed で すべて自動インストール
- ueif でインストール
virt-install でシリアルコンソールを使って、SSH経由でターミナルからインストールできる方法を確認する。
2. preseed で すべて自動インストール
上記の手法に加えて、PreSeedで更に自動化をする。インストールはSSH経由で行えるので、細かい設定を自動化する。
3. ueif でインストール
上記の手法に加えて、UEFIを使ってブートを変えてインストールする方法を確認する。
準備
kvm / qemu / libvirt の準備
sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virtinst libvirt-daemon dnsmasq-base ovmf
UEFIのファームウェアの準備
sudo apt install ovmf
virt-install で仮想マシンをインストールする。
ただし、コンソールから行う。
コンソール利用条件
コンソールインストールは、インストラーをttyS0起動する必要がある。
→ そのためには、カーネルにオプションを引き渡す。
→ そのために、カーネル直接起動が必要である。
→ そのために、HTTP経由でインストラーを直接取得する。
→ そのために、--location
を利用する。
カーネル起動/ location の例
virt-install \
--location=http://exapple.com/debian/dists/buster/main/installer-amd64/
installer はdebian ミラーをみてURLを参照する。
今回は、preseedで完全に自動化したいので、locationを利用した。手作業でカーネルオプションを書き換えるのであればISOから起動しても構わない。
location が決まったら、インストールを開始します。
ディスクを作成
qcow2 で作成しSparseファイルにする。
sparse とは「固定量を確保しない、使ったぶんだけ実容量を消費する」スタイル。
sudo qemu-img create -f qcow2 /var/lib/libvirt/images/d02.qcow2 8G
ディスクのチェック
仮想マシン用のディスクが作成できたのでチェックする。
sudo qemu-img info /var/lib/libvirt/images/d02.qcow2
apt インストールチェック
BISO(MBR)で仮想マシンを作成し。apt で一式が揃えられたことを確認する
インストールが成功し、コマンドが間違ってないことを確認。EFIはBIOSインストールを編集して行うので、まずはMBRでインストールする。
virt-install \
--connect=qemu:///system \
--graphics none \
--location http://ftp.kddilabs.jp/pub/Linux/distributions/Debian/debian/dists/buster/main/installer-amd64/ \
--extra-args="console=ttyS0"
--ram 4096 \
--vcpus 8 \
--virt-type kvm \
--os-type linux \
--os-variant debian10 \
--name d01-manual \
--disk path=/var/lib/libvirt/images/d01.qcow2 \
ttyS0 / シリアルコンソールのインストール
シリアルコンソールのインストールは、次の3つをつければオッケみたいです。
--graphics none \
--location http://ftp.kddilabs.jp/pub/Linux/distributions/Debian/debian/dists/buster/main/installer-amd64/ \
--extra-args="console=ttyS0"
インストのやり直する方法
インストをやり直すとき、仮想マシンと、仮想ディスクを削除する。
コマンドから操作する。仮想マシンを削除・ディスク削除・ディスク作成
### 削除して
sudo virsh destroy d01
sudo virsh undefine d02 --nvram
sudo rm /var/lib/libvirt/images/d02.qcow2
## 作り直す
sudo qemu-img create -f qcow2 /var/lib/libvirt/images/d02.qcow2 8G
virt-install --connect ...
失敗したら削除して、即時に再インスト。
ueif でインストールする
UEFIでインストールするには、ovmf を使う。
UEFIのファームウェアの準備
sudo apt install ovmf
--boot uefi
これで、UEFIのインストールができる。
preseed の自動インストール
preseed.cfg を使って自動インストールする。
preseed を使って設定を済ませてしまう。
これで無人インストールになる。
preseed の 注意
ファイル名 preseed.cfg
が固定なので、ファイル名は変えられない。
preseed.cfg は インストールファイルに書かれているので
preseed を使う(MBRでチェック)
preseed ファイルのパスを起動オプションに入れるとできる。
--initrd-inject=/path/to/preseed.cfg \
Preseedのチェックを行う。ここもまだMBRインストールでチェックしておく。
virt-install \
--connect=qemu:///system \
--initrd-inject=/home/takuya/preseed.cfg \
--name d02 \
--ram 4096 \
--disk path=/var/lib/libvirt/images/d02.qcow2 \
--vcpus 8 \
--virt-type kvm \
--os-type linux \
--os-variant debian10 \
--graphics none \
--location http://ftp.kddilabs.jp/pub/Linux/distributions/Debian/debian/dists/buster/main/installer-amd64/ \
--extra-args="console=ttyS0"
すべての設定が終わって正しくインストールできる状態にしておいて、UEFIに関するエラーとBIOS/Pressedなど初期設定に関するエラーを切り分けておいた。
preseedは、なぜ使うのかというとEFIパーティションをきっちり作成したいからである。起動して手動インストールだけなら不要かもしれない。
peseed の設定
deiban の公式サイトに、preseedのサンプルが設置されている。
これを改変して、利用することにする。
設定の確認
cat preseed.cfg | \grep -vE '^#.*$|^$'
私の設定
d-i debian-installer/locale string en
d-i debian-installer/language string en
d-i debian-installer/country string JP
d-i debian-installer/locale string en_US.UTF-8
d-i keyboard-configuration/xkb-keymap select us
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string d01
d-i netcfg/get_domain string .lan
d-i mirror/country string jp
d-i mirror/http/hostname string debian-mirror.sakura.ne.jp
d-i mirror/http/directory string /debian/
d-i mirror/http/proxy string
d-i passwd/root-login boolean false
d-i passwd/make-user boolean true
d-i passwd/user-fullname string takuya
d-i passwd/username string takuya
d-i passwd/user-password-crypted password $5$J2NR$xxxxxxxxxxxxxxx
d-i clock-setup/utc boolean true
d-i time/zone string Asia/Tokyo
d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server string ntp.nict.jp
d-i partman-auto/method string lvm
d-i partman-auto-lvm/guided_size string max
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/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-md/confirm boolean true
d-i partman-partitioning/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 base-installer/install-recommends boolean false
d-i apt-setup/cdrom/set-first boolean true
d-i apt-setup/non-free boolean true
d-i apt-setup/contrib boolean true
tasksel tasksel/first multiselect ssh-server
popularity-contest popularity-contest/participate boolean false
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean false
d-i grub-installer/bootdev string default
d-i finish-install/reboot_in_progress note
preseedでユーザを作成しておく
preseedでユーザとパスワードを初期設定ができる。
パスワードの作り方
次のようなパスワード $1$nByvFzd5$MXmZlDPk8xchbyQfW9qfm1
を生成する。
いくつか方法がある。
mkpasswd -s -m md5
openssl passwd -1 -salt $( openssl rand -base64 3 | tr '+/' 'Ab')
openssl passwd -5 -salt $( openssl rand -base64 3 | tr '+/' 'Ab')
uefi と EFI を使う。セキュアブート問題
UEFIで起動するのには、MBRのインストールのコマンドに、次を追加する。
--boot uefi
理想論であれば上記だけで済むはず。
しかし、boot uefi だけのすると、セキュアブートが有効になってしまい、いい感じに起動しないことがある。(後述)
そのため、次にのようにOVMFを指定する。
--boot uefi,loader=/usr/share/OVMF/OVMF_CODE_4M.fd,loader_ro=yes,loader_type=pflash,nvram_template=/usr/share/OVMF/OVMF_VARS_4M.fd,loader_secure=no --connect=qemu:///system --initrd-inject=/home/takuya/preseed.cfg --name d02 --ram 4096 --disk path=/var/lib/libvirt/images/d02.qcow2 --vcpus 8 --virt-type kvm --os-type linux --os-variant debian10 --graphics none --location http://ftp.kddilabs.jp/pub/Linux/distributions/Debian/debian/dists/buster/main/installer-amd64/ --extra-args="console=ttyS0"
これを考慮すると、次のようになった。
preseed+uefi+netinstall+console の完成ワンライな~
virt-install \
--boot uefi,loader=/usr/share/OVMF/OVMF_CODE_4M.fd,loader_ro=yes,loader_type=pflash,nvram_template=/usr/share/OVMF/OVMF_VARS_4M.fd,loader_secure=no \
--connect=qemu:///system \
--initrd-inject=/home/takuya/preseed.cfg \
--name d02 \
--ram 4096 \
--disk path=/var/lib/libvirt/images/d02.qcow2 \
--vcpus 8 \
--virt-type kvm \
--os-type linux \
--os-variant debian10 \
--graphics none \
--location http://ftp.kddilabs.jp/pub/Linux/distributions/Debian/debian/dists/buster/main/installer-amd64/ \
--extra-args="console=ttyS0"
--boot uefi
のオプションをつければいい。
virt-install --boot uefi \
uefi をつけた場合 undefine (削除)のコマンドが変わる。
virsh --connect=qemu:///system undefine d02 --nvram
起動エラー対応 uefi セキュアブート問題
先日にこれらを試したとき、つぎのように、OVMFをnosecure にして、code ms を使わない指定に変えた。virt-install でpreseedを組み合わせた時、ovmf 4mを使わないと動かなかった。
preseedでディスクレイアウト詳細変更、インストール後に uefi のセレクトをコールすればいいんだろうけど、今回は見送った
--boot uefi,loader=/usr/share/OVMF/OVMF_CODE_4M.fd,loader_ro=yes,loader_type=pflash,nvram_template=/usr/share/OVMF/OVMF_VARS_4M.fd,loader_secure=no
https://sven.stormbind.net/blog/posts/deb_uefi_pxe_install_hpe_dl120/
preseed インストールEFI対応
EFI起動して、画面を見ながら自動自動と選んでいけばEFIになるのだが、preseedで同じように、自動を選択したはずだが、GPT/EFIにならなかった。
GPTにはなるが、/boot と /boot/efi が作られなかった。
そこで、preseedを変えて、明示的にEFIパーティションを作るようにした。
d-i partman/mount_style select uuid
d-i partman/choose_label string gpt
d-i partman/default_label string gpt
d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select gpt-boot-root-swap
d-i partman-auto/expert_recipe string \
gpt-boot-root-swap :: \
1 1 1 free \
$bios_boot{ } \
method{ biosgrub } . \
200 200 200 fat32 \
$primary{ } \
method{ efi } format{ } . \
512 512 512 ext2 \
$primary{ } $bootable{ } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext2 } \
mountpoint{ /boot } . \
1000 20000 -1 ext4 \
$primary{ } \
method{ format } format{ } \
use_filesystem{ } filesystem{ ext4 } \
mountpoint{ / } .
d-i partman-partitioning/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-basicfilesystems/no_swap boolean false
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/device_remove_lvm boolean true
インストールにかかった時間
preseed / ueif / virt-install 自動化した結果、カップ麺時間で仮想マシンを準備することができた。
real 3m41.697s
user 0m2.782s
sys 0m1.35s
2022-04-23 追記
preseed のディスクレイアウトを、自動設定にしてLVMで設定をやめたら難なく起動した
virt-install \
--boot uefi \
--connect=qemu:///system \
--initrd-inject=/home/takuya/preseed.cfg \
--name d03 \
--ram 8912 \
--disk path=/var/lib/libvirt/images/d03.qcow2 \
--vcpus 8 \
--virt-type kvm \
--os-type linux \
--os-variant debian10 \
--graphics none \
--location http://ftp.kddilabs.jp/pub/Linux/distributions/Debian/debian/dists/buster/main/installer-amd64/ \
--extra-args="console=ttyS0"
preseed.cfg
## locales
d-i debian-installer/locale string en
d-i debian-installer/language string en
d-i debian-installer/country string JP
d-i debian-installer/locale string en_US.UTF-8
d-i keyboard-configuration/xkb-keymap select us
## network
d-i netcfg/dhcpv6_timeout string 1
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string d01
d-i netcfg/get_domain string .lan
## apt mirror
d-i mirror/country string jp
d-i mirror/http/hostname string debian-mirror.sakura.ne.jp
d-i mirror/http/directory string /debian/
d-i mirror/http/proxy string
### user
d-i passwd/root-login boolean false
d-i passwd/make-user boolean true
d-i passwd/user-fullname string takuya
d-i passwd/username string takuya
d-i passwd/user-password-crypted password $5$RFWZ$aaaaaaaaaa
## timezone
d-i clock-setup/utc boolean true
d-i time/zone string Asia/Tokyo
d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server string ntp.nict.jp
## partition
d-i partman-auto/method string regular
d-i partman-efi/non_efi_system boolean false
d-i partman-auto/choose_recipe select atomic
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/choose_partition select Finish partitioning and write changes to disk
### apt / package
tasksel tasksel/first multiselect
popularity-contest popularity-contest/participate boolean false
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean false
d-i grub-installer/bootdev string default
d-i finish-install/reboot_in_progress note
-boot uefi
つければ動くはず
qemu+kvm をpreseed で UEFI + lvm インストール
オプションを見直して、preseedインストールがLVMでも動くようになったので追記。
virt-install コマンド
virt-install\
--boot uefi \
--connect=qemu:///system \
--initrd-inject=/home/takuya/preseed.cfg \
--name d03 \
--ram 4096 \
--disk path=/var/lib/libvirt/images/d03.qcow2 \
--vcpus 8 \
--virt-type kvm \
--os-type linux \
--os-variant debian10 \
--graphics none \
--location http://ftp.kddilabs.jp/pub/Linux/distributions/Debian/debian/dists/buster/main/installer-amd64/ \
--extra-args="console=ttyS0"
preseed.cfg の設定
d-i debian-installer/locale string en
d-i debian-installer/language string en
d-i debian-installer/country string JP
d-i debian-installer/locale string en_US.UTF-8
d-i keyboard-configuration/xkb-keymap select us
d-i netcfg/dhcpv6_timeout string 1
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string d03
d-i netcfg/get_domain string .lan
d-i mirror/country string manual
d-i mirror/http/hostname string debian-mirror.sakura.ne.jp
d-i mirror/http/directory string /debian
#d-i mirror/http/proxy string
#d-i mirror/http/proxy string http://172.17.0.1:3142
d-i passwd/root-login boolean false
d-i passwd/make-user boolean true
d-i passwd/user-fullname string takuya
d-i passwd/username string takuya
d-i passwd/user-password-crypted password $5$RFWZ$q0cwtZLAY4er92IloQE0RwBbRPdohAeZb7p6gNhhTH5
d-i clock-setup/utc boolean true
d-i time/zone string Asia/Tokyo
d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server string ntp.nict.jp
### partiion
d-i partman-auto/method string lvm
d-i partman-efi/non_efi_system boolean true
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-lvm/confirm boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-partitioning/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/choose_partition select Finish partitioning and write changes to disk
### tasksel none
tasksel tasksel/first multiselect
### finish
popularity-contest popularity-contest/participate boolean false
d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean false
d-i grub-installer/bootdev string default
d-i finish-install/reboot_in_progress note
インストールの完了
takuya@:~$ sudo qemu-img info /var/lib/libvirt/images/d03.qcow2
image: /var/lib/libvirt/images/d03.qcow2
file format: qcow2
virtual size: 8 GiB (8589934592 bytes)
disk size: 1.4 GiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false
takuya@:~$
過去資料