Loopback 2 offers significant improvements over the initial release across the board, even though version 1 was quite useful and good. The interface change to allow a more visual drag-and-drop. Read the Migration Guide for instructions on upgrading to LoopBack 3.0. The Release Notes provide a comprehensive reference for all the changes between 2.x and 3.x. Getting started.
by Andrew M. Bishop, amb@gedanken.demon.co.uk
v1.1, 24 September 1999This HOWTO explains how to use the Linux loopback device to create a Linuxnative filesystem format installation that can be run from a DOS partitionwithout re-partitioning. Other uses of this same technique are also discussed.1.1 Copyright
The Loopback Root Filesystem HOWTOCopyright (C) 1998,99 Andrew M. Bishop (amb@gedanken.demon.co.uk).
This documentation is free documentation; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.
This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.
The GNU General Public License is available from http://www.fsf.org/or, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
1.2 Revision History
Initial Version (June 1998)
Slight Modifications, kernel version changes, typos etc. (1998 - July 1999)
Added Copyright Information and Re-Submitted (September 1999)
First I will describe some of the general principles that are used in thesetting up of a loopback filesystem as the root device.
2.1 Loopback Devices
A loopback device in Linux is a virtual device that can be used like anyother media device.
Examples of normal media devices are hard disk partitions like/dev/hda1
, /dev/hda2
, /dev/sda1
, or entire disks likethe floppy disk /dev/fd0
etc. They are all devices that can be used tohold a files and directory structures. They can be formatted with thefilesystem that is required (ext2fs, msdos, ntfs etc.) and then mounted.
The loopback filesystem associates a file on another filesystem as a completedevice. This can then be formatted and mounted just like any of the otherdevices listed above. To do this the device called /dev/loop0
or/dev/loop1
etc is associated with the file and then this new virtualdevice is mounted.
2.2 Ramdisk Devices
In Linux it is also possible to have another type of virtual device mounted as afilesystem, this is the ramdisk device.
In this case the device does not refer to any physical hardware, but to aportion of memory that is set aside for the purpose. The memory that isallocated is never swapped out to disk, but remains in the disk cache.
A ramdisk can be created at any time by writing to the ramdisk device/dev/ram0
or /dev/ram1
etc. This can then be formatted andmounted in the same way that the loopback device is.
When a ramdisk is used to boot from (as is often done on Linux installationdisks or rescue disks) then the disk image (the entire contents of the disk as asingle file) can be stored on the boot floppy in a compressed form. This isautomatically recognised by the kernel when it boots and is uncompressed intothe ramdisk before it is mounted.
2.3 The Initial Ramdisk Device
The initial ramdisk device in Linux is another important mechanism thatwe need to be able to use a loopback device as a the root filesystem.
When the initial ramdisk is used the filesystem image is copied into memory andmounted so that the files on it can be accessed. A program on this ramdisk(called /linuxrc
) is run and when it is finished a different device ismounted as the root filesystem. The old ramdisk is still present though and ismounted on the directory /initrd
if present or available through thedevice /dev/initrd
.
This is unusual behaviour since the normal boot sequence boots from thedesignated root partition and keeps on running. With the initial ramdisk optionthe root partition is allowed to change before the main boot sequence isstarted.
2.4 The Root Filesystem
The root filesystem is the device that is mounted first so that it appears asthe directory called /
after booting.
There are a number of complications about the root filesystem that are due tothe fact that it contains all files. When booting the rc
scripts arerun, these are either the files in /etc/rc.d
or /etc/rc?.d
depending on the version of the /etc/init
program.
When the system has booted it is not possible to unmount the root partition orchange it since all programs will be using it to some extent. This is why theinitial ramdisk is so useful because it can be used so that the final rootpartition is not the same as the one that is loaded at boot time.
2.5 The Linux Boot Sequence
To show how the initial ramdisk operates in the boot sequence, the order ofevents is listed below.
- The kernel is loaded into memory, this is performed by
LILO
orLOADLIN
. You can see theLoading..
message as this happens. - The ramdisk image is loaded into memory, again this is performed by
LILO
orLOADLIN.
You can see theLoading..
messageagain as this happens. - The kernel is initialised, including parsing the command line options andsetting of the ramdisk as the root device.
- The program
/linuxrc
is run on the initial ramdisk. - The root device is changed to that specified in the kernel parameter.
- The init program
/etc/init
is run which will perform the userconfigurable boot sequence.
This is just a simplified version of what happens, but is sufficient to explainhow the kernel starts up and where the initial ramdisk is used.
Now that the general principles are explained the method of creating theloopback device can be explained.
3.1 Requirements
To create the loopback root device will require a number of things.
- A working Linux system.
- A way to copy large files onto the target DOS partition.
Most important is access to an installed Linux system. This is because the loopdevice can only be created under Linux. This will mean that it is not possibleto bootstrap a working system from nothing. The requirements of the Linuxsystem that you use is that you can compile a kernel on it.
Once the loopback device is created it will be a large file. I have used an 80MB files, but while this was sufficient for an X terminal it may not be enoughif you want to use it for much else. This file must be copied onto the DOSpartition, so either a network or a lot of floppy disks must be used.
The software that you will require includes
Loopback Rx | Pharmacy Software | Loopback Analytics
LOADLIN
version 1.6 or above- A version of
mount
that supports loopback devices - A version of the kernel that supports the required options.
All of these should be standard for recent Linux installations.
3.2 Creating the Linux Kernel
I created the loopback device using Linux kernel version 2.0.31, other versionsshould also work, but they must have at least the options listed below.
The kernel options that you will need to enable are the following:
- RAM disk support (
CONFIG_BLK_DEV_RAM
). - Initial RAM disk (initrd) support (
CONFIG_BLK_DEV_INITRD
). - Loop device support (
CONFIG_BLK_DEV_LOOP
). - fat fs support (
CONFIG_FAT_FS
). - msdos fs support (
CONFIG_MSDOS_FS
).
The first two are for the RAM disk device itself and the initial ram diskdevice. The next one is the loop back filesystem option. The last two are themsdos filesystem support which is required to mount the DOS partitition.
Compiling a kernel without modules is the easiest option, although if you dowant modules then it should be possible although I have not tried it. Ifmodules are used then you should make sure that you have the options abovecompiled in and not as modules themselves.
Depending on the kernel version that you have you may need to apply a kernelpatch. It is a very simple one that allows the loopback device to be used asthe root filesystem.
- Kernel versions before 2.0.0; I have no information about these.
- Kernel version 2.0.0 to 2.0.34; you need to apply the kernel patch for2.0.x kernels as shown below.
- Kernel version 2.0.35 to 2.0.x; no kernel patch is required.
- Kernel version 2.1.x; you need to apply the kernel patch for 2.0.x or2.2.x kernels as shown below, depending on the exact 2.1.x version.
- Kernel version 2.2.0 to 2.2.10; you need to apply the kernel patch for2.2.x kernels as shown below.
- Kernel version 2.3.x; you need to apply the kernel patch for 2.2.x kernelsas shown below.
For 2.0.x kernels the file /init/main.c
needs to have a single lineadded to it as shown by the modified version below. The line that says'loop', 0x0700
is the one that was added.
For 2.2.x kernels the file /init/main.c
needs to have three lines addedto it as shown by the modified version below. The line that says 'loop',0x0700
and the ones either side of it are the ones that were added.
Once the kernel is configured it should be compiled to produce a zImage
file (make zImage
). This file will be arch/i386/boot/zImage
when compiled.
3.3 Creating the Initial Ramdisk Device
The initial ramdisk is most easily created as a loopback device from the start.You will need to do this as root, the commands that you need to execute arelisted below, they are assumed to be run from root's home directory(/root
).
There are a number of steps to this, but they can be described as follows.
- Create a mount point for the initial ramdisk (an empty directory).
- Create an empty file of the size required. Here I have used 1024kB, you mayneed less or more depending on the contents, (the size is the last parameter).
- Make an ext2 filesystem on the empty file.
- Mount the file onto the mount point, this uses the loopback device.
- Change to the mounted loopback device.
- Create the files that are required (see below for details).
- Move out of the mounted loopback device.
- Unmount the device.
- Create a compressed version for use later.
Contents Of The Initial Ramdisk
The files that you will need on the ramdisk are the minimum requirements to beable to execute any commands.
/linuxrc
The script that is run to mount the msdos file system(see below)./lib/*
The dynamic linker and the libraries that the programsneed./etc/*
The cache used by the dynamic linker (not strictly needed,but does stop it complaining)./bin/*
A shell interpreter (ash
because it is smallerthanbash
. Themount
andlosetup
programs forhandling the DOS disk and setting up the loopback devices./dev/*
The devices that will be used. You need/dev/zero
forld-linux.so
,/dev/hda*
to mount themsdos disk and/dev/loop*
for the lopback device./mnt
An empty directory to mount the msdos disk on.
The initial ramdisk that I used is listed below, the contents come to about800kB when the overhead of the filesystem are taken into account.
The only complex steps about this are the devices in dev
. Use themknod
program to create them, use the existing devices in /dev
as a template to get the required parameters.
The /linuxrc file
The /linuxrc
file on the initial ramdisk is required to do all of thepreparations so that the loopback device can be used for the root partition whenit exits.
The example below tries to mount /dev/hda1
as an msdos partition andif it succeeds then sets up the files /linux/linuxdsk.img
as/dev/loop0
and /linux/linuxswp.img
as /dev/loop1
.
The first device /dev/loop0
will become the root device and the secondone /dev/loop1
will become the swap space.
If you want to be able to write to the DOS partition as a non-root user when youhave finished then you should use mount -n -t msdos /dev/hda1 /mnt -ouid=0,gid=0,umask=000,quiet
instead. This will map all accesses to the DOSpartition to root and set the permissions appropriately.
3.4 Creating The Root Device
The root device that you will be using is the file linuxdsk.img
. Youwill need to create this in the same way that the initial ramdisk was created,but bigger. You can install any Linux installation that you like onto thisdisk.
The easiest way might be to copy an existing Linux installation into it. Analternative is to install a new Linux installation onto it.
Assuming that you have done this, there are some minor changes that you mustmake.
The /etc/fstab
file must reference the root partition and theswap using the two loopback devices that are setup on the initial ramdisk.
This will ensure that when the real root device is used the kernel will not beconfused about where the root device is. It will also allow the swap space tobe added in the same way a swap partition is normally used. You should removeany other reference to a root disk device or swap partition.
If you want to be able to read the DOS partition after Linux has started thenyou will need to make a number of extra small changes.
Create a directory called /initrd
, this is where the initial ramdiskwill be mounted once the loopback root filesystem is mounted.
Create a symbolic link called /DOS
that points to /initrd/mnt
where the real DOS parition will be mounted.
Add a line into the rc file that mounts the disks. This should run the commandmount -f -t msdos /dev/hda1 /initrd/mnt
, this will create a 'fake'mount of the DOS partition so that all programs (like df
) will know thatthe DOS partition is mounted and where to find it. If you used differentoptions in the /linuxrc
file that obviously you should use them herealso.
There is no need to have a Linux kernel on this root device since that isalready loaded earlier. If you are using modules however then you shouldinclude them on this device as normal.
3.5 Creating the Swap Device
The root device that you will be using is the file linuxswap.img
. Theswap device is very simple to create. Create an empty file as was done for theinitial ramdisk and then run mkswap linuxswap.img
to intialise it.
The size of the swap space will depend on what you plan to do with the installedsystem, but I would recommend between 8MB and the amount of RAM that you have.
3.6 Creating the MSDOS Directory
The files that are going to be used need to be moved onto the DOS partition.
The files that are required in the DOS directory called C:LINUX
arethe following:
LINUXDSK.IMG
The disk image that will become the root device.LINUXSWP.IMG
The swap space.
3.7 Creating the Boot Floppy
The boot floppy that is used is just a normal DOS format bootable floppy.
This is created using format a: /s
from DOS.
Onto this disk you will need to create an AUTOEXEC.BAT
file (as below)and copy the kernel, compressed initial ramdisk and LOADLIN
executable.
AUTOEXEC.BAT
The DOS automatically executed batch file.LOADLIN.EXE
TheLOADLIN
program executable.ZIMAGE
The Linux kernel.INITRDGZ.IMG
The compressed initial ramdisk image.
The AUTOEXEC.BAT
file should contain just one line as below.
This specifies the kernel image to use, the initial ramdisk image, the rootdevice after the initial ramdisk has finished and that the root partition is tobe mounted read-only.
To boot from this new root device all that is required is that the floppy diskprepared as described above is inserted for the PC to boot from.
You will see the following sequence of events.
- DOS boots
- AUTOEXEC.BAT starts
- LOADLIN is run
- The Linux kernel is copied into memory
- The initial ramdisk is copied into memory
- The Linux kernel is started running
- The
/linuxrc
file on the initial ramdisk is run - The DOS partition is mounted and the root and swap devices set up
- The boot sequence continues from the loopback device
When this is complete you can remove the boot floppy and use the Linux system.
4.1 Possible Problems With Solutions
There are a number of stages where this process could fail, I will try toexplain what they are and what to check.
DOS booting is easy to recognise by the message that it prints MS-DOSStarting ..
on the screen. If this is not seen then the floopy disk iseither not-bootable or the PC is not bootable from the floppy disk drive.
When the AUTOEXEC.BAT
file is run the commands in it should be echoedto the screen by default. In this case there is just the single line in thefile that starts LOADLIN
.
Loopback 2 1 3 X 2 3
When LOADLIN
executes it will do two very visible things, firstly itwill load the kernel into memory, secondly it will copy the ramdisk into memory.Both of these are indicated by a Loading..
message.
The kernel starts by uncompressing itself, this can give crc errors ifthe kernel image is corrupted. Then it will start running the initialisationsequence which is very verbose with diagnostic messages. Loading of the initialramdisk device is also visible during this phase.
When the /linuxrc
file is run there is no diagnostic messages, but youcan add these yourself as an aid to debugging. If this stage fails to set upthe loopback device as the root device then you may see a message that there isno root device and the kernel aborts.
The normal boot sequence of the new root device will now continue and this isquite verbose. There may be problems about the root device being mountedread-write, but the LOADLIN
command line option 'ro
' shouldstop that. Other problems that can occur are that the boot sequence is confusedabout where the root device is, this is probably due to a problem with/etc/fstab
.
When the boot sequence has completed, the remaining problem is that programs areconfused about whether the DOS partition is mounted or not. This is why it is agood idea to use the fake mount command described earlier. This makes life alot easier if you want to access the files on the DOS device.
4.2 Reference Documents
The documents that I used to create my first loopback root filesystem were:
- The Linux kernel source, in particular
init/main.c
- The Linux kernel documentation, in particular
Documentation/initrd.txt
andDocumentation/ramdisk.txt
. - The
LILO
documentation. - The
LOADLIN
documentation
Once the principle of booting a filesystem in a file on a DOS partition has beenestablished there are many other things that you can now do. Wifi signal 4 2 2 x 4.
5.1 DOS Hard-disk Only Installation
If it is possible to boot Linux from a file on a DOS harddisk by using a bootfloppy then it is obviously also possible to do it using the harddisk itself.
A configuration boot menu can be used to give the option of runningLOADLIN
from within the AUTOEXEC.BAT
. This will give a muchfaster boot sequence, but is otherwise identical.
5.2 LILO Booted Installation
Using LOADLIN
is only one option for booting a Linux kernel. There isalso LILO
that does much the same but without needing DOS.
In this case the DOS format floppy disk can be replaced by an ext2fs format one.Otherwise the details are very similar, with the kernel and the initial ramdiskbeing files on that disk.
The reason that I chose the LOADLIN
method is that the arguments thatneed to be given to LILO
are slightly more complex. Also it is moreobvious to a casual observer what the floppy disk is since it can be read underDOS.
5.3 VFAT / NTFS Installation
I have tried the NTFS method, and have had no problems with it. The NTFSfilesystem driver is not a standard kernel option in version 2.0.x, but isavailable as a patch from http://www.informatik.hu-berlin.de/~loewis/ntfs/. In version 2.2.x theNTFS driver is included as standard in the kernel.
The only changes for the VFAT or NTFS options are in the initial ramdisk, thefile /linuxrc
needs to mount a file system of type vfat or ntfs ratherthat msdos.
I know of no reason why this should not also work on a VFAT partition.
5.4 Installing Linux without Re-partitioning
The process of installing Linux on a PC from a standard distribution requiresbooting from a floppy disk and re-partitioning the disk. This stage couldinstead be accomplished by a boot floppy that creates an empty loopback deviceand swap file. This would allow the installation to proceed as normal, but itwould install into the loopback device rather than a partition.
This could be used as an alternative to a UMSDOS
installation, it wouldbe more efficient in disk usage since the minimum allocation unit in the ext2filesystem is 1kB instead of up to 32kB on a DOS partition. It can also be usedon VFAT and NTFS formatted disks which are otherwise a problem.
5.5 Booting From a Non-bootable device
This method can also be used to boot a Linux system from a device that is notnormally bootable.
- CD-Rom
- Zip Disks
- Parallel port disk drives
Obviously there are many other devices that could be used, NFS root filesystemsare already included in the kernel as an option, but the method described heremight also be used instead.