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