Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Raspberry PiのDD用イメージファイルをNOOBSで使えるようなtar.xzにするスクリプト。
#!/bin/bash
if [ $# -ne 1 ]; then
echo "USAGE: $0 <imagefile>"
exit 1
fi
if [ ${EUID:-${UID}} != 0 ]; then
echo 'You must run as root user.'
exit 1
fi
IMGNAME=$1
WORK=`pwd`
LOOPPATH=`losetup -f`
LOOPID=`echo ${LOOPPATH} | awk -F/ '{print $3}'`
IMGFILE=`echo ${IMGNAME} | awk -F. '{print $1}'`
echo Mounting image file
losetup -f ${IMGNAME}
kpartx -a ${LOOPPATH}
mount /dev/mapper/${LOOPID}p2 /mnt
mount /dev/mapper/${LOOPID}p1 /mnt/boot
echo Creating archive [root partition]
cd /mnt
tar -cpf ${WORK}/${IMGFILE}-root.tar * --exclude=proc/* --exclude=sys/* --exclude=dev/pts/* --exclude=boot/*
echo Creating archive [boot partition]
cd /mnt/boot
tar -cpf ${WORK}/${IMGFILE}-boot.tar .
cd ${WORK}
echo Unounting image file
umount /mnt/boot
umount /mnt
kpartx -d ${LOOPPATH}
losetup -d ${LOOPPATH}
echo Modifing partition information.
tar xf ${IMGFILE}-root.tar etc/fstab
sed -i -e 's/mmcblk0p1/mmcblk0p5/g' etc/fstab cmdline.txt
sed -i -e 's/mmcblk0p2/mmcblk0p6/g' etc/fstab cmdline.txt
tar uf ${IMGFILE}-root.tar etc/fstab
tar uf ${IMGFILE}-boot.tar ./cmdline.txt
ls -lh ${IMGFILE}-*.tar
xz -e -9 ${IMGFILE}-boot.tar
xz -e -9 ${IMGFILE}-root.tar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.