summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernard Ogden <bernie.ogden@linaro.org>2016-01-19 10:47:34 +0000
committerBernard Ogden <bernie.ogden@linaro.org>2016-01-26 09:46:17 +0000
commitb7a7570c0b6e26d6ad35f31858b2dd4cd11629ec (patch)
tree14a46b70639edc6f79fc2733add30400aa4b7364
Initial commit of http://people.linaro.org/~fathi.boudra/lb2img.sh
Change-Id: I62d1b15b1750b7acd571613fda8069ea3da8c0b2
-rw-r--r--lb2img.sh82
1 files changed, 82 insertions, 0 deletions
diff --git a/lb2img.sh b/lb2img.sh
new file mode 100644
index 0000000..e583a1d
--- /dev/null
+++ b/lb2img.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+# (C) 2013-2015 Fathi Boudra <fathi.boudra@linaro.org>
+
+# Convert tarball (live-build or other) to raw image
+
+PROGNAME=$(basename $0)
+TARGZFILE=
+IMGFILE=
+
+usage() {
+ echo "usage: ${PROGNAME} <targzfile> <imgfile>"
+ echo "e.g. ${PROGNAME} ${TARGZFILE} ${IMGFILE}"
+ exit 1
+}
+
+TARGZFILE=$1
+IMGFILE=$2
+
+# we must provide parameters
+[ -n "${TARGZFILE}" ] || usage
+[ -n "${IMGFILE}" ] || usage
+
+# we must be root
+[ $(whoami) = "root" ] || { echo "E: You must be root" && exit 1; }
+
+# we must have few tools
+MKFS=$(which mkfs.ext4) || { echo "E: You must have mkfs.ext4" && exit 1; }
+TUNE2FS=$(which tune2fs) || { echo "E: You must have tune2fs" && exit 1; }
+QEMUIMG=$(which qemu-img) || { echo "E: You must have qemu-img" && exit 1; }
+
+${QEMUIMG} create -f raw ${IMGFILE} 1G
+DEVICE=$(losetup --find --show ${IMGFILE})
+
+echo "I: Create filesystem"
+${MKFS} -L cirros-rootfs -O ^has_journal ${DEVICE}
+
+echo "I: Tune filesystem"
+${TUNE2FS} -c 0 -i 0 ${DEVICE}
+
+echo "I: Mount device on local filesystem"
+MOUNTDIR=$(mktemp -d /tmp/${PROGNAME}.XXXXXX)
+mount ${DEVICE} ${MOUNTDIR}
+
+[ $(tar -tf ${TARGZFILE} | head -n 1) = "binary/" ] && TAR_EXTRA_OPTS="--strip-components=1"
+tar -zxf ${TARGZFILE} -C ${MOUNTDIR} --numeric-owner ${TAR_EXTRA_OPTS}
+
+mount -t proc proc ${MOUNTDIR}/proc
+
+cat > ${MOUNTDIR}/runme.sh << EOF
+echo "nameserver 8.8.8.8" > /etc/resolv.conf
+apt-get update
+apt-get install --no-install-recommends -y linux-image-generic openssh-server
+apt-get clean
+rm -f /var/lib/apt/lists/*
+rm -f /etc/ssh/ssh_host_*_key*
+adduser --gecos cirros --disabled-login cirros
+echo "cirros:cubswin:)" | chpasswd
+EOF
+chroot ${MOUNTDIR} /bin/bash -x runme.sh
+rm -f ${MOUNTDIR}/runme.sh
+
+cp -a ${MOUNTDIR}/boot/vmlinuz* .
+cp -a ${MOUNTDIR}/boot/initrd.img* .
+
+head -n -1 ${MOUNTDIR}/etc/rc.local > rc.local.tmp
+cat >> rc.local.tmp << EOF
+# Generate the SSH keys if non-existent
+test -f /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server
+
+exit 0
+EOF
+mv rc.local.tmp ${MOUNTDIR}/etc/rc.local
+
+umount -l ${MOUNTDIR}/proc || true
+umount ${DEVICE}
+rm -rf ${MOUNTDIR}
+losetup --detach ${DEVICE}
+
+gzip -9 ${IMGFILE}
+
+echo "I: Done"