Sunday, March 23, 2008

Playing with mount and loopback devices

Here, I managed to mount some swap space through a file, which I mounted as a loopback device

# creating the file to setup the swap on.
varmathe@opensuse103-vm:~> dd bs=1024 if=/dev/zero of=./testfile count=102400
102400+0 records in
102400+0 records out
104857600 bytes (105 MB) copied, 4.25891 s, 24.6 MB/s

# for the rest, I need to be root
varmathe@opensuse103-vm:~> su
Password:

# mounting the file as a loopback block device.
opensuse103-vm:/home/varmathe # losetup /dev/loop0 testfile

# formatting the device as swap space
opensuse103-vm:/home/varmathe # mkswap /dev/loop0
Setting up swapspace version 1, size = 104853 kB
no label, UUID=853895fe-e4cf-4741-8106-cc7e88f89210

# asking linux to use that too for swap
opensuse103-vm:/home/varmathe # swapon /dev/loop0


Here is another example where I manage some more playing around with losetup


# Creating a 10 mb disk image. It's just a file of size 10mb full of zeros.
varmathe@opensuse103-vm:~> dd bs=1024 if=/dev/zero of=./testfile count=10240
10240+0 records in
10240+0 records out
10485760 bytes (10 MB) copied, 1.03734 s, 10.1 MB/s

# Now on, I'm root
varmathe@opensuse103-vm:~> su
Password:

# Setting up the loop device
opensuse103-vm:/home/varmathe # losetup /dev/loop0 ./testfile

# Creating an ext3 filesystem on the loop device.
opensuse103-vm:/home/varmathe # mkfs -t ext3 /dev/loop0
mke2fs 1.40.2 (12-Jul-2007)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2560 inodes, 10240 blocks
512 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=10485760
2 block groups
8192 blocks per group, 8192 fragments per group
1280 inodes per group
Superblock backups stored on blocks:
8193

Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

# creating a folder to mount the device on.
opensuse103-vm:/home/varmathe # mkdir test-point

# mounting..
opensuse103-vm:/home/varmathe # mount -t ext3 /dev/loop0 ./test-point/

# Dumping a file there..
opensuse103-vm:/home/varmathe # cd ./test-point/
opensuse103-vm:/home/varmathe/test-point # ls
lost+found
opensuse103-vm:/home/varmathe/test-point # cat > testfile.txt << EOF
> this is a test file to check the making of
> the filesystem with loop devices thing
> hope this thingy will work
> if it does, this file will be reclaimable
> when I remount this filesystem
> EOF
opensuse103-vm:/home/varmathe/test-point # ls
lost+found testfile.txt

# Unmounting the device..
opensuse103-vm:/home/varmathe/test-point # cd ..
opensuse103-vm:/home/varmathe # umount test-point/

# Disassociating the loopback..
opensuse103-vm:/home/varmathe # losetup -d /dev/loop0

# Reassociating..
opensuse103-vm:/home/varmathe # losetup /dev/loop0 ./testfile

# mounting again..
opensuse103-vm:/home/varmathe # mount -t ext3 /dev/loop0 ./test-point/

# Ah yes, it's all still there..
opensuse103-vm:/home/varmathe # cd test-point/
opensuse103-vm:/home/varmathe/test-point # ls
lost+found testfile.txt
opensuse103-vm:/home/varmathe/test-point # cat testfile.txt
this is a test file to check the making of
the filesystem with loop devices thing

hope this thingy will work
if it does, this file will be reclaimable
when I remount this filesystem


No comments: