-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwicshell.sh
executable file
·126 lines (115 loc) · 2.79 KB
/
wicshell.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
#
# Authors:
# Roberto A. Foglietta <roberto.foglietta@gmail.com>
#
# SPDX-License-Identifier: MIT
#
function getloopdev() {
if [ -b "$fimg" ]; then
if [ -b ${fimg}2 ]; then
echo ${fimg}2
elif [ -b ${fimg}p2 ]; then
echo ${fimg}p2
else
echo ${fimg}
fi
return $?
fi
local a
a=$(losetup -j $fimg | cut -d: -f1)
a=$(ls -1v "$a" 2>/dev/null | tail -n1)
d=${a:+${a}p2}
test -b "$d" && echo $d && return 0
d=${a:+${a}p1}
test -b "$d" && echo $d && return 0
d=$a
test -b "$d" && echo $d && return 0
}
function umountall() {
set +e
umount -R ${1:-$rootdir}
if getloopdev >/dev/null; then
losetup -d ${bdev/p[12]/}
getloopdev | sed -e "s,\(.*\),ERROR: loop device \\1 is still busy!,"
fi
echo
}
function jumpinto() {
set -e
test -b $bdev
mkdir -p $rootdir
mount $bdev $rootdir
mkdir -p $rootdir/work
mount -o bind build/tmp/work $rootdir/work
mkdir -p $rootdir/isar-apt
mount -o bind build/tmp/deploy/isar-apt $rootdir/isar-apt
mount -t sysfs sys $rootdir/sys
mount -t proc proc $rootdir/proc
mount -t devtmpfs udev $rootdir/dev
mount -t devpts devpts $rootdir/dev/pts
mount -t cgroup2 cgroup2 $rootdir/sys/fs/cgroup
echo "chroot into $(basename $fimg)..."
echo
export debian_chroot=chroot
hostname=$(cat $rootdir/etc/hostname)
hostname=${hostname:-unknown}
sed -i "s,\(127.0.0.1.*localhost$\),\\1 $hostname $HOSTNAME," $rootdir/etc/hosts
export HOSTNAME=$hostname
if true; then echo '#!/usr/bin/env
hostname -F /etc/hostname
export HOSTNAME=$(hostname -s); export HOME=/root
source /etc/profile; source /etc/locale; export LC_ALL
'$(env | grep _proxy= | sed -e "s,\(.*\),export \\1;,")'
alias exp-last-part=/usr/share/expand-on-first-boot/expand-last-partition.sh
echo $debian_chroot hostname: $(hostname -s), current user: $(whoami)
echo; cd'
fi > $rootdir/root/.chrootrc
chroot $rootdir /bin/bash --rcfile /root/.chrootrc -ic bash
echo "chroot exiting..."
}
if [ "$(whoami)" != "root" ]; then
echo
echo "WARNING: this script should run as root, sudo!"
sudo -E $0 "$@"
exit $?
fi
if [ -e "$1" ]; then
fimg=$(readlink -e $1)
fi
cd $(dirname $0)
if [ ! -n "$1" -a ! -e "$fimg" ]; then
echo "Searching the image..."
fimg=$(ls -1 build/tmp/deploy/images/*/*.wic)
n=( $fimg )
if [ ${#n[@]} -gt 1 ]; then
echo
echo "WARNING: more than one image found, choose one:"
echo
echo "$fimg"
echo
exit 1
fi
fi
if [ ! -e "$fimg" ]; then
echo
echo "ERROR: no any image or block device ${1:+'$1' }found, abort!"
echo
exit 1
fi
rootdir=build/root
bdev="$(getloopdev)"
trap "umountall $(readlink -f $rootdir)" EXIT
set -e
echo
if [ -b "$fimg" ]; then
jumpinto $fimg
elif [ -b "$bdev" ]; then
echo "WARNIING: using a previous loop device $bdev"
echo
jumpinto $bdev
else
losetup -Pf $fimg
bdev=$(getloopdev)
jumpinto $bdev
fi