-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvroot_create
executable file
·188 lines (170 loc) · 5.93 KB
/
vroot_create
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
. $(dirname $0)/vroot_common
ARCHIVE_UBUNTU=http://archive.ubuntu.com/ubuntu
ARCHIVE_DEBIAN=http://ftp.us.debian.org/debian
ARCHIVE_DEBIAN=http://archive.debian.org/debian
################################################################################
# shows the help page
################################################################################
function usage()
{
echo "
${base} [options] <vroot_name>
-h|--help prints this page
-a|--arch architecture, i386 or amd64 [default: ${VROOT_DEFAULT_ARCHITECTURE}]
-d|--distro distribution, Ubuntu or Debian [default: ${VROOT_DEFAULT_DISTRIBUTION}]
-c|--codename codename of the version [default: ${VROOT_DEFAULT_CODENAME}]
"
exit 255
}
################################################################################
# installs the debootstrap utility that we will use to bootstrap
################################################################################
function install_prerequisites()
{
local packages="debootstrap sudo"
sudo -- apt-get install --assume-yes ${packages} || true
}
################################################################################
# install the files that the chroot would need for you to use it.
################################################################################
function install_files()
{
local vroot_directory=$1
for file in /etc/passwd /etc/shadow /etc/group /etc/sudoers /etc/hosts; do
sudo -- cp -f ${file} ${vroot_directory}/etc
done
}
################################################################################
# run a commant in a chroot
################################################################################
function chroot_run()
{
local vroot_directory=$1; shift
sudo -- chroot ${vroot_directory} $@
}
################################################################################
# install a package into a chroot
################################################################################
function chroot_apt_get()
{
local vroot_directory=$1; shift
chroot_run ${vroot_directory} /usr/bin/apt-get --assume-yes $@
}
################################################################################
# create the directory where the vroot will live
################################################################################
function make_vroot_directory()
{
local vroot_directory=$1
if [ -e ${vroot_directory} ]; then
echo "vroot already exists: ${vroot_directory}"
exit 4
fi
mkdir -p ${vroot_directory}
}
################################################################################
# now run the bootstrap to install the packages
################################################################################
function bootstrap_chroot()
{
local architecture=$1
local codename=$2
local vroot_directory=$3
local archive=$4
local debootstrap_script=$5
sudo -- debootstrap --arch ${architecture} ${codename} ${vroot_directory} ${archive} ${debootstrap_script}
}
################################################################################
# install extra packages
################################################################################
function install_packages()
{
local vroot_directory=$1; shift
chroot_apt_get ${vroot_directory} update
chroot_apt_get ${vroot_directory} install $@
}
################################################################################
# gets called on error to clean up any artifacts
################################################################################
function cleanup_error()
{
local vroot_directory=$1
sudo -- rm -rf ${vroot_directory}
}
################################################################################
# main
################################################################################
archive=
debootstrap_script=
codename=${VROOT_DEFAULT_CODENAME}
distro=$(vroot_get_linux_distribution)
architecture=${VROOT_DEFAULT_ARCHITECTURE}
if [ "${distro}" = "Debian" ]; then
archive=${ARCHIVE_DEBIAN}
elif [ "${distro}" = "Ubuntu" ]; then
archive=${ARCHIVE_UBUNTU}
else
# we should never get here, if we do, /usr/bin/lsb_release is missing or
# returning something we don't understand.
echo "${base}: bad distro specified: ${distro}"
usage
fi
if [ "${architecture}" != "amd64" ] && [ "${architecture}" != "i386" ]; then
echo "${base}: bad architecture specified: ${architecture}"
usage
fi
# options may be followed by one colon to indicate they have a required argument
if ! options=$(getopt -o hd:a:c: -l help,distro:,arch:,codename: -- "$@")
then
# something went wrong, getopt will put out an error message for us
exit 1
fi
eval set -- ${options}
while [ $# -gt 0 ]; do
case $1 in
-h|--help)
usage;;
-a|--arch)
architecture="$2"
shift;;
-c|--codename)
codename="$2"
shift;;
-d|--distro)
distro="$2"
shift;;
--)
shift
break
;;
-*)
echo "${base}: unrecognized option $1" 1>&2; exit 2;;
esac
shift
done
vroot=$1
[ -z "${vroot}" ] && usage
if [ "${distro}" = "Ubuntu" ]; then
archive=${ARCHIVE_UBUNTU}
debootstrap_script=/usr/share/debootstrap/scripts/gutsy
elif [ "${distro}" = "Debian" ]; then
archive=${ARCHIVE_DEBIAN}
debootstrap_script=/usr/share/debootstrap/scripts/sid
else
echo "${base}: unknown distro: ${distro}"
usage
fi
echo "arch: ${architecture}
distro: ${distro}
codename: ${codename}
vroot: ${vroot}
"
install_prerequisites
vroot_directory=$(vroot_get_directory ${vroot})
make_vroot_directory ${vroot_directory}
trap 'cleanup_error' INT TERM ERR
bootstrap_chroot ${architecture} ${codename} ${vroot_directory} ${archive} ${debootstrap_script}
install_files ${vroot_directory}
install_packages ${vroot_directory} sudo lsb-release
echo "Done! Run \"vroot ${vroot}\" to get into the vroot."