You might want to try out cdctl. It's available here: http://cdctl.sourceforge.net/. This switch will accomplish what you want.
-oS, --lockdoor=S
Locks the eject button so the disc cannot be manually ejected. Requires kernel ver‐
sion 2.2.4 or newer. Set S to 0 to unlock the drive, and to 1 to lock it.
I've used this for the last 10 years on Fedora, RHEL, CentOS but the package is available as a tarball and should be compilable on Ubuntu. NOTE: The package hasn't been updated in a while but it works fine. There's a patch available on gentoo's site that fixes a small issue so that it can be compiled with more recent kernels. The patch is available here:
sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/app-misc/cdctl/files/cdctl-0.15-cdc_ioctls.patch?revision=1.1
Once you've installed it you can use the command to lock the cd tray:
cdctl -o1
To unlock the tray:
cdctl -o0