[CentOS] copy old CF image to new CF device

Wed Dec 8 15:30:39 UTC 2010
Jerry Geis <geisj at pagestation.com>

I am using an 8G CF card. (I was using dd to duplicate the card with 
different size cards I was advised not to).

So I wrote the script below which basically:
1) runs fdisk to setup the device
2) makes the ext3 file system and the swap
3) mounts my old image "/" filesystem and the new partition
4) tar and untar the directories
5) run grub on it
6) set label for "/"

After I run all the above I get:

EXT3-fs error ext3_find_entry reading directory #129586 offset 0

then a couple lines later it remounts the root file system read only.

What have I missed in successfully make the CF card based on the old image?

Jerry
---------

DEVICE=/dev/sde
PARTITION_1=/dev/sde1
PARTITION_2=/dev/sde2

fdisk $DEVICE << EOF
o
n
p
1

+7000M
n
p
2


t
2
82
a
1
w
EOF

echo ""
fdisk -l $DEVICE

echo ""
mke2fs -j $PARTITION_1
mkswap    $PARTITION_2

# mount the first partition in the image file
mount -t ext3 -o loop,offset=32256 ebox4300.centos.5.5.cf.img /mnt/from_image
mount -t ext3                      $PARTITION_1                                  /mnt/to_image

PUSH_PWD=`pwd`

# Directories to copy
cd /mnt/from_image
for DIRECTORY in bin boot dev etc home install lib misc net opt root sbin selinux srv tftpboot tmp usr var
do
	tar -cf - $DIRECTORY | (cd /mnt/to_image; tar -xpvf - )
done

# Skip these directories above and just make them for mounting
mkdir /mnt/to_image/proc
mkdir /mnt/to_image/sys

cd $PUSH_PWD

umount /mnt/from_image
umount /mnt/to_image

grub << EOF
device (hd0) $DEVICE
root (hd0,0)
setup (hd0)
quit
EOF

e2label   $PARTITION_1 "/"
mkswap -L "SWAP-hda2" $PARTITION_2