forked from ubuntu-si/ubuntu-si
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ubuntu-defaults-chroot
executable file
·104 lines (85 loc) · 2.5 KB
/
ubuntu-defaults-chroot
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
#!/bin/sh
# Automatic chrooting in a ubuntu-defaults-image tree
#
# Authors: Paolo Sammicheli <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
set -e
# Decomment for enabling debug
# set -x
help () {
cat >&2 <<EOF
Usage: $0 [--help]
It sets up a proper chroot enviroment, start it and clean it after the operations.
It must be invoked from a directory containing files made with
ubuntu-default-image. More infos: https://wiki.ubuntu.com/ItalianCD/Tools
Options:
--help : shown this help
EOF
}
chroot_file () {
$SUDO cat <<EOF > ./start-chroot
#!/bin/sh
echo "setting up chroot enviroment..."
mount none -t proc /proc
mount none -t sysfs /sys
mount none -t devpts /dev/pts
export HOME=/root
export LC_ALL=C
bash
echo "cleaning up chroot enviroment..."
apt-get clean
rm -rf /tmp/*
> /etc/resolv.conf
> /etc/hosts
umount -lf /proc
umount -lf /sys
umount -lf /dev/pts
exit
EOF
}
jump_in () {
# checking if exist .stage directory, otherwise exit with error
if [ ! -d .stage -o ! -d chroot ]; then
echo "\nERROR: .stage or chroot directories not found. \nYou must run this sommand from a directory containing files mage with ubuntu-defaults-image"
else
echo "setting up enviroment"
$SUDO cp /etc/hosts chroot/etc/
$SUDO cp /etc/resolv.conf chroot/etc/resolv.conf
# Creating the chrooted start script
chroot_file
$SUDO mv start-chroot chroot/usr/bin/
$SUDO chmod +x chroot/usr/bin/start-chroot
# starting the chroot enviroment
echo "entering in the chroot enviroment. Use \"exit\" for ending the session"
$SUDO chroot chroot usr/bin/start-chroot
# deleting chrooted start script
echo "performing cleaning..."
$SUDO rm chroot/usr/bin/start-chroot
fi
}
#####################
# Starting main body
# Checking sudo
if [ "$(id -u)" = 0 ]; then
SUDO=env
else
SUDO=sudo
fi
# Checking parameters. If so printing help
if [ -z $1 ]; then
jump_in
else
help
fi