#!/bin/sh
# DF-5537: vkroot image (adapted from the DF-5337/DF-5289 proven rig)
# Raw UFS filesystem, no disklabel: the vkernel's mountroot falls to the
# manual prompt and the driver pre-feeds "ufs:vkd0".
set -e
dd if=/dev/zero of=/vkroot.img bs=1m count=1200
vnconfig -c /dev/vn0 /vkroot.img
newfs /dev/vn0
mount /dev/vn0 /mnt
mkdir -p /mnt/bin /mnt/sbin /mnt/lib /mnt/libexec /mnt/root /mnt/etc /mnt/dev /mnt/tmp /mnt/var
for f in sh cat ls echo cp mv rm mkdir chmod dd kill ps test sync sleep stty uname hostname; do
  cp /bin/$f /mnt/bin/ 2>/dev/null || true
done
cp "/bin/[" /mnt/bin/ 2>/dev/null || true
for f in mount umount sysctl reboot devfs; do cp /sbin/$f /mnt/sbin/ 2>/dev/null || true; done
cp -p /lib/*.so* /mnt/lib/
cp -p /libexec/ld-elf.so.2 /mnt/libexec/
chmod 1777 /mnt/tmp
cp /root/vkwinch /mnt/root/vkwinch
chmod 755 /mnt/root/vkwinch
cat > /tmp/vkinit.c <<'EOF'
#include <fcntl.h>
#include <unistd.h>
int main(void) {
	int fd = open("/dev/console", O_RDWR);
	if (fd >= 0) { dup2(fd,0); dup2(fd,1); dup2(fd,2); if (fd>2) close(fd); }
	execl("/bin/sh", "sh", (char *)NULL);
	return 1;
}
EOF
cc -static -O -o /mnt/bin/vkinit /tmp/vkinit.c
umount /mnt
vnconfig -u vn0
echo "IMAGE DONE"
