How can I (install and) use the cloop device in Ubuntu?

I need to be able to mount and read on the fly a compressed dd image. I think that cloop is what I need. But I don't see it in /lib/modules and, anyway, I'd like to know how it works first.

Anybody has already managed to mount a compressed image and read it on the fly via cloop?

1 Answer

Unfortunately Ubuntu 12.04 doesn't include the cloop.ko module needed to have a compressed loopback device. However, it's very easy to add it to the running kernel.

I found it on this page (not sure if it's the latest version but compiles and works well), so I wget'd and uncompressed this file. The configure seems to be already included inside the Makefile so running:

make

is enough to build the module correctly. The package also contains the advfs utility, the original name for the command create_compressed_fs whose synopsis can be found here.

I copied the modules in /lib/modules with:

sudo cp cloop.ko /lib/modules/`uname -r`

but this step is not mandatory. Then one can choose to use the advfs or install the ubuntu package cloop-utils that provides the same utility with another name. I chose the package option because it's cleaner (I also suggest to install pv to have a nice progress bar):

sudo aptitude install cloop-utils pv

Now the compressed image can be created through the create_compressed_fs utility, and can be subsequently assigned to the /dev/clooop0 device passing the filename as parameter to the module cloop.ko. Honestly I didn't find a better way to pass the filename to the cloop module and this poses a problem if one wants to mount multiple compressed images in different time frames. However, for the moment I don't have this problem, so this script has been more than adequate for my needs.

#/bin/sh -e
INPUTDEVICE="/dev/sda1"
OUTPUTFILE="/tmp/dev-sda1.cloop.img"
BLOCKSIZE="1048576"
NUMBLOCKS="2000"
COMPRESSIONLEVEL="9"
#COMPRESSIONLEVEL="-1" # for 7zip compression (untested)
CLOOPLOGFILE="/tmp/cloop-creation.log"
CLOOPBLOCKSIZE="64K"
dd if="$INPUTDEVICE" bs="$BLOCKSIZE" count="$NUMBLOCKS" | pv -s "$((NUMBLOCKS * BLOCKSIZE))" -Wpetr | create_compressed_fs -L "$COMPRESSIONLEVEL" -B "$CLOOPBLOCKSIZE" -s "$NUMBLOCKS"M - "$OUTPUTFILE" > "$CLOOPLOGFILE" 2>&1
insmod /lib/modules/`uname -r`/cloop.ko file="$OUTPUTFILE"
mount -o ro /dev/cloop0 /mnt/cloop

Some other information can be found on the Knoppix wiki about cloop devices, here.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like