1fe1755faSAlan Somers# vim: filetype=sh 2fe1755faSAlan Somers 3fe1755faSAlan Somers# SPDX-License-Identifier: BSD-2-Clause 4fe1755faSAlan Somers# 5fe1755faSAlan Somers# Copyright (c) 2024 Axcient 6fe1755faSAlan Somers# All rights reserved. 7fe1755faSAlan Somers# 8fe1755faSAlan Somers# Redistribution and use in source and binary forms, with or without 9fe1755faSAlan Somers# modification, are permitted provided that the following conditions 10fe1755faSAlan Somers# are met: 11fe1755faSAlan Somers# 1. Redistributions of source code must retain the above copyright 12fe1755faSAlan Somers# notice, this list of conditions and the following disclaimer. 13fe1755faSAlan Somers# 2. Redistributions in binary form must reproduce the above copyright 14fe1755faSAlan Somers# notice, this list of conditions and the following disclaimer in the 15fe1755faSAlan Somers# documentation and/or other materials provided with the distribution. 16fe1755faSAlan Somers# 17fe1755faSAlan Somers# THIS DOCUMENTATION IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18fe1755faSAlan Somers# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19fe1755faSAlan Somers# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20fe1755faSAlan Somers# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21fe1755faSAlan Somers# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22fe1755faSAlan Somers# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23fe1755faSAlan Somers# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24fe1755faSAlan Somers# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25fe1755faSAlan Somers# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26fe1755faSAlan Somers# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27fe1755faSAlan Somers 28fe1755faSAlan Somersload_modules() { 29fe1755faSAlan Somers if ! kldstat -q -m ctl; then 30fe1755faSAlan Somers kldload ctl || atf_skip "could not load ctl kernel mod" 31fe1755faSAlan Somers fi 32fe1755faSAlan Somers if ! ctladm port -o on -p 0; then 33fe1755faSAlan Somers atf_skip "could not enable the camsim frontend" 34fe1755faSAlan Somers fi 35fe1755faSAlan Somers} 36fe1755faSAlan Somers 37fe1755faSAlan Somersfind_device() { 38fe1755faSAlan Somers LUN=$1 39fe1755faSAlan Somers 40fe1755faSAlan Somers # Rescan camsim 41fe1755faSAlan Somers # XXX camsim doesn't update when creating a new device. Worse, a 42fe1755faSAlan Somers # rescan won't look for new devices. So we must disable/re-enable it. 43fe1755faSAlan Somers # Worse still, enabling it isn't synchronous, so we need a retry loop 44fe1755faSAlan Somers # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=281000 45fe1755faSAlan Somers retries=5 46fe1755faSAlan Somers ctladm port -o off -p 0 >/dev/null 47fe1755faSAlan Somers ctladm port -o on -p 0 >/dev/null 48fe1755faSAlan Somers HEXLUN=`printf %x $LUN` 49fe1755faSAlan Somers while true; do 50*b032be71SAlan Somers dev=`camcontrol devlist | awk -v lun=$HEXLUN ' 51*b032be71SAlan Somers /FREEBSD CTL.*,pass/ && $9==lun { 52*b032be71SAlan Somers split($10, fields, /[,]/); print fields[1]; 53*b032be71SAlan Somers } 54*b032be71SAlan Somers /FREEBSD CTL.*\(pass/ && $9==lun { 55*b032be71SAlan Somers split($10, fields, /[,]/); print fields[2]; 56*b032be71SAlan Somers } 57*b032be71SAlan Somers ' | sed 's:[()]::'` 58fe1755faSAlan Somers if [ -z "$dev" -o ! -c /dev/$dev ]; then 59fe1755faSAlan Somers retries=$(( $retries - 1 )) 60fe1755faSAlan Somers if [ $retries -eq 0 ]; then 61fe1755faSAlan Somers cat lun-create.txt 62fe1755faSAlan Somers camcontrol devlist 63fe1755faSAlan Somers atf_fail "Could not find GEOM device" 64fe1755faSAlan Somers fi 65fe1755faSAlan Somers sleep 0.1 66fe1755faSAlan Somers continue 67fe1755faSAlan Somers fi 68fe1755faSAlan Somers break 69fe1755faSAlan Somers done 70fe1755faSAlan Somers # Ensure that it's actually ready. camcontrol may report the disk's 71fe1755faSAlan Somers # ident before it's actually ready to receive commands. Maybe that's 72fe1755faSAlan Somers # because all of the GEOM providers must probe it? 73fe1755faSAlan Somers while true; do 74fe1755faSAlan Somers dd if=/dev/$dev bs=4096 count=1 of=/dev/null >/dev/null 2>/dev/null && break 75fe1755faSAlan Somers retries=$(( $retries - 1 )) 76fe1755faSAlan Somers if [ $retries -eq 0 ]; then 77fe1755faSAlan Somers atf_fail "Device never became ready" 78fe1755faSAlan Somers fi 79fe1755faSAlan Somers sleep 0.1 80fe1755faSAlan Somers done 81fe1755faSAlan Somers} 82fe1755faSAlan Somers 83fe1755faSAlan Somers# Create a CTL LUN 84fe1755faSAlan Somerscreate_ramdisk() { 85fe1755faSAlan Somers EXTRA_ARGS=$* 86fe1755faSAlan Somers 87fe1755faSAlan Somers atf_check -o save:lun-create.txt ctladm create -b ramdisk -s 1048576 $EXTRA_ARGS 88fe1755faSAlan Somers atf_check egrep -q "LUN created successfully" lun-create.txt 89fe1755faSAlan Somers LUN=`awk '/LUN ID:/ {print $NF}' lun-create.txt` 90fe1755faSAlan Somers if [ -z "$LUN" ]; then 91fe1755faSAlan Somers atf_fail "Could not find LUN id" 92fe1755faSAlan Somers fi 93fe1755faSAlan Somers find_device $LUN 94fe1755faSAlan Somers} 95fe1755faSAlan Somers 96fe1755faSAlan Somerscleanup() { 97fe1755faSAlan Somers if [ -e "lun-create.txt" ]; then 98fe1755faSAlan Somers lun_id=`awk '/LUN ID:/ {print $NF}' lun-create.txt` 99fe1755faSAlan Somers ctladm remove -b ramdisk -l $lun_id > /dev/null 100fe1755faSAlan Somers fi 101fe1755faSAlan Somers} 102