[Tutor] free loop device

Martin Walsh mwalsh at groktech.org
Wed May 7 02:30:14 CEST 2008


Nathan McBride wrote:
> Yup, I got some help in IRC.  What I ended up doing was using regex to
> pull out each "/dev/loopX".  Then
> took the X and fed it to max which in turn gave me the highest numbered
> loop device in use.  After which I
> then just added 1 to X and added it to the end of "/dev/loop_".  The
> only other thing I had to do was put a
> check in incase there were no used loop devices in which case then it
> defaults to "/dev/loop0".

One might also consider reimplementing 'losetup -f' in python, but after
my own attempt I realize it might not be that practical, and is
potentially dangerous I suppose -- if, like me, you don't fully
understand the underlying system calls. I've attached my attempt for the
sake of discussion only, and not as a solution -- perhaps someone with
interest will correct any errors and make it usable. I would definitely
appreciate it.

Drifting off topic now... I copied most of the find_unused_loop_device
implementation in util-linux/lomount.c[1]. The main points of interest,
and potential doom due to my ignorance, are related to the fcntl.ioctl call:

1) the LOOP_GET_STATUS ioctl op const, isn't exposed in any of the
typical python modules that I can find, and as such I'm worried that
it's value is somehow platform dependent.

2) for a similar reason, I am passing a string of the largest allowed
length as the 3rd arg to the fcntl.ioctl call on line 33, since the size
of the returned data seems to be governed by a struct defined in loop.h,
which needs dev_t from a kernel header. Whew. This seems to work fine on
my ubuntu system, if sloppy. But, since I don't know, I tend to assume
it could cause problems with stability or security.

Anyway, thanks for the interesting question Nathan. Now I have some
reading to do. :)

Marty

[1]
http://www.google.com/codesearch?hl=en&q=show:3q3vE6vLdaY:0lRVP2J7BtU:j-QqODsRp3s&sa=N&ct=rd&cs_p=ftp://ftp.kernel.org/pub/linux/utils/util-linux/testing/util-linux-2.13-pre7.tar.gz&cs_f=util-linux-2.13-pre7/mount/lomount.c&start=1
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: loop.txt
URL: <http://mail.python.org/pipermail/tutor/attachments/20080506/d001e862/attachment.txt>


More information about the Tutor mailing list