preseed command string fail with newline character using virt-install initrd-inject

I am creating a new virtual machine using libvirt's virt-install command. The command pulls a local preseed file from the hypervisor using 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

Everything works fine until the late_command portion of the preseed which dynamically prints an upstart script to file with the following line;

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

The Ubuntu 12.04 installer for the vm stops and I see the following information in the installer debug logs:

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

The syntax for the preseed if fine, because it runs properly over http in the extra-args line instead of through initrd-inject. Also the double quotes get escaped properly, because the problem still exists if they are removed.

My hypervisor is on debian-squeeze with virtinst=0.600.3-3 cherry-picked from sid.


As far as I can tell: The backslash is an escape character for newlines in the preseed file format. If you need a backslash to appear in a shell command, the only solution I've found is to base64 encode the value that you need. For example, if you need a custom rc.local, and you don't want to fetch it with "wget" (or can't, because there's no DNS configuration in the post-install environment, as in 18.04), you can encode the script:

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

Then, your preseed should contain:

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

(I had to learn that the hard way.)