Fix how mkarchiso makes usb image

The current implementation in how partition is created for ext2 img
it looks a bit bad.

This patch makes the partition in more standarized way, respecting
cylinder alignement:
* The size of resulting image will be in cylinder multiple ~8MB.
* Use fdisk instead of sfdisk (sfdisk write some bad information)
* Make the result image in one pass, instead of concatenating.

Also the advantage is that with this can add another partitions
without any issues in the usb-flash-drive with this .img.

For example of current situation:

qemu-system-x86_64 -hda archlinux-avr.toolchain.img -serial stdio
---------------------------------------------------------------------
[root@avr ~]# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 223 MB, 223974400 bytes
59 heads, 41 sectors/track, 180 cylinders
Units = cylinders of 2419 * 512 = 1238528 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         181      218693+  83  Linux
Partition 1 has different physical/logical beginnings (non-Linux?):
     phys=(0, 1, 1) logical=(0, 1, 23)
Partition 1 has different physical/logical endings:
     phys=(27, 58, 41) logical=(180, 49, 21)

Command (m for help): v
Partition 1 has different physical/logical beginnings (non-Linux?):
     phys=(0, 1, 1) logical=(0, 1, 23)
Partition 1 has different physical/logical endings:
     phys=(27, 58, 41) logical=(180, 49, 21)
Partition 1: previous sectors 437449 disagrees with total 67731
62 unallocated 512-byte sectors

Command (m for help):
---------------------------------------------------------------------

qemu-system-x86_64 -hda archlinux-avr.toolchain-fix.img -serial stdio
---------------------------------------------------------------------
[root@avr ~]# fdisk /dev/sda

Command (m for help): p

Disk /dev/sda: 230 MB, 230307840 bytes
255 heads, 63 sectors/track, 28 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x5c94ca4f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          28      224878+  83  Linux

Command (m for help): v
62 unallocated 512-byte sectors
---------------------------------------------------------------------

Signed-off-by: Gerardo Exequiel Pozzi <vmlinuz386@yahoo.com.ar>
This commit is contained in:
Gerardo Exequiel Pozzi 2009-10-21 00:21:58 -03:00
parent c801829c61
commit db1dde541c

View File

@ -261,39 +261,49 @@ command_iso () {
command_usb () { command_usb () {
_imgcommon _imgcommon
fsimg="${imgname}.part1" modprobe -q loop
# Calculate cylinder size in bytes
CYL_SIZE=$((255*63*512))
# First partition offset
PART_OFFSET=$((63*512))
# ext2 overhead's upper bound is 6%, empirically tested up to 1GB # ext2 overhead's upper bound is 6%, empirically tested up to 1GB
rootsize=$(du -bs "${work_dir}/iso" | cut -f1) rootsize=$(du -bs "${work_dir}/iso" | cut -f1)
imgsz=$(( (${rootsize}*106)/100/512 + 1)) # image size in sectors imgsz=$(( (${rootsize}*106)/100/${CYL_SIZE} + 1 )) # image size in cylinders
# Get next free loop device
devloop=$(losetup -f)
# create the filesystem image file # create the filesystem image file
dd if=/dev/zero of="$fsimg" bs=512 count="$imgsz" dd if=/dev/zero of="$imgname" bs="$CYL_SIZE" count="$imgsz"
# Setup a loop device, and skip the first 63 sectors
losetup "$devloop" -o "$PART_OFFSET" "$imgname"
# create a filesystem on the image # create a filesystem on the image
mke2fs -m 0 -F -L "${LABEL}" "$fsimg" mke2fs -m 0 -F -L "${LABEL}" "$devloop"
# mount the filesystem and copy data # mount the filesystem and copy data
modprobe loop
TMPDIR=$(mktemp -d archiso-usbXXXXXX) TMPDIR=$(mktemp -d archiso-usbXXXXXX)
mount -o loop "$fsimg" "$TMPDIR" mount "$devloop" "$TMPDIR"
cp -a "${work_dir}"/iso/* "$TMPDIR" cp -a "${work_dir}"/iso/* "$TMPDIR"
umount "$TMPDIR" umount -d "$TMPDIR"
rm -rf "$TMPDIR" rm -rf "$TMPDIR"
# add sectors 0-62, then glue together
dd if=/dev/zero of="${imgname}" bs=512 count=63
cat "$fsimg" >> "${imgname}"
rm "$fsimg"
# create a partition table # create a partition table
# if this looks like voodoo, it's because it is fdisk -C "$imgsz" -H 255 -S 63 "$imgname" << EOF
sfdisk -uS -f "${imgname}" << EOF n
63,$imgsz,83,* p
0,0,00 1
0,0,00
0,0,00
a
1
p
w
EOF EOF
# install grub on the image # install grub on the image