Guest User

Untitled

a guest
Apr 4th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. #!/bin/bash
  2. set -e
  3.  
  4. # specs for virtual machine
  5. declare -A VMACHINE=( [RAM]=4g [CPU]=2 )
  6. # you can override them by redifining them in this file
  7. [ -e specs.sh ] && source ./specs.sh
  8.  
  9. echo all files in skel will be copied in /home/user
  10.  
  11. touch skel
  12.  
  13. function require () { which $1 &> /dev/null && echo "$1 OK" || ( echo "ARG: You SHOULD install $1 $@"; exit 1; ) }
  14. echo CHECKING
  15.  
  16. require growisofs
  17. require qemu-img package qemu system
  18. require curl
  19.  
  20. LOCATION=$(pwd)
  21. ARCH=${ARCH:-amd64}
  22. VERSION=${VERSION:-14.2}
  23. FLAVOUR=${FLAVOUR:-RELEASE}
  24. FROM_VM=https://download.freebsd.org/ftp/releases/VM-IMAGES/${VERSION}-${FLAVOUR}/${ARCH}/Latest/
  25. FROM_CDROM=https://download.freebsd.org/ftp/releases/ISO-IMAGES/${VERSION}/
  26. FILENAME=FreeBSD-${VERSION}-${FLAVOUR}-${ARCH}.qcow2
  27. CDROM="$( basename $FILENAME .qcow2 )-bootonly.iso"
  28. BUILD=${LOCATION}/build
  29. read -p "Enter a password for root & user\n# " -s PASSWORD
  30. echo
  31.  
  32. [ -f $CDROM ] || curl "$FROM_CDROM/${CDROM}" > "$CDROM"
  33. [ -f ${FILENAME}.bak ] ||( curl $FROM_VM/${FILENAME}.xz | xz -d - > ${FILENAME}.bak; )
  34. KEYMAP="fr"
  35. BUILD=${LOCATION}/build
  36. SKEL=${LOCATION}/skel
  37. BUILD_ETC=${LOCATION}/build/etc
  38.  
  39. echo new fresh ISO
  40. cp $LOCATION/$CDROM $LOCATION/new.iso
  41. cp $LOCATION/$CDROM $LOCATION/new2.iso
  42.  
  43. echo creating build dir
  44.  
  45. rm -rf "$BUILD"
  46. mkdir "$BUILD"
  47. mkdir "$BUILD/etc"
  48. mkdir "$BUILD/boot"
  49. [ -d $SKEL ] || (mkdir "$SKEL" && touch "$SKELL/killroy_was_here")
  50.  
  51. echo new fresh qcow2
  52. # using qemu-nbd snapshot here could be smart but I hate their doc
  53. cp $LOCATION/$FILENAME.bak $LOCATION/${FILENAME}
  54.  
  55.  
  56.  
  57.  
  58. echo creating /etc/rc.conf
  59. cat > $BUILD/etc/rc.conf << EIA
  60. keymap="$KEYMAP"
  61. firstboot_freebsd_update_enable=YES
  62. growfs_enable=YES
  63. dumpdev="AUTO"
  64. EIA
  65. cat > $BUILD/boot/loader.conf << \EOA
  66. dcons_load="YES"
  67. boot_multicons="YES"
  68. boot_serial="YES"
  69. console="comconsole"
  70. EOA
  71.  
  72. echo creating /boot/loader.conf
  73.  
  74.  
  75. echo creating /ect/installerconfig to executes custom code
  76. cat > $BUILD/etc/installerconfig << \EOG
  77. #!/bin/sh
  78.  
  79. echo Resizing
  80. ROOTFS_VM=/dev/ada0p4
  81. JAILFS_VM=/dev/ada0p5
  82. camcontrol reprobe /dev/ada0
  83. echo repairaing the size extension made with qemuimg
  84. gpart recover ada0
  85. gpart resize -i 4 /dev/ada0
  86. growfs -y $ROOTFS_VM
  87. echo adding zfs partition
  88. #gpart add -t freebsd-zfs -a 4k -s 1500M ada0
  89. gpart recover ada0
  90.  
  91. poweroff
  92.  
  93.  
  94. EOG
  95.  
  96.  
  97.  
  98. chmod +x $BUILD_ETC/installerconfig
  99. echo building the new CD image with new layer including installerconfig
  100.  
  101. volid=$(isoinfo -d -i new.iso | awk '/Volume id/{print$3}')
  102.  
  103. growisofs -M new.iso -d -l -r -V "$volid" -graft-points /etc/rc.conf=$BUILD/etc/rc.conf /etc/installerconfig=$BUILD/etc/installerconfig
  104. echo resizing VM qemu size
  105. qemu-img resize $LOCATION/$FILENAME +5000M
  106. echo bootsrtapping qemu image with growfs
  107. qemu-system-x86_64 -m ${VMACHINE[RAM]} -smp ${VMACHINE[CPU]} -cdrom new.iso -boot order=d -drive file=${LOCATION}/${FILENAME}
  108.  
  109.  
  110. echo creating another cdrom
  111. ROOTFS_VM=/dev/ada0p4
  112.  
  113. cat > $BUILD/etc/installerconfig << EOJ
  114. #!/bin/sh
  115.  
  116. echo Custom Install 2
  117.  
  118. set -x
  119. echo doing manipulation on the host with a chroot
  120. #read -p "debugging the install by launching a promptless shell" TEST
  121. #sh
  122. mount -t tmpfs -o size=1624m tmpfs /tmp
  123. #fsck -y $ROOTFS_VM
  124. mount $ROOTFS_VM /mnt
  125.  
  126. echo changing rc.conf on the mounted VM
  127. cp /etc/rc.conf /mnt/etc/rc.conf
  128. echo changing boot/loader on the mounted VM
  129. cp /boot/loader.conf.template /mnt/boot/loader.conf
  130. cp -a /skel /mnt/tmp
  131.  
  132. #echo setting up ZFS >> /etc/motd.template
  133. #service zfs start
  134. #zpool create jails /dev/ada0p5
  135.  
  136.  
  137. cat << 'EOP' | chroot /mnt /bin/sh
  138. mount -t devfs devfs /dev
  139.  
  140. echo 'ifconfig_em0="DHCP"' >> /etc/rc.conf
  141. echo setting up network interfaxce >> /etc/motd.template
  142. service netif restart
  143. echo fixing my broken local dnsmaq using 1.1.1.1
  144. echo making 1.1.1.1 the first dns server
  145. echo 'prepend domain-name-servers 1.1.1.1;' > /etc/dhclient.conf
  146.  
  147. dhclient em0
  148.  
  149.  
  150. ifconfig em0
  151. netstat -nr
  152. sysrc sshd_enabled="YES"
  153. echo ssh automatically starting >> /etc/motd.template
  154. pkg install -y python310
  155. echo installing pip for python3.10 >> /etc/motd.template
  156. echo use ports to have latest python current >> /etc/motd.template
  157. python3.10 -mensurepip
  158. pkg install -y git
  159. echo installing doas and setting user as a root user without pass >> /etc/motd.template
  160. pkg install -y doas
  161. echo "permit nopass user as root" > /usr/local/etc/doas.conf
  162. sync
  163. sleep 5
  164.  
  165. #portsnap --interactive fetch
  166. #portsnap --interactive extract
  167. #portsnap --interactive update
  168. #sysrc console="comsonsole"
  169.  
  170. EOJ
  171.  
  172. cat >> $BUILD/etc/installerconfig << EOQ
  173. #echo bash c installed >> /etc/motd.template
  174. sleep 5
  175.  
  176. echo 'export TERM=vt100-color' >> /usr/local/etc/profile
  177. echo setting up hostname >> /etc/motd.template
  178. echo 'hostname="freebsd-${VERSION}_${FLAVOUR}"' >> /etc/rc.conf
  179. sync
  180. sleep 5
  181.  
  182. echo -n '$PASSWORD' | pw useradd -n user -m -G wheel -h 0
  183. echo copying user file in user dir >> /etc/motd.template
  184. chown -R user:user /tmp/skel/*
  185. echo setting up hostname >> /etc/motd.template
  186. cp -a /tmp/skel/* /home/user
  187. cp -a /tmp/skel/.[^.]* /home/user
  188. rm -rf /tmp/skel
  189. sync
  190. sleep 5
  191.  
  192. echo importing user files >> /etc/motd.template
  193. echo -n '$PASSWORD' | pw usermod root -h 0
  194. echo setting up pwd >> /etc/motd.template
  195. chsh -s /usr/local/bin/bash root
  196. sync
  197. sleep 5
  198.  
  199. echo setting up pwd >> /etc/motd.template
  200. pkg install -y bash bash-completion
  201.  
  202. echo installing podman
  203.  
  204. pkg install -y podman-suite
  205. pkg install -y podman
  206.  
  207.  
  208. echo https://podman.io/docs/installation >> /etc/motd.template
  209. sync
  210. sleep 5
  211.  
  212.  
  213. ##### BUG WAS THERE ##########
  214. echo "fdesc /dev/fd fdescfs rw 0 0" >> /etc/fstab
  215.  
  216. service podman enable
  217.  
  218. cp /usr/local/etc/containers/pf.conf.sample /etc/pf.conf
  219.  
  220. echo mod pf >> /etc/motd.template
  221. sync
  222. perl -pe 's/v4egress_if.*/v4egress_if="em0"/' /etc/pf.conf
  223. perl -pe 's/v6egress_if.*/v6egress_if="em0"/' /etc/pf.conf
  224.  
  225. echo enabling pf >> /etc/motd.template
  226. service pf enable
  227. echo 'pf_load="YES"' >> /boot/loader.conf
  228. echo 'net.pf.filter_local=1' >> /etc/sysctl.conf.local
  229.  
  230. sync
  231. sleep 5
  232. echo changing storage to vfs >> /etc/motd.template
  233. sed -I .bak -e 's/driver = "zfs"/driver = "vfs"/' /usr/local/etc/containers/storage.conf
  234.  
  235.  
  236. sysrc linux_enable=YES
  237. echo https://github.com/containers/podman/blob/main/troubleshooting.md >> /etc/motd.template
  238.  
  239. echo 'unqualified-search-registries = ["docker.io", "registry.fedoraproject.org", "quay.io", "registry.access.redhat.com"]' >> /etc/containers/registries.conf
  240. echo setting up hostname >> /etc/motd.template
  241.  
  242. sync
  243. sleep 5
  244. chsh -s /usr/local/bin/bash user
  245.  
  246. EOP
  247.  
  248. echo emitting power off to avoir hanigup
  249. sync
  250.  
  251. sleep 5
  252. poweroff
  253.  
  254. EOQ
  255.  
  256. chmod +x $BUILD/etc/installerconfig
  257. echo building the new iso with new layer
  258.  
  259. volid=$(isoinfo -d -i new2.iso | awk '/Volume id/{print$3}')
  260.  
  261. 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
  262.  
  263.  
  264. qemu-system-x86_64 -m ${VMACHINE[RAM]} -smp ${VMACHINE[CPU]} -cdrom new2.iso -boot order=d -drive file=${LOCATION}/${FILENAME}
  265.  
  266. echo booting the image
  267. echo creating ./start_${VERSION}_${FLAVOUR}.sh for later convenience
  268. echo qemu-system-x86_64 -m ${VMACHINE[RAM]} -smp ${VMACHINE[CPU]} -nographic ${LOCATION}/${FILENAME} \$\* > ./start_${VERSION}_${FLAVOUR}.sh
  269. chmod +x ./start_${VERSION}_${FLAVOUR}.sh
  270. ./start_${VERSION}_${FLAVOUR}.sh
Advertisement
Add Comment
Please, Sign In to add comment