Re: [Tutor] free loop device
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=enq=show:3q3vE6vLdaY:0lRVP2J7BtU:j-QqODsRp3ssa=Nct=rdcs_p=ftp://ftp.kernel.org/pub/linux/utils/util-linux/testing/util-linux-2.13-pre7.tar.gzcs_f=util-linux-2.13-pre7/mount/lomount.cstart=1 #!/usr/bin/env python2.5 import os import stat import errno import fcntl if os.uname()[4] == 'x86_64': LOOP_GET_STATUS = 0x4C05 else: LOOP_GET_STATUS = 0x4C03 def find_unused_loop_device(): Return the next unused loop device node, returns None if the next device cannot be determined (and swallows exceptions encountered along the way: permission denied, no such file, etc). for loop_format in ['/dev/loop%d', '/dev/loop/%d']: for i in range(256): dev = loop_format % i try: st = os.stat(dev) except OSError: break # assume invalid loop_format if stat.S_ISBLK(st.st_mode): try: fd = os.open(dev, os.O_RDONLY) except OSError: pass # assume permission denied else: try: fcntl.ioctl(fd, LOOP_GET_STATUS, 1024*'\x00') except IOError, e: if e.errno == errno.ENXIO: os.close(fd) return dev os.close(fd) if __name__ == '__main__': print find_unused_loop_device() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] free loop device
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hey Guys, I have a program that uses the loop devices. However I originally was finding the free one with: 'losetup -f'. Which returns the next free loop device. :D However, I found that some of the other distros have a version of losetup that does not include that switch. So I am trying to work up another way to get the next device. What I'm figuring is doing 'losetup -a', parsing the returned data, and incrementing it by one... However I don't know how to accomplish this. An example 'losetup -a' would be like this: [EMAIL PROTECTED] Desktop]# losetup -a /dev/loop0: [fd00]:11305639 (enc) /dev/loop1: [fd00]:11306533 (/home/nmcbride/Desktop/xxx.iso) Can anyone give me a hand? Thanks, Nate -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org iD8DBQFIH2Oo/n+duykW6K8RAnCTAJ480KREx5udm22wFx3KbNQ08p6AeQCfcfoO IS+ZG3J8M1/oEjcHN/pLhU4= =5zQk -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] free loop device
Nathan McBride wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hey Guys, I have a program that uses the loop devices. However I originally was finding the free one with: 'losetup -f'. Which returns the next free loop device. :D However, I found that some of the other distros have a version of losetup that does not include that switch. So I am trying to work up another way to get the next device. What I'm figuring is doing 'losetup -a', parsing the returned data, and incrementing it by one... However I don't know how to accomplish this. An example 'losetup -a' would be like this: [EMAIL PROTECTED] Desktop]# losetup -a /dev/loop0: [fd00]:11305639 (enc) /dev/loop1: [fd00]:11306533 (/home/nmcbride/Desktop/xxx.iso) Can anyone give me a hand? I for one have no idea how this relates to Python. If others do then they are likely to help you. -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] free loop device
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 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. Works like a charm. :D Nate bob gailer wrote: Nathan McBride wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hey Guys, I have a program that uses the loop devices. However I originally was finding the free one with: 'losetup -f'. Which returns the next free loop device. :D However, I found that some of the other distros have a version of losetup that does not include that switch. So I am trying to work up another way to get the next device. What I'm figuring is doing 'losetup -a', parsing the returned data, and incrementing it by one... However I don't know how to accomplish this. An example 'losetup -a' would be like this: [EMAIL PROTECTED] Desktop]# losetup -a /dev/loop0: [fd00]:11305639 (enc) /dev/loop1: [fd00]:11306533 (/home/nmcbride/Desktop/xxx.iso) Can anyone give me a hand? I for one have no idea how this relates to Python. If others do then they are likely to help you. -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.7 (GNU/Linux) Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org iD8DBQFIH78C/n+duykW6K8RAuE3AJoDu69W1D7dldLPxXpCfRGuoc++FQCgj2G3 4fG0aSw7fe/noi4hqesG9oA= =vas+ -END PGP SIGNATURE- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor