From c23da3d37e4d59322830ebee8a0f5ab919a9843b Mon Sep 17 00:00:00 2001
From: Scott Garman <scott.a.garman@intel.com>
Date: Fri, 13 Aug 2010 11:12:33 -0700
Subject: poky-extract-sdk: script for extracting sdk images using pseudo

This script automates the creation of a rootfs area using pseudo so
it can be used by a QEMU nfsroot boot.

Signed-off-by: Scott Garman <scott.a.garman@intel.com>
---
 meta/packages/qemu/qemu-helper-nativesdk_1.0.bb |  1 +
 scripts/poky-extract-sdk                        | 95 +++++++++++++++++++++++++
 2 files changed, 96 insertions(+)
 create mode 100755 scripts/poky-extract-sdk

diff --git a/meta/packages/qemu/qemu-helper-nativesdk_1.0.bb b/meta/packages/qemu/qemu-helper-nativesdk_1.0.bb
index 6b4979d79..7ffb1a27f 100644
--- a/meta/packages/qemu/qemu-helper-nativesdk_1.0.bb
+++ b/meta/packages/qemu/qemu-helper-nativesdk_1.0.bb
@@ -11,6 +11,7 @@ SRC_URI = "file://${POKYBASE}/scripts/poky-qemu \
            file://${POKYBASE}/scripts/poky-qemu-ifup \
            file://${POKYBASE}/scripts/poky-qemu-ifdown \
            file://${POKYBASE}/scripts/poky-find-native-sysroot \
+           file://${POKYBASE}/scripts/poky-extract-sdk \
            file://tunctl.c \
            file://raw2flash.c \
           "
diff --git a/scripts/poky-extract-sdk b/scripts/poky-extract-sdk
new file mode 100755
index 000000000..70bc44b79
--- /dev/null
+++ b/scripts/poky-extract-sdk
@@ -0,0 +1,95 @@
+#!/bin/bash
+#
+# This utility extracts an SDK image tarball using pseudo, and stores
+# the pseudo database in var/pseudo within the rootfs. If you want to
+# boot QEMU using an nfsroot, you *must* use this script to create the
+# rootfs to ensure it is done correctly with pseudo.
+#
+# Copyright (c) 2010 Intel Corp.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+function usage() {
+	echo "Usage: $0 <poky-sdk-tarball> <extract-dir>"
+}
+
+if [ $# -ne 2 ]; then
+	usage
+	exit 1
+fi
+
+SYSROOT_SETUP_SCRIPT=`which poky-find-native-sysroot`
+if [ -z "$SYSROOT_SETUP_SCRIPT" ]; then
+    echo "Error: Unable to find the poky-find-native-sysroot script"
+    echo "Did you forget to source your Poky environment script?"
+    exit 1
+fi
+. $SYSROOT_SETUP_SCRIPT
+PSEUDO_OPTS="-P $NATIVE_SYSROOT_DIR/usr"
+
+ROOTFS_TARBALL=$1
+SDK_ROOTFS_DIR=$2
+
+if [ ! -e "$ROOTFS_TARBALL" ]; then
+   	echo "Error: sdk tarball '$ROOTFS_TARBALL' does not exist"
+   	usage
+   	exit 1
+fi
+
+TAR_OPTS=""
+if [[ "$ROOTFS_TARBALL" =~ tar\.bz2$ ]]; then
+	TAR_OPTS="-xjf"
+fi
+if [[ "$ROOTFS_TARBALL" =~ tar\.gz$ ]]; then
+	TAR_OPTS="-xzf"
+fi
+if [[ "$ROOTFS_TARBALL" =~ \.tar$ ]]; then
+	TAR_OPTS="-xf"
+fi
+if [ -z "$TAR_OPTS" ]; then
+	echo "Error: Unable to determine sdk tarball format"
+	echo "Accepted types: .tar / .tar.gz / .tar.bz2"
+	exit 1
+fi
+
+if [ ! -d "$SDK_ROOTFS_DIR" ]; then
+	echo "Creating directory $SDK_ROOTFS_DIR"
+	mkdir -p "$SDK_ROOTFS_DIR"
+fi
+
+if [ -e "$SDK_ROOTFS_DIR/var/pseudo" ]; then
+	echo "Error: $SDK_ROOTFS_DIR/var/pseudo already exists!"
+	echo "Please delete the entire rootfs tree manually if this is really what you want"
+	exit 1
+fi
+
+mkdir -p "$SDK_ROOTFS_DIR/var/pseudo"
+touch "$SDK_ROOTFS_DIR/var/pseudo/pseudo.pid"
+PSEUDO_LOCALSTATEDIR="$SDK_ROOTFS_DIR/var/pseudo"
+export PSEUDO_LOCALSTATEDIR
+
+echo "Extracting rootfs tarball using pseudo..."
+echo "$PSEUDO $PSEUDO_OPTS tar -C \"$SDK_ROOTFS_DIR\" $TAR_OPTS \"$ROOTFS_TARBALL\""
+$PSEUDO $PSEUDO_OPTS tar -C "$SDK_ROOTFS_DIR" $TAR_OPTS "$ROOTFS_TARBALL"
+
+DIRCHECK=`ls -l "$SDK_ROOTFS_DIR" | wc -l`
+if [ "$DIRCHECK" -lt 5 ]; then
+	echo "Warning: I don't see many files in $SDK_ROOTFS_DIR"
+	echo "Please double-check the extraction worked as intended"
+	exit 0
+fi
+
+echo "SDK image successfully extracted to $SDK_ROOTFS_DIR"
+
+exit 0
-- 
cgit v1.2.3