Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Mounting A File As A Disk

mount file loopback device

  • Please log in to reply
6 replies to this topic

#1 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 25 June 2012 - 09:59 PM

Hey everyone,

I'm writing my own operating system and am having issues installing it on a fake disk. I'm using a file as a virtual disk for use with Bochs, the processor simulator I'm using to test the system. However, I have a problem:

My kernel and some other files need to be written to the disk file, so I've done the following:
# Create a 1.44MB floppy disk. Binary file initialized to zeroes.
dd if=/dev/zero of=disk.dsk bs=512 count=2880

# Assign it to a loopback device
sudo losetup /dev/loop0 disk.dsk

# The system now thinks it's a legit device. Format it.
sudo mkfs -t msdos /dev/loop0

# Mount it.
sudo mount /dev/loop0 /mnt/fakefat

This works. Now I can copy my files onto there, then unmount it, delete the loopback device, and the disk now contains all the files I need.

Problem: When doing it from a bash script it sometimes works. I'm suspecting some sort of race condition, but I have no idea what it could be. There are three types of results:

1) The operation succeeds with no problems.
2) The system claims there's no space left on the device.
3) mount barfs and claims that the mount directory doesn't exist.

Here's my installation script. It takes two parameters, the first being the path to the disk file, the second is the path to the directory where the files to be installed are located. It copies the entire directory tree.

#!/bin/bash

# Copy the bin/disk directory

# Determine the next free loopback device to use
LOOP=$(sudo losetup -f)

# Create a random mount directory
MOUNT="/mnt/$RANDOM-$RANDOM-$RANDOM"

# Set up the loopback device to the disk file
sudo losetup $LOOP $1;
if [[ $? -ne 0 ]]; then
    echo "Failed to set up disk image as loopback device." 1>&2
    exit 1
fi

# Create the mount directory
sudo mkdir $MOUNT;
if [[ $? -ne 0 ]]; then
    echo "Failed to create mount point '$MOUNT'." 1>&2
    sudo losetup -d $LOOP
    exit 2
fi

# Mount the loopback device onto the directory we just made
sudo mount $LOOP $MOUNT;
if [[ $? -ne 0 ]]; then
    echo "Failed to mount disk image." 1>&2
    sudo losetup -d $LOOP
    exit 3
fi

RETVAL=0

# Copy the directory tree to our disk. If we fail, keep going because
# we still need to unmount this.
sudo cp -r $2/* $MOUNT/;
if [[ $? -ne 0 ]]; then
    echo "Failed to copy files to disk image." 1>&2
    RETVAL=4
fi

# Unmount the disk
sudo umount -l $MOUNT;
if [[ $? -ne 0 ]]; then
    echo "Failed. Be sure to remove mount point '$MOUNT'" 1>&2
    echo "and delete the loopback device '$LOOP'" 1>&2
    RETVAL=5
fi

# Delete the loopback device
sudo losetup -d $LOOP;
if [[ $? -ne 0 ]]; then
    echo "Failed. Be sure to remove mount point '$MOUNT'" 1>&2
    echo "and delete the loopback device '$LOOP'" 1>&2
RETVAL=6
fi

# Remove the mount directory
sudo rmdir $MOUNT;
if [[ $? -ne 0 ]]; then
    echo "Failed. Be sure to remove the mount point '$MOUNT' later." 1>&2
    RETVAL=7
fi

# Exit the script
exit $RETVAL

Any ideas?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 26 June 2012 - 06:31 AM

Very stupid question, and that is why it might be worth a penny, did you try hardcoding the script parameters and vars ? i mean, the loopback, the random dir,

like, you start : if exist, delete , etc

then create everything
go

i say that since you have different results doing the same thing ... then probably you are not doing the same thing, at least to O.S eyes

please Dar, don't get mad at me, i know you know your stuff right, but sometimes evil is on those little details, :D

once you test what goes good and what goes wrong you learn and then do the script right
  • 0

#3 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 26 June 2012 - 09:02 AM

1) It works when I do it manually, however it usually doesn't work with a script, regardless of whether I have hard-coded parameters or not. (Hence my assessment that it's a race condition.) Putting one-second sleep statements after each command doesn't help. All it does is increase the ratio of "disk is full" to "directory does not exist" errors.
2) Running the script with the same parameters back-to-back usually produces different results.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 26 June 2012 - 09:33 AM

Well, manually vs script, it gives an idea of what is happening, and by reading carefully i think i get where the race condition kicks

it think mkfs an mount returns control to the script while the core ops are still executing in background

i will go with a previous check on filesystem state (mkfs does format right ? format takes a little more time) and run the script after filesystem is in ok state.

so any loop who checks on filesystem ready state, with some sleep in between should work

that explain both, not mounted and not full space, meaining you get there either when format didn't finish (ergo not at full capacity) or when it didn't even started.
that also explains why you get more "disk full"s when you insert an sleep
  • 0

#5 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 26 June 2012 - 09:59 AM

I manually formatted the disk file beforehand; the script just automates the mount-copy-unmount process.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 26 June 2012 - 10:02 AM

then the checkpoint is in mount state, but the point stands, device is not ready, still doing something on background, letting you work on partially mounted device or in no device at all
  • 0

#7 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 26 June 2012 - 02:14 PM

So apparently the problem was that I was removing the mount point directory after trying to delete the loopback device. If I delete the directory beforehand, it always works. Any idea why?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download