#!/bin/bash set -eu distId() { echo $1 |cut -d '/' -f 1 } dist2codename() { echo $1 |cut -d '/' -f 2 } dist2filename() { local id=$(distId $1) local codename=$(dist2codename $1) id=$(echo -n $id | cut -c 1 | tr '[[:lower:]]' '[[:upper:]]'; echo $id | cut -c 2-) codename=$(echo -n $codename | cut -c 1 | tr '[[:lower:]]' '[[:upper:]]'; echo $codename | cut -c 2-) echo $id$codename | tr -d ' ' } FILENAME=$(dist2filename $DIST) set -x SCRIPT_BASE=~/vdn-bullseye/files/$FILENAME-base.sh IMG_NAME=~/vdn-bullseye/files/$FILENAME-base.disk exit 0 cat << EOF > /tmp/bootstrap/config ROOT_PASSWD='iut*' TEST_PASSWD='iut*' ID=\$(echo \$DIST|cut -d '/' -f 1) VERSION_CODENAME=\$(echo \$DIST|cut -d '/' -f 2) KVERS=$(uname -r) EOF set -a . /tmp/bootstrap/config set +a error() { echo $@ >&2 exit 1 } link() { ls -l $1 | cut -d '>' -f 2- | cut -d ' ' -f 2 } injectFileAndLink() { local b=$(basename $1) local d=$(dirname $1) local l #echo "--> $1" [ ! -d $DST/$d ] && mkdir -p $DST/$d [ ! -e $DST/$1 ] && { #echo "cp $1 $DST/$1" cp -a $1 $DST/$1 } if [ -L $1 ]; then l=$(link $1) # Absolute link if ! echo $l | grep -q '^/'; then l=$d/$l fi injectFileAndLink $l fi } injectDepends() { local b d l ld #echo "deps : $1" ! file $1 | grep -q 'dynamically linked' && return # Dynmamic linker l=$(ldd $1 | grep -v '=>' | grep ld-linux | sed -re 's/^[[:space:]]*//' | cut -d ' ' -f 1) #echo " $l" injectFileAndLink $l # Libraries for l in $(ldd $1 | grep '=>' | cut -d '>' -f 2 | cut -d ' ' -f 2); do #echo " -> $l" injectFileAndLink $l done } injectCommandWithDepends() { local f=$1 if ! echo $1 | grep -q '/'; then f=$(whereis -b -B $SEARCH_DIRS -f $1 | cut -d ' ' -f 2) fi echo "*** $f" [ -z "$f" ] && error "Command $f not found in $SEARCH_DIRS !" injectFileAndLink $f injectDepends $f if [ -L $f ]; then f=$(link $f) injectCommandWithDepends $f fi } [ -n "$KVERS" ] || { error "KVERS is empty !?! "; } LOCAL_KERNEL=$(ls /boot/vmlinuz*$KVERS | head -n 1) [ -n "$LOCAL_KERNEL" ] || { error "No kernel found in /boot for $KVERS"; } LOCAL_INITRD=$(ls /boot/initrd*$KVERS | head -n 1) [ -n "$LOCAL_INITRD" ] || { error "No initrd found in /boot for $KVERS"; } FORMAT=$(file $LOCAL_INITRD) case "$FORMAT" in *cpio*) FORMAT=asciCpio;; *Zstandard*) FORMAT=zStandard;; *) echo "Unknown initrd format ($FORMAT)" >&2 exit 1 esac if [ $FORMAT = zStandard ]; then if [ -z "$(which zstdcat)" ]; then echo "zstdcat not found ! Need zstd package !" >&2 exit 1 fi if [ -z "$(which zstd)" ]; then echo "zstd not found ! Need zstd package !" >&2 exit 1 fi fi [ ! -d /tmp/bootstrap/initrd ] && mkdir -p /tmp/bootstrap/initrd cd /tmp/bootstrap if [ ! -e /tmp/bootstrap/initrd/init ]; then echo "Extract $LOCAL_INITRD..." case $FORMAT in asciCpio) ( cd initrd && zcat $LOCAL_INITRD | cpio -idm > /dev/null);; zStandard) ( cd initrd && zstdcat $LOCAL_INITRD | cpio -idm > /dev/null ) esac fi [ -e /tmp/bootstrap/initrd/init.bak ] || cp /tmp/bootstrap/initrd/init /tmp/bootstrap/initrd/init.bak DST=/tmp/bootstrap/initrd #rm -f $DST/bin/busybox $DST/usr/bin/busybox #apt-get download haveged #dpkg -x haveged*.deb initrd #apt-get download libhavege2 #dpkg -x libhavege2*.deb initrd apt-get download busybox dpkg -x busybox_*.deb initrd apt-get download debootstrap dpkg -x debootstrap*.deb initrd apt-get download file dpkg -x file*.deb initrd apt-get download libmagic-mgc dpkg -x libmagic-mgc*.deb initrd #apt-get download haveged #dpkg -x haveged*.deb initrd set +x #[ ! -d initrd/sbin ] && mkdir initrd/sbin cat << EOF > /tmp/bootstrap/initrd/init #!/bin/busybox ash EOF set +u [ -n "$http_proxy" ] && echo "export http_proxy=$http_proxy" >> /tmp/bootstrap/initrd/init [ -n "$https_proxy" ] && echo "export https_proxy=$https_proxy" >> /tmp/bootstrap/initrd/init set -u cat << EOF >> /tmp/bootstrap/initrd/init set -a . /config set +a [ -d /dev ] || mkdir -m 0755 /dev [ -d /proc ] || mkdir /proc [ -d /sys ] || mkdir /sys mkdir -p /var/lock mount -t sysfs -o nodev,noexec,nosuid sysfs /sys mount -t proc -o nodev,noexec,nosuid proc /proc mkdir /dev/pts mount -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /dev/pts modprobe virtio_pci modprobe virtio_blk modprobe ext4 modprobe crc32c modprobe virtio-net mdev -s if ! fdisk -l /dev/vda | grep -q /dev/vda1; then echo -e "n\np\n1\n\n\nw\n" | fdisk /dev/vda mdev -s /bin/mke2fs -j -t ext4 /dev/vda1 fi mkdir -p /tmp/d mount /dev/vda1 /tmp/d || { echo "Can't mount partition !" >&2 echo "Repair + exit to continue or poweroff -f to halt !" /bin/busybox ash } ifconfig eth0 10.0.2.15 route add default gw 10.0.2.2 ln -sf /bin/bash /bin/sh #/usr/sbin/haveged if [ ! -d /tmp/d/bin ]; then echo "Debootstrap $VERSION_CODENAME ..." debootstrap --arch=amd64 $VERSION_CODENAME /tmp/d fi mount -o bind /dev /tmp/d/dev mount -t devpts -o noexec,nosuid,gid=5,mode=0620 devpts /tmp/d/dev/pts mount -o bind /proc /tmp/d/proc mount -o bind /sys /tmp/d/sys cp /config /tmp/d/root/config cp /base.sh /tmp/d/root/base.sh chroot /tmp/d /root/base.sh #echo "In chroot exit to quit" #chroot /tmp/d /bin/bash #/bin/busybox ash poweroff -f EOF chmod 755 /tmp/bootstrap/initrd/init echo "Inject files and program with depends..." [ ! -d $DST/bin ] && mkdir $DST/bin injectCommandWithDepends /bin/bash injectCommandWithDepends /sbin/modprobe injectCommandWithDepends /sbin/fdisk injectCommandWithDepends /sbin/mke2fs injectCommandWithDepends /usr/bin/gpgv injectCommandWithDepends /usr/bin/perl #injectCommandWithDepends /usr/bin/file #injectCommandWithDepends /usr/bin/ldd #injectCommandWithDepends /usr/bin/strace #injectCommandWithDepends /usr/bin/script injectFileAndLink /lib/x86_64-linux-gnu/libresolv.so.2 injectFileAndLink /lib/x86_64-linux-gnu/libnss_files.so.2 injectFileAndLink /lib/x86_64-linux-gnu/libnss_dns.so.2 #injectFileAndLink /usr/lib/x86_64-linux-gnu/libidn2.so cp /sbin/mke2fs $DST/bin/mke2fs #injectCommandWithDepends /bin/grep #mv $DST/usr/bin/grep $DST/bin/grep injectCommandWithDepends /usr/bin/perl #injectCommandWithDepends /usr/bin/wget #cp /usr/bin/wget $DST/bin mkdir -p $DST/usr/share/keyrings cp /usr/share/keyrings/debian-archive-keyring.gpg $DST/usr/share/keyrings cp /etc/resolv.conf $DST/etc/resolv.conf cp /tmp/bootstrap/config $DST cp $SCRIPT_BASE $DST/base.sh # Build /tmp/bootstrap/initrd.img if [ ! -e /tmp/bootstrap/initrd.img ]; then echo "Create /tmp/bootstrap/initrd.img..." case $FORMAT in asciCpio) ( cd initrd && find . | cpio -o -H newc -R root:root | gzip -9 > /tmp/bootstrap/initrd.img);; zStandard) ( cd initrd && find . | cpio -o -H newc -R root:root | zstd -9 > /tmp/bootstrap/initrd.img );; esac fi [ ! -e $DISK ] && { echo "Create sparse disk ($SIZE G)..." dd of=$DISK count=0 bs=1G seek=$SIZE } qemu-system-x86_64 -kernel $LOCAL_KERNEL -smp 8 -enable-kvm -cpu host -device virtio-rng-pci,rng=rng0 -object rng-random,filename=/dev/urandom,id=rng0 -rtc base=localtime -m 3072M -serial mon:stdio -monitor null -nographic -initrd /tmp/bootstrap/initrd.img -append "root=/dev/vda boot=live ro console=ttyS0,115200n8 net.ifnames=0 noresume" -boot order=c -drive file=$DISK,if=virtio,format=raw -device virtio-net-pci,netdev=n0,mac=52:56:0A:E8:00:02 -netdev user,id=n0 exit 0 #qemu-system-x86_64 -smp 8 -enable-kvm -cpu host -device virtio-rng-pci,rng=rng0 -object rng-random,filename=/dev/urandom,id=rng0 -pidfile /tmp/vdn-davalan/vdn-tiny-davalan-pid -rtc base=localtime -m 2048M -serial mon:stdio -monitor null -vnc unix:/tmp/vdn-davalan/vdn-vnc-davalan-tiny-socket -spice unix,disable-ticketing,addr=/tmp/vdn-davalan/vdn-spice-davalan-tiny-socket -device virtio-serial -chardev spicevmc,id=vdagent,debug=0,name=vdagent -device virtserialport,chardev=vdagent,name=com.redhat.spice.0 -initrd /home/davalan/vdn-bullseye/files/initrd-tgz.img-5.10.0-15-amd64 -kernel /home/davalan/vdn-bullseye/files/vmlinuz-5.10.0-15-amd64 -append root=/dev/vda1 ro console=ttyS0,115200n8 vdn-emulator=kvm vdn-mode=tgz net.ifnames=0 noresume -boot order=c -drive file=/home/davalan/vdn-bullseye/files/DebianBullseye-amd64.disk,if=virtio,snapshot=on,format=raw -drive file=/home/scratch/davalan/vdn-save/demo-bullseye/tiny.tgz,if=virtio,media=disk,format=raw -drive file=/tmp/vdn-davalan/vdn-tiny-davalan-part,if=virtio,media=disk,format=raw -drive file=/tmp/vdn-davalan/vdn-tiny-davalan-swap,if=virtio,media=disk,format=raw -drive file=/tmp/vdn-davalan/vdn-tiny-davalan-config.tgz,if=virtio,media=disk,format=raw -device virtio-net-pci,mac=52:56:0A:E8:00:00 -nic none -device virtio-net-pci,netdev=n2,mac=52:56:0A:E8:00:01 -netdev socket,id=n2,mcast=234.0.10.232:9810 -device virtio-net-pci,netdev=n0,mac=52:56:0A:E8:00:02 -netdev user,id=n0,hostfwd=tcp::5022-:22