11 EXIT_COMMAND_NOT_FOUND
=127
21 IMAGE_RELEASE
=${FALSE}
24 MOUNTED_DIRS
="/proc" "/sys" "dev/pts" "dev/shm" "dev" "run" "tmp" ""
32 echo "[${level}] ${message}"
39 log DEBUG
"Running command: ${cmd}"
49 log DEBUG
"Returned with code '${ret}'"
59 for mounted_dir
in ${MOUNTED_DIRS}
62 if is_mounted
"${IMAGE_MOUNT_DIR}${mounted_dir}"; then
63 umount
"${IMAGE_MOUNT_DIR}${mounted_dir}"
65 log DEBUG
"${IMAGE_MOUNT_DIR}${mounted_dir} is not mounted"
70 # Remove partition from the kernel table.
73 # Remove loopback device
78 # Cleanup Config file for the local repo
79 if [ -f "${LOCAL_REPO_FILE}" ]; then
80 rm -f "${LOCAL_REPO_FILE}"
86 local mounted_dir
=${1}
88 if [ ! -d ${mounted_dir} ]; then
89 log ERROR
"Is not a directory ${mounted_dir}"
92 mountpoint
${mounted_dir}
97 #Check that nothing is mounted in working dir
99 for mounted_dir
in ${MOUNTED_DIRS}
102 if is_mounted
"${IMAGE_MOUNT_DIR}${mounted_dir}"; then
103 log ERROR
"${IMAGE_MOUNT_DIR}${mounted_dir} is still mounted"
106 log DEBUG
"${IMAGE_MOUNT_DIR}${mounted_dir} is not mounted"
112 if [ -d "${WORKING_DIR}" ]; then
113 rm -dfR "${WORKING_DIR}"
122 generate_image_filename
() {
127 echo "${distro}-${version}-${arch}"
130 check_for_pakfire
() {
134 # Check that pakfire-server binary is available
135 # Check that pakfire binary is available
137 # Check that repo files are installed. (pakfire need to know which repos exist)
138 local repo_dir
="/etc/pakfire/repos"
140 if [ ! -d "${repo_dir}" ]; then
141 log ERROR
"Could not find respository directory ${repo_dir}"
145 return ${return_value}
149 local compression
=${1}
150 local image_file
=${2}
153 log debug
"Compressing ${image_file} with ${compression}"
155 case "${compression}" in
157 # Check that the file does not exist yet
158 if [ -f "${image_file}.xz" ]; then
159 log ERROR
"Failed to compress the image. The file already exists"
162 cmd xz
"-${level}" "${image_file}"
165 # Check that the file does not exist yet
166 if [ -f "${image_file}.zip" ]; then
167 log ERROR
"Failed to compress the image. The file already exists"
171 cmd
zip "-${level}" "${image_file}.zip" "${image_file}"
172 # Remove the file which we compressed+
173 rm -f "${image_file}"
179 reset_root_password
() {
182 cp -avf ${root_dir}/etc
/passwd
${root_dir}/etc
/passwd.orig
184 # Drop root password.
185 sed -e "s/^\(root:\)[^:]*:/\1:/" ${root_dir}/etc
/passwd.orig
> ${root_dir}/etc
/passwd
187 # Remove passwd backup file.
188 rm -rvf ${root_dir}/etc
/passwd.orig
192 # Dir where the repos should be located
203 # Clone git repositories.
204 for repo
in ${repos}; do
210 # This function is used to publish the produced images
214 # The image we created usually a img. file
215 local image_base_file
=${2}
217 local image_name_final
="$(generate_image_filename "${DISTRO}" "${VERSION}" "${ARCH}")"
220 # Do these steps for every image format we like to publish
221 for image_type
in ${IMAGE_TYPES_PUBLISH}; do
222 # Convert images to the type specified in IMAGE_TYPES_PUBLISH
223 convert_image
"${image_type}" "${image_base_file}" "${image_name_final}"
226 if [[ ${IMAGE_RELEASE} -eq ${TRUE} ]]; then
227 local compression_type
228 local compression_level
231 # Get compressioon type
232 compression_type
="$(get_compression_type ${image_type})"
235 compress_image
"${compression_type}" "${image_name_final}.${image_type}" ${compression_level}
237 # Move images to this path
238 mv -f "${image_name_final}.${image_type}.${compression_type}" ${path}
240 # point the latest links to these images
241 ln -s -f "${path}/${image_name_final}.${image_type}.${compression_type}" \
242 "${path}/$(generate_image_filename "${DISTRO}" "latest" "${ARCH}").${image_type}.${compression_type}"
245 # Move images to this path
246 mv -f "${image_name_final}.${image_type}" ${path}
257 if [[ ${type} = "img
" ]]; then
258 # We do not need to convert the image here but we need to rename
259 mv -f ${from} ${to}.${type}
263 if [[ ${type} = "qcow2
" ]]; then
264 local command="qemu-img convert
-c -O ${type} ${from} ${to}.${type}"
266 local command="qemu-img convert
-O ${type} ${from} ${to}.${type}"
272 get_compression_type() {
273 local image_type=${1}
275 case "${image_type}" in
277 # These types can be used only under Unix so we use xz as compression
281 # These types can be also under Windows so we use zip as compression
288 check_for_free_space() {
291 local space_in_path=0
293 space_in_path=$(df -h -B MB --output=avail ${path} | tail -n 1)
294 space_in_path=${space_in_path%MB}
295 log debug ${space_in_path}
298 if [ ${space_in_path} -lt ${space} ]; then
299 log error "Not enough free space available under
${path}"
300 log error "Free space is
${space_in_path}MB but we need
at least
${space}MB
"
306 while [ $# -gt 0 ]; do
309 IMAGE_RELEASE=${TRUE}
313 error "Invalid argument
: ${1}"
314 return ${EXIT_CONF_ERROR}
322 local chroot_dir="${1}"
328 if [ ! -d ${chroot_dir} ]; then
329 log ERROR "Cannot chroot
in a non directory
${chroot_dir}"
332 mount proc "${chroot_dir}/proc
" -t proc -o nosuid,noexec,nodev
333 mount sys "${chroot_dir}/sys
" -t sysfs -o nosuid,noexec,nodev,ro
334 mount udev "${chroot_dir}/dev
" -t devtmpfs -o mode=0755,nosuid
335 mount devpts "${chroot_dir}/dev
/pts
" -t devpts -o mode=0620,gid=5,nosuid,noexec
336 mount shm "${chroot_dir}/dev
/shm
" -t tmpfs -o mode=1777,nosuid,nodev
337 mount /run "${chroot_dir}/run
" --bind
338 mount tmp "${chroot_dir}/tmp
" -t tmpfs -o mode=1777,strictatime,nodev,nosuid
342 cmd chroot "${chroot_dir}" "${command}"
345 umount "${chroot_dir}/proc
"
346 umount "${chroot_dir}/sys
"
347 umount "${chroot_dir}/dev
/pts
"
348 umount "${chroot_dir}/dev
/shm
"
349 umount "${chroot_dir}/dev
"
350 umount "${chroot_dir}/run
"
351 umount "${chroot_dir}/tmp
"