#!/bin/sh ############################################################# # Author: Taryel Hlontsi, 2024 # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see ############################################################# doc=$(cat <<'EOF' NOTES: To be able to mount a directory in NetBSD that runs as KVM/Qemu quest the following has to be done: 1) the kernel must include vio9p driver (not included by default) 2) dev node has to be created for the character device (file share in this case) 3) mount_9p utility must be used to mount the file share 4) (oprional) a service can be created that mounts the fileshare at boot time EOF ) ############################################################# # HELPER FUNCTIONS ############################################################# info() { GREEN='\033[1;32m' NC='\033[0m' echo -e "${GREEN}$1${NC}" } trace() { YELLOW='\033[1;33m' NC='\033[0m' echo -e "${YELLOW}$1${NC}" } warn() { PURPLE='\033[1;35m' NC='\033[0m' echo -e "${PURPLE}$1${NC}" } error() { RED='\033[1;31m' NC='\033[0m' echo -e "${RED}$1${NC}" } ############################################################# # 1 - BUILD AND INSTALL KERNEL THAT SUPPORTS VIO9P DRIVER ############################################################# install_kernel_with_vio9p() { local step_name='STEP 1: BUILD AND INSTALL KERNEL THAT SUPPORTS VIO9P' warn "Press enter to proceed with $step_name"; read confirm info "$step_name" local expected_kernel_name='MYKERNEL2' if uname -v | grep -q "$expected_kernel_name"; then warn 'The kernel seems to be already installed. Nothing to do' return 0 fi local config_path="/usr/src/sys/arch/$(uname -m)/conf/" if [ ! -e "$config_path" ]; then warn 'There is no sources to compile the kernel.' warn 'Go to https://www.netbsd.org/docs/guide/en/chap-fetch.html' warn 'to check how to obtain the sources' return 1 fi cd $config_path pwd cp GENERIC $expected_kernel_name sed -i "s~^#vio9p~vio9p~" $expected_kernel_name config $expected_kernel_name && \ cd ../compile/$expected_kernel_name && \ make depend && \ make && \ mv /netbsd /netbsd.backup && \ mv netbsd / if [ $? != 0 ]; then warn 'Something went bananas while compiling and installing the kernel' return 1 fi info 'Now the machine will be rebooted' info 'After that, run the script again to continue this process' shutdown -r +1 } ############################################################# # 2 - MOUNT SHARED DIR ############################################################# mount_virtio9p_share() { local step_name='STEP 2: MOUNT VIRTIO 9P SHARED DIR' warn "Press enter to proceed with $step_name"; read confirm info "$step_name" local service_name='share' local config_file="/etc/rc.d/${service_name}" cd /dev && sh MAKEDEV vio9p0 if [ $? != 0 ]; then warn 'MAKEDEV failed' return 1 fi cat <<'EOF' > ${config_file} #!/bin/sh # # PROVIDE: share # REQUIRE: DAEMON # KEYWORD: shutdown . /etc/rc.subr name="share" rcvar="$name" command="mount_9p -cu /dev/vio9p0 /mnt" start_cmd="share_start" stop_cmd=":" share_start() { echo "Mounting vio9p0 file share to /mnt" ${command} } load_rc_config $name run_rc_command "$1" EOF local system_config='/etc/rc.conf' local service_enable="$service_name=YES" cat $system_config | grep -q "$service_enable" \ || echo "$service_enable" >> $system_config info 'STEP 2 is done!' info 'Now the machine will be rebooted' info 'Once rebooted, run `ls /mnt` to check if the fileshare is mounted' shutdown -r +1 } info "$doc" install_kernel_with_vio9p \ && mount_virtio9p_share