Kvm-Virtualization

使用 virt-install initrd-inject 預置命令字元串因換行符而失敗

  • September 28, 2018

我正在使用 libvirt 的 virt-install 命令創建一個新的虛擬機。該命令使用 initrd-inject 從管理程序中提取本地預置文件:

virt-install \
--autostart \
--name vm1 \
--ram 4096 \
--location=http://archive.ubuntu.com/ubuntu/dists/precise/main/installer-amd64/ \
--initrd-inject=/var/lib/libvirt/preseeds/vms/preseed.cfg \
--extra-args="locale=en_US console-setup/ask_detect=false keyboard-configuration/layoutcode=us hostname=virtual domain=unassigned-domain interface=auto" \
--vcpu=4 \
--vnc \
--vnclisten=0.0.0.0 \
--noautoconsole \
--os-type=linux \
--os-variant=ubuntukarmic \
-w bridge=br0 \
-w bridge=br1 \
-w network=default \
--disk format=qcow2,size=20,bus=virtio,path=/export/vm/vm1.qcow2

一切正常,直到 preseed 的 late_command 部分動態列印一個新貴腳本以使用以下行歸檔;

d-i preseed/late_command string printf "description \"the run-once bootstrap\"\n\nstart on net-device-up\nstop on runlevel [!2345]\n\npre-start script\n wget -O /root/bootstrap.sh http://my.bootstrap/bootstrap;\nchmod +x /root/bootstrap.sh;\n  /root/bootstrap.sh > /var/log/bootstrap.log 2>&1\nend script" > /target/etc/init/run_bootstrap.conf

用於 vm 的 Ubuntu 12.04 安裝程序停止,我在安裝程序調試日誌中看到以下資訊:

Feb 21 19:44:54 preseed: running preseed command preseed/late_command: printf "description \"the run-once bootstrap\"
Feb 21 19:44:54 log-output: sh: syntax error: unterminated quoted string
Feb 21 19:44:54 finish-install: /bin/preseed_command: return: line 23: Illegal number: start
Feb 21 19:44:54 finish-install: warning: /usr/lib/finish-install.d/07preseed returned error code 2

如果 preseed 的語法很好,因為它在額外參數行中的 http 上正確執行,而不是通過 initrd-inject。雙引號也可以正確轉義,因為如果刪除它們,問題仍然存在。

我的管理程序在 debian-squeeze 上,virtinst=0.600.3-3 從 sid 中​​挑選出來。

據我所知:反斜杠是預置文件格式中換行符的轉義字元。如果您需要在 shell 命令中出現反斜杠,我發現的唯一解決方案是對您需要的值進行 base64 編碼。例如,如果您需要自定義 rc.local,並且不想使用“wget”獲取它(或者不能,因為安裝後環境中沒有 DNS 配置,如 18.04),您可以編碼腳本:

$ base64 < my-rc.local | tr -d \\n

然後,您的 preseed 應包含:

ubiquity ubiquity/success_command string echo your_base64_encoded_file | base64 -d > /target/etc/rc.local ; chmod +x /target/etc/rc.local

(我必須以艱難的方式學習。)

引用自:https://serverfault.com/questions/481244