Last active
February 19, 2024 21:31
-
-
Save jul/1497d95e09ad08acf84c473029221000 to your computer and use it in GitHub Desktop.
qemu freeBSD on linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# specs for virtual machine | |
declare -A VMACHINE=( [RAM]=4g [CPU]=2 ) | |
# you can override them by redifining them in this file | |
[ -e specs.sh ] && source ./specs.sh | |
function require () { which $1 &> /dev/null && echo "$1 OK" || ( echo "ARG: You SHOULD install $1 $@"; exit 1; ) } | |
echo CHECKING | |
require growisofs | |
require qemu-img package qemu system | |
require curl | |
LOCATION=$(pwd) | |
ARCH=amd64 | |
VERSION=14.0 | |
FLAVOUR=RELEASE | |
FROM_VM=https://download.freebsd.org/ftp/releases/VM-IMAGES/${VERSION}-${FLAVOUR}/${ARCH}/Latest/ | |
FROM_CDROM=https://download.freebsd.org/ftp/releases/ISO-IMAGES/${VERSION}/ | |
FILENAME=FreeBSD-${VERSION}-${FLAVOUR}-${ARCH}.qcow2 | |
CDROM="$( basename $FILENAME .qcow2 )-bootonly.iso" | |
BUILD=${LOCATION}/build | |
read -p "Enter a password for root & user\n# " -s PASSWORD | |
echo | |
[ -f $CDROM ] || curl "$FROM_CDROM/${CDROM}" > "$CDROM" | |
[ -f ${FILENAME}.bak ] ||( curl $FROM_VM/${FILENAME}.xz | xz -d - > ${FILENAME}.bak; ) | |
KEYMAP="fr" | |
BUILD=${LOCATION}/build | |
SKEL=${LOCATION}/skel | |
BUILD_ETC=${LOCATION}/build/etc | |
echo new fresh ISO | |
cp $LOCATION/$CDROM $LOCATION/new.iso | |
cp $LOCATION/$CDROM $LOCATION/new2.iso | |
echo creating build dir | |
rm -rf "$BUILD" | |
mkdir "$BUILD" | |
mkdir "$BUILD/etc" | |
mkdir "$BUILD/boot" | |
[ -d $SKEL ] || (mkdir "$SKEL" && touch "$SKELL/killroy_was_here") | |
echo new fresh qcow2 | |
# using qemu-nbd snapshot here could be smart but I hate their doc | |
cp $LOCATION/$FILENAME.bak $LOCATION/${FILENAME} | |
echo creating /etc/rc.conf | |
cat > $BUILD/etc/rc.conf << EIA | |
keymap="$KEYMAP.iso" | |
console="comconsole" | |
EIA | |
cat > $BUILD/boot/loader.conf << \EOA | |
dcons_load="YES" | |
boot_multicons="YES" | |
boot_serial="YES" | |
console="comconsole" | |
EOA | |
echo creating /boot/loader.conf | |
echo creating /ect/installerconfig to executes custom code | |
cat > $BUILD/etc/installerconfig << \EOG | |
#!/bin/sh | |
echo Resizing | |
ROOTFS_VM=/dev/ada0p4 | |
camcontrol reprobe /dev/ada0 | |
gpart recover /dev/ada0 | |
gpart resize -i 4 /dev/ada0 | |
growfs -y $ROOTFS_VM | |
poweroff | |
EOG | |
chmod +x $BUILD_ETC/installerconfig | |
echo building the new CD image with new layer including installerconfig | |
volid=$(isoinfo -d -i new.iso | awk '/Volume id/{print$3}') | |
growisofs -M new.iso -d -l -r -V "$volid" -graft-points /etc/rc.conf=$BUILD/etc/rc.conf /etc/installerconfig=$BUILD/etc/installerconfig | |
echo resizing VM qemu size | |
qemu-img resize $LOCATION/$FILENAME +1500M | |
echo bootsrtapping qemu image with growfs | |
qemu-system-x86_64 -m ${VMACHINE[RAM]} -smp ${VMACHINE[CPU]} -cdrom new.iso -boot order=d -drive file=${LOCATION}/${FILENAME} | |
echo creating another cdrom | |
ROOTFS_VM=/dev/ada0p4 | |
cat > $BUILD/etc/installerconfig << EOJ | |
#!/bin/sh | |
echo Custom Install 2 | |
set -x | |
echo doing manipulation on the host with a chroot | |
#read -p "debugging the install by launching a promptless shell" TEST | |
#sh | |
mount -t tmpfs -o size=1624m tmpfs /tmp | |
mount $ROOTFS_VM /mnt | |
echo changing rc.conf on the mounted VM | |
cp /etc/rc.conf /mnt/etc/rc.conf | |
echo changing boot/loader on the mounted VM | |
cp /boot/loader.conf.template /mnt/boot/loader.conf | |
cp -a /skel /mnt/tmp | |
cat << 'EOP' | chroot /mnt /bin/sh | |
mount -t devfs devfs /dev | |
dhclient em0 | |
echo 'ifconfig_em0="DHCP"' >> /etc/rc.conf | |
echo setting up network interfaxce >> /etc/motd.template | |
sysrc sshd_enabled="YES" | |
echo ssh automatically starting >> /etc/motd.template | |
pkg install -y python310 | |
#echo installing pip for python3.10 >> /etc/motd.template | |
python3.10 -mensurepip | |
pkg install -y git | |
#echo installing doas and setting user as a root user without >> /etc/motd.template | |
pkg install -y doas | |
echo "permit nopass user as root" > /usr/local/etc/doas.conf | |
sleep 5 | |
sync | |
#portsnap --interactive fetch | |
#portsnap --interactive extract | |
#portsnap --interactive update | |
#sysrc console="comsonsole" | |
EOJ | |
cat >> $BUILD/etc/installerconfig << EOQ | |
#echo bash c installed >> /etc/motd.template | |
sync | |
sleep 5 | |
echo 'export TERM=vt100-color' >> /usr/local/etc/profile | |
echo setting up hostname >> /etc/motd.template | |
echo 'hostname="freebsd-${VERSION}_${FLAVOUR}"' >> /etc/rc.conf | |
sync | |
sleep 5 | |
echo -n '$PASSWORD' | pw useradd -n user -m -G wheel -h 0 | |
echo copying user file in user dir >> /etc/motd.template | |
chown -R user:user /tmp/skel/* | |
echo setting up hostname >> /etc/motd.template | |
cp -a /tmp/skel/* /home/user | |
cp -a /tmp/skel/.[^.]* /home/user | |
rm -rf /tmp/skel | |
sync | |
sleep 5 | |
echo importing user files >> /etc/motd.template | |
echo -n '$PASSWORD' | pw usermod root -h 0 | |
echo setting up pwd >> /etc/motd.template | |
chsh -s /usr/local/bin/bash root | |
sync | |
sleep 5 | |
echo setting up pwd >> /etc/motd.template | |
pkg install -y bash bash-completion | |
sync | |
sleep 5 | |
echo setting up hostname >> /etc/motd.template | |
chsh -s /usr/local/bin/bash user | |
EOP | |
echo emitting power off to avoir hanigup | |
poweroff | |
EOQ | |
chmod +x $BUILD/etc/installerconfig | |
echo building the new iso with new layer | |
volid=$(isoinfo -d -i new2.iso | awk '/Volume id/{print$3}') | |
growisofs -M new2.iso -input-charset=utf8 -d -l -r -V "$volid" -graft-points /etc/rc.conf=$BUILD/etc/rc.conf /etc/installerconfig=$BUILD/etc/installerconfig /boot/loader.conf.template=$BUILD/boot/loader.conf /skel=$SKEL | |
qemu-system-x86_64 -m ${VMACHINE[RAM]} -smp ${VMACHINE[CPU]} -cdrom new2.iso -boot order=d -drive file=${LOCATION}/${FILENAME} | |
echo booting the image | |
echo creating ./start_${VERSION}_${FLAVOUR}.sh for later convenience | |
echo qemu-system-x86_64 -m ${VMACHINE[RAM]} -smp ${VMACHINE[CPU]} -nographic ${LOCATION}/${FILENAME} > ./start_${VERSION}_${FLAVOUR}.sh | |
chmod +x ./start_${VERSION}_${FLAVOUR}.sh | |
./start_${VERSION}_${FLAVOUR}.sh | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment