User:Quozl/Remastering

From OLPC
< User:Quozl
Revision as of 06:20, 2 July 2012 by Quozl (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Remastering a .zd file without having to make a build. Useful for XO-1.75 when no ARM build machinery is on your side of a slow pipe.

The script takes as input a .zd file, extracts the disk image, mounts the two filesystems, makes changes, then converts the result back to a .zd file.

#!/bin/bash
set -evx

# the build number to use as input
n=12

BUILD=$(printf %02d $n)
BUILD_AS_PATH=$(printf %d $n)

INPUT=210${BUILD}o2.zd
IMAGE=210${BUILD}o2.img
FINAL=fs.zd

if [[ ! -e $INPUT ]]; then
    echo no build
    exit 1
fi

# convert the .zd file into a disk image
zdextract < $INPUT > $IMAGE

# build a random fill pattern for later use
dd if=/dev/urandom of=$IMAGE.fill bs=4096 count=1 2>/dev/null

# grab the offsets from the partition table
P1=$(parted -m $IMAGE "unit B print"|grep '^1'|cut -f2 -d:|sed 's/B//g')
P2=$(parted -m $IMAGE "unit B print"|grep '^2'|cut -f2 -d:|sed 's/B//g')

# connect the partitions to loopback block devices
L1=$(losetup --show --find --offset $P1 $IMAGE)
L2=$(losetup --show --find --offset $P2 $IMAGE)

# mount the two filesystems
mkdir -p /mnt/root /mnt/boot
mount $L2 /mnt/root
mount $L1 /mnt/boot
ROOT=/mnt/root/versions/run/$BUILD_AS_PATH/
USER=/mnt/root/home/olpc/

# turn off runin fscheck, it will fail once we change things
touch $ROOT/runin/soiled

# provide local kernel
# rsync -av /tmp/xo/bootpart/boot/* /mnt/boot/boot/
# rsync -av /tmp/xo/lib/modules ${ROOT}/lib/

# force initial runin
# touch $ROOT/runin/quick

# open to your key
SSH_CONFIG=/mnt/root/home/root/.ssh
mkdir --mode=g-rwx,o-rwx $SSH_CONFIG
cat << EOF > $SSH_CONFIG/authorized_keys
(put your key here)
EOF
chmod o-rwx,g-rwx $SSH_CONFIG/authorized_keys

# save some space and therefore compression and fs-update time
# rm -rf $USER/Activities/Wikipedia*
# rm -rf $USER/Activities/Scratch*
# rm -rf $USER/Library/*

# enable sshd on start
cat <<EOF > $ROOT/etc/rc.local
#!/bin/sh
touch /var/lock/subsys/local
if [[ ! -f /etc/rc.local.quozl ]]; then
    service sshd start
    chkconfig sshd on
    touch /etc/rc.local.quozl
fi
EOF
chmod +x $ROOT/etc/rc.local

# associate to local wireless
mkdir -p $USER/.sugar/default/nm/
chown -R 500:500 $USER/.sugar
cat <<EOF > $USER/.sugar/default/nm/connections.cfg
[Auto qz]
type = 802-11-wireless
ssid = qz
uuid = 757dc7687efb4e7250f4eb6697568482ae948526
autoconnect = True
timestamp = 1321510400

EOF

# dismount the filesystem
umount /mnt/boot
umount /mnt/root
rmdir /mnt/root /mnt/boot

# fill the free blocks, to ensure sparse output
fill-free-blocks -v $L1 $IMAGE.fill
fill-free-blocks -v $L2 $IMAGE.fill

# discard the loopback
until losetup -d $L1; do sleep 0.1; done
until losetup -d $L2; do sleep 0.1; done

# compress back to .zd format
zhashfs 0x20000 sha256 $IMAGE fs.zsp fs.zd