Open a Terminal application window and enter the following command.
This changes the current working directory to
/Volumes/Data, which is the root folder of the external drive. If you try to eject the drive, you should get a message stating the drive is in use by the Terminal application.
I suppose one could instead enter the commands below which would run processes in the background. Afterwards, you could quit the Terminal application while still protecting the drive from being ejected.
bash -c 'while true; do sleep 3600; done &'
User Solomon Ucko posted the following question as a comment.
To make this permanent, would there be some way to set this as a startup script?
This subject is well covered elsewhere on Stack Exchange. Some examples are given below.