1eb8dc403SDave Cobbley#!/bin/bash
2eb8dc403SDave Cobbley#
3eb8dc403SDave Cobbley# This utility extracts an SDK image tarball using pseudo, and stores
4eb8dc403SDave Cobbley# the pseudo database in var/pseudo within the rootfs. If you want to
5eb8dc403SDave Cobbley# boot QEMU using an nfsroot, you *must* use this script to create the
6eb8dc403SDave Cobbley# rootfs to ensure it is done correctly with pseudo.
7eb8dc403SDave Cobbley#
8eb8dc403SDave Cobbley# Copyright (c) 2010 Intel Corp.
9eb8dc403SDave Cobbley#
10c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
11eb8dc403SDave Cobbley#
12eb8dc403SDave Cobbley
13eb8dc403SDave Cobbleyfunction usage() {
14eb8dc403SDave Cobbley	echo "Usage: $0 <image-tarball> <extract-dir>"
15eb8dc403SDave Cobbley}
16eb8dc403SDave Cobbley
17eb8dc403SDave Cobbleyif [ $# -ne 2 ]; then
18eb8dc403SDave Cobbley	usage
19eb8dc403SDave Cobbley	exit 1
20eb8dc403SDave Cobbleyfi
21eb8dc403SDave Cobbley
22eb8dc403SDave CobbleySYSROOT_SETUP_SCRIPT=`which oe-find-native-sysroot 2> /dev/null`
23eb8dc403SDave Cobbleyif [ -z "$SYSROOT_SETUP_SCRIPT" ]; then
24eb8dc403SDave Cobbley	echo "Error: Unable to find the oe-find-native-sysroot script"
25eb8dc403SDave Cobbley	echo "Did you forget to source your build system environment setup script?"
26eb8dc403SDave Cobbley	exit 1
27eb8dc403SDave Cobbleyfi
28*517393d9SAndrew Geissler. $SYSROOT_SETUP_SCRIPT qemu-helper-native
29eb8dc403SDave CobbleyPSEUDO_OPTS="-P $OECORE_NATIVE_SYSROOT/usr"
30eb8dc403SDave Cobbley
31eb8dc403SDave CobbleyROOTFS_TARBALL=$1
32eb8dc403SDave CobbleySDK_ROOTFS_DIR=$2
33eb8dc403SDave Cobbley
34eb8dc403SDave Cobbleyif [ ! -e "$ROOTFS_TARBALL" ]; then
35eb8dc403SDave Cobbley	echo "Error: sdk tarball '$ROOTFS_TARBALL' does not exist"
36eb8dc403SDave Cobbley	usage
37eb8dc403SDave Cobbley	exit 1
38eb8dc403SDave Cobbleyfi
39eb8dc403SDave Cobbley
40eb8dc403SDave Cobbley# Convert SDK_ROOTFS_DIR to a full pathname
41eb8dc403SDave Cobbleyif [[ ${SDK_ROOTFS_DIR:0:1} != "/" ]]; then
42eb8dc403SDave Cobbley	SDK_ROOTFS_DIR=$(readlink -f $(pwd)/$SDK_ROOTFS_DIR)
43eb8dc403SDave Cobbleyfi
44eb8dc403SDave Cobbley
45eb8dc403SDave CobbleyTAR_OPTS=""
461a4b7ee2SBrad Bishopif [[ "$ROOTFS_TARBALL" =~ tar\.xz$ ]]; then
471a4b7ee2SBrad Bishop	TAR_OPTS="--numeric-owner -xJf"
481a4b7ee2SBrad Bishopfi
49eb8dc403SDave Cobbleyif [[ "$ROOTFS_TARBALL" =~ tar\.bz2$ ]]; then
50eb8dc403SDave Cobbley	TAR_OPTS="--numeric-owner -xjf"
51eb8dc403SDave Cobbleyfi
52eb8dc403SDave Cobbleyif [[ "$ROOTFS_TARBALL" =~ tar\.gz$ ]]; then
53eb8dc403SDave Cobbley	TAR_OPTS="--numeric-owner -xzf"
54eb8dc403SDave Cobbleyfi
55eb8dc403SDave Cobbleyif [[ "$ROOTFS_TARBALL" =~ \.tar$ ]]; then
56eb8dc403SDave Cobbley	TAR_OPTS="--numeric-owner -xf"
57eb8dc403SDave Cobbleyfi
58eb8dc403SDave Cobbleyif [ -z "$TAR_OPTS" ]; then
59eb8dc403SDave Cobbley	echo "Error: Unable to determine sdk tarball format"
601a4b7ee2SBrad Bishop	echo "Accepted types: .tar / .tar.gz / .tar.bz2 / .tar.xz"
61eb8dc403SDave Cobbley	exit 1
62eb8dc403SDave Cobbleyfi
63eb8dc403SDave Cobbley
64eb8dc403SDave Cobbleyif [ ! -d "$SDK_ROOTFS_DIR" ]; then
65eb8dc403SDave Cobbley	echo "Creating directory $SDK_ROOTFS_DIR"
66eb8dc403SDave Cobbley	mkdir -p "$SDK_ROOTFS_DIR"
67eb8dc403SDave Cobbleyfi
68eb8dc403SDave Cobbley
69eb8dc403SDave Cobbleypseudo_state_dir="$SDK_ROOTFS_DIR/../$(basename "$SDK_ROOTFS_DIR").pseudo_state"
70eb8dc403SDave Cobbleypseudo_state_dir="$(readlink -f $pseudo_state_dir)"
71eb8dc403SDave Cobbley
7282c905dcSAndrew Geisslerdebug_image="`echo $ROOTFS_TARBALL | grep '\-dbg\.rootfs\.tar'`"
73d5ae7d90SBrad Bishop
74d5ae7d90SBrad Bishopif [ -e "$pseudo_state_dir" -a -z "$debug_image" ]; then
75eb8dc403SDave Cobbley	echo "Error: $pseudo_state_dir already exists!"
76eb8dc403SDave Cobbley	echo "Please delete the rootfs tree and pseudo directory manually"
77eb8dc403SDave Cobbley	echo "if this is really what you want."
78eb8dc403SDave Cobbley	exit 1
79eb8dc403SDave Cobbleyfi
80eb8dc403SDave Cobbley
81eb8dc403SDave Cobbleymkdir -p "$pseudo_state_dir"
82eb8dc403SDave Cobbleytouch "$pseudo_state_dir/pseudo.pid"
83eb8dc403SDave CobbleyPSEUDO_LOCALSTATEDIR="$pseudo_state_dir"
84eb8dc403SDave Cobbleyexport PSEUDO_LOCALSTATEDIR
85eb8dc403SDave Cobbley
86eb8dc403SDave Cobbleyecho "Extracting rootfs tarball using pseudo..."
87eb8dc403SDave Cobbleyecho "$PSEUDO $PSEUDO_OPTS tar -C \"$SDK_ROOTFS_DIR\" $TAR_OPTS \"$ROOTFS_TARBALL\""
88eb8dc403SDave Cobbley$PSEUDO $PSEUDO_OPTS tar -C "$SDK_ROOTFS_DIR" $TAR_OPTS "$ROOTFS_TARBALL"
89eb8dc403SDave Cobbley
90eb8dc403SDave CobbleyDIRCHECK=`ls -l "$SDK_ROOTFS_DIR" | wc -l`
91eb8dc403SDave Cobbleyif [ "$DIRCHECK" -lt 5 ]; then
92eb8dc403SDave Cobbley	echo "Warning: I don't see many files in $SDK_ROOTFS_DIR"
93eb8dc403SDave Cobbley	echo "Please double-check the extraction worked as intended"
94eb8dc403SDave Cobbley	exit 0
95eb8dc403SDave Cobbleyfi
96eb8dc403SDave Cobbley
97eb8dc403SDave Cobbleyecho "SDK image successfully extracted to $SDK_ROOTFS_DIR"
98eb8dc403SDave Cobbley
99eb8dc403SDave Cobbleyexit 0
100