LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] where insanity meets lilo (very long)

[ILUG] where insanity meets lilo (very long)

John P. Looney john at antefacto.com
Wed Jan 2 20:52:24 GMT 2002


 There was some discussion on the blending of ramdisks, lilo and loopback
devices a while back. Thought, now that I can boast that I did it (and it
was dead easy, once I worked out the appropriate incantations), I may as
well share with the work. Very handy for making up bootable disk images,
if you ever need to do such a thing.

 First, you need an OS. It's appropriate to use RPM or some such. Or a
tarball of a running system, if you want to cheat. Put it in a big pot
(say /tmp/os_filesystem).

 Next, you need to make sure that your OS filesystem has a copy of the
important lilo files. Say;

    boot.b bzImage  map  message

 Take the required boot.b and map files from the lilo distribution. Make
sure that the "message" and the "bzImage" are what you want. The first
step now is to make a disk image. Do do this, you need to do;

# -- START 
# Make a blank 64MB file (our disk is 64MB in size);
dd if=/dev/zero of=$IMAGE_FILE bs=1024 count=63232

# Add partition structure 
echo "
x                   # expert mode
c                   # set cylinders
247
h                   # set heads
16
s                   # set sectors
32
r                   # return to normal mode
n                   # new partition
p                   # primary
1                   # number 1
                    # default start sector
+2288k              # size
n                   # new partition
p                   # primary
2                   # number 2
                    # default start sector (next free)
                    # size = rest of disk
w                   # save
" | fdisk $IMAGE_FILE 2> /dev/null

mkdir -p /mnt/image_hda1
mkdir -p /mnt/image_hda2

# Make, format & tune files to be used as templates for the image
dd if=/dev/zero of=/tmp/hda1 bs=1024 count=2288 # 2.228MiB
dd if=/dev/zero of=/tmp/hda2 bs=1024 count=60928 # 60MiB
mkfs -F -m0 /tmp/hda1 2>&1 > /dev/null
mkfs -F -m0 /tmp/hda2 2>&1 > /dev/null
tune2fs -c 0 /tmp/hda1 2>&1 > /dev/null
tune2fs -c 0 /tmp/hda2 2>&1 > /dev/null
mount -o loop /tmp/hda1 /mnt/image_hda1
mount -o loop /tmp/hda2 /mnt/image_hda2

# Now, we have the empty filesystems mounted...they should be populated
(cd /mnt/image_hda1 && tar xzf ~/boot.tar.gz)
(cd /tmp/os_filesystem && tar cf - . ) | (cd /mnt/image_hda2 && tar xf - )
umount /mnt/image_hda1
umount /mnt/image_hda2

# Put the two images into the IMAGE file, and mount them
hda1_offset_blocks="16" # The first filesystem is 16 blocks into the disk
hda2_offset_blocks="2304"
dd if=/tmp/hda1 of=$IMAGE_FILE seek=$hda1_offset_blocks bs=1024
dd if=/tmp/hda2 of=$IMAGE_FILE seek=$hda2_offset_blocks bs=1024
# -- END

 By now, you have a file, which has a partition table, and at least one
populated filesystems. Needless to say, you can do whatever you want here.
For the rest of this, I am assuming that the 2.5MB one is a /boot
partition, and has been populated with those four files from ~/boot.tar.gz

 Next, we have to setup some loopback filesystems, so that LILO can be
conned into thinking that a file on a hard disk is a bootable hard disk in
its own right;

# -- START
# Use losetup to setup some devices that are actually offsets into a 
# file on the disk, so are subsections of said file
hda2_offset_bytes="2359296" # where the second filesystem starts
hda1_offset_bytes="16384"   # where the first filesystem starts
losetup -o $hda2_offset_bytes /dev/loop6 $IMAGE_FILE
losetup -o $hda1_offset_bytes /dev/loop7 $IMAGE_FILE

# Mount the partitions in the IMAGE FILE
mount -t ext2 /dev/loop6 /mnt/image_hda2
mount -t ext2 /dev/loop7 /mnt/image_hda2/boot

# Set up a loopback device that is the complete disk image file
losetup /dev/loop5 $IMAGE_FILE
lilo  -C ~/lilo.conf.loopback
losetup -d /dev/loop5

echo "Unmounting Flash Images partitions"
umount /mnt/image_hda2/mnt
umount /mnt/image_hda2
losetup -d /dev/loop6
losetup -d /dev/loop7
# -- END

 And violà. There is one last thing. A well commented, and demented
lilo.conf;

# Demented loopback lilo.conf
# loop5 is the "whole disk". We want to install lilo into its MBR
boot=/dev/loop5
prompt=50
message=/mnt/image_hda2/boot/message
map=/mnt/image_hda2/boot/map
install=/mnt/image_hda2/boot/boot.b

# Needed, as the "whole disk" has to be described
disk = /dev/loop5
  bios=0x80
  sectors = 32
  heads = 16
  cylinders = 247
  partition=/dev/loop7
    start=32 # 32 sectors=16384 bytes on the disk we use
  
# Needed, because /boot is mounted as /dev/loop6, and LILO doesn't know
# what the partition structure for loopback devices is.
disk = /dev/loop6 
  bios=0x80
  sectors = 32
  heads = 16
  cylinders = 247

# Needed, so it knows what disk it'll be later
disk = /dev/hda
  bios=0x80
  sectors = 32
  heads = 16
  cylinders = 247

image=/mnt/image_hda2/boot/bzImage
    label=linux
    read-only
    root=/dev/hda2
# -- END

 And now, you should be able to write your $IMAGE_FILE to a disk, flash or
whatever with "dd", and it should boot. Sorted. BTW, I did contact the
lilo author about this, and he was quite helpful (and suggested the "-q"
option, which interprets the boot block, so you can see why it wrote what
it did). I can't help thinking this should be in a HOWTO or some such.

Kate, the relieved.

-- 
_______________________________________
John Looney             Chief Scientist
a n t e f a c t o     t: +353 1 8586004
www.antefacto.com     f: +353 1 8586014





More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell