1#!/bin/bash 2 3get_user_id() 4{ 5 local user=$1 6 local my_uid 7 local my_gid 8 9 if ! my_uid=`id -u $user`; then 10 adduser --shell /bin/bash $user 11 passwd -d $user 12 fi 13 14 my_gid=`id -g $user` 15 echo "$my_uid:$my_gid" 16} 17 18remove_container() 19{ 20 local container=$1 21 22 docker stop $container 23 docker rm $container 24} 25 26add_container() 27{ 28 local container=$1 29 docker run --name $container --hostname=server.example.com \ 30 --device=/dev/ublk-control \ 31 --device-cgroup-rule='a *:* rmw' \ 32 --tmpfs /tmp --tmpfs /run --volume /sys/fs/cgroup:/sys/fs/cgroup:ro \ 33 --detach -i fedora:38 34 docker exec -i $container dnf install -y git libtool automake autoconf g++ liburing-devel 35 docker exec -i $container git config --global http.version HTTP/1.1 36} 37 38install_ublk() 39{ 40 local container=$1 41 42 docker exec -i $container git clone -b next https://github.com/ming1/ubdsrv.git 43 docker exec -i -w /ubdsrv $container autoreconf -i 44 docker exec -i -w /ubdsrv $container ./configure 45 docker exec -i -w /ubdsrv $container make -j4 install 46} 47 48test_ublk() 49{ 50 local container=$1 51 local ugid=$2 52 local ublk_script=`mktemp` 53 54 echo "#!/bin/bash" >> $ublk_script 55 echo "ublk add -t null -n 10 --unprivileged" >> $ublk_script 56 echo "ublk list" >> $ublk_script 57 echo "sleep 2" >> $ublk_script 58 echo "ls -l /dev/ublk[bc]10" >> $ublk_script 59 echo "dd if=/dev/zero of=/dev/ublkb10 bs=1M count=64" >> $ublk_script 60 echo "ublk del -n 10" >> $ublk_script 61 echo "sleep 2" >> $ublk_script 62 echo "ls -l /dev/ublk[bc]10" >> $ublk_script 63 64 chmod +x $ublk_script 65 chown $ugid $ublk_script 66 67 docker cp -a $ublk_script $container:/test.sh 68 docker exec -u $ugid -i $container bash -c "cat /test.sh" 69 docker exec -u $ugid -i $container bash -c ". /test.sh" 70 rm -f $ublk_script 71} 72 73ublk_clean() 74{ 75 local container=$1 76 local this_log=$2 77 78 cat $this_log 79 remove_container $container > /dev/null 2>&1 80 rm "$this_log" 81 exit -1 82} 83 84C="ublk_docker" 85UG="" 86ULOG=`mktemp` 87 88if ! UG=`get_user_id ublk_docker`; then 89 echo "no ublk_docker user for running test" 90 exit -1 91fi 92 93echo "setup container" 94if ! add_container $C >> "$ULOG"; then 95 echo "add container failed" 96 ublk_clean $C "$ULOG" 97fi 98 99echo "install ublk from github" 100if ! install_ublk $C >> "$ULOG" 2>&1; then 101 echo "install ublk failed" 102 ublk_clean $C "$ULOG" 103fi 104 105echo "add/del ublk inside container by user ublk_docker($UG)" 106if ! test_ublk $C $UG; then 107 echo "test ublk in container failed" 108 ublk_clean $C "$ULOG" 109fi 110 111echo "remove container" 112remove_container $C >> "$ULOG" 113rm "$ULOG" 114