summaryrefslogtreecommitdiffstats
path: root/server/export_target
blob: f177bbfdc18df82dff72f9acd5de1cc1ec17ce65 (plain) (blame)
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
#!/bin/bash
# Copyright (c) 2012 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your feedback to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org
#
# Server side script to generate stage3.1,2 initial ramfses for OpenSLX Linux
# stateless clients
#############################################################################

#where we are
MODE_DIR="${ROOT_DIR}/server"

#files generated by this script land in boot
SERVER_BOOT_DIR="${MODE_DIR}/boot/${REMOTE_IP}"

#builds from remote server
SERVER_BUILD_DIR="${MODE_DIR}/local_builds/${REMOTE_IP}"

#stage32 sqfs directory 
STAGE32_SQFS_DIR="${SERVER_BOOT_DIR}/stage32_sqfs/mnt"

# initial checks
initial_checks() {

	local TOOL_STR="$TOOL_STR initial_checks:"

	#check for required tools
        for BIN in squashfs-tools
        do
                if dpkg -l "${BIN}" 2&>/dev/null; then 
                	pinfo "Installing ${BIN}..."
			apt-get -y install ${BIN}
		fi
        done

	[ ! -d "${SERVER_BOOT_DIR}" ] && mkdir -p "${SERVER_BOOT_DIR}"
}

copy_kernel() {
	#copy kernel to boot directory
	pinfo "Copying kernel from ${REMOTE_IP} to ${SERVER_BOOT_DIR}/kernel/"
	if [ -d ${SERVER_BUILD_DIR}/kernel ]; then
		cd ${SERVER_BUILD_DIR}
		tarcopy kernel ${SERVER_BOOT_DIR}
		cd - &> /dev/null
	fi
}

sync_remote() {
	[[ "${#SERVER_BUILD_DIR}" -lt "10" ]] && perror "Safety check failed: SERVER_BUILD_DIR is less than 10 chars. ($SERVER_BUILD_DIR)"
	pinfo "Synching '$REMOTE_IP:export/build' to './server/local_builds'..."
	#TODO setup link to remote build directory, later this directory will be rsynced or exported to this server...
	
	mkdir -p "${SERVER_BUILD_DIR}" || perror "Could not create directory for local copy of remote system ($SERVER_BUILD_DIR)"
	rsync -a --numeric-ids --delete -v -e "ssh -oStrictHostKeyChecking=no" "root@$REMOTE_IP:/export/build/*" "$SERVER_BUILD_DIR" || perror "rsync from 'root@$REMOTE_IP:/export/build' to '$SERVER_BUILD_DIR' failed."
}

generate_stage32() {
	local TOOL_STR="${TOOL_STR} generate_stage32:"
	[ ! -d "${STAGE32_SQFS_DIR}" ] && mkdir -p "${STAGE32_SQFS_DIR}"
	[ -e "${STAGE32_SQFS_DIR}/${TARGET}.sqfs" ] && rm "${STAGE32_SQFS_DIR}/${TARGET}.sqfs"
	pinfo "Writing '${TARGET}.sqfs' to '${STAGE32_SQFS_DIR}/${TARGET}.sqfs'"
	mksquashfs "${SERVER_BUILD_DIR}/${TARGET}" "${STAGE32_SQFS_DIR}/${TARGET}.sqfs" -comp xz -b 1M -no-recovery >&6 || perror "mksquashfs failed ($?)."
	generate_initramfs "${SERVER_BOOT_DIR}/stage32_sqfs" "./mnt/${TARGET}.sqfs" "${SERVER_BOOT_DIR}/initramfs-${TARGET}"
	# cleanup
	[ -d "${SERVER_BOOT_DIR}/stage32_sqfs" ] && rm -rf ${SERVER_BOOT_DIR}/stage32_sqfs
}

generate_stage31() {
	local TOOL_STR="${TOOL_STR} generate_stage31:"
	pinfo "Writing 'initramfs-${TARGET}' to '${SERVER_BOOT_DIR}'"
	generate_initramfs "${SERVER_BUILD_DIR}/${TARGET}" "." "${SERVER_BOOT_DIR}/initramfs-${TARGET}" 
}

generate_addons() {
	local TOOL_STR="${TOOL_STR} generate_addons:"
	pinfo "Writing '${TARGET}.sqfs' to '${SERVER_BOOT_DIR}/${TARGET}.sqfs'"
	[ -e "${SERVER_BOOT_DIR}/${TARGET}.sqfs" ] && rm "${SERVER_BOOT_DIR}/${TARGET}.sqfs"
	mksquashfs "${SERVER_BUILD_DIR}/${TARGET}" "${SERVER_BOOT_DIR}/${TARGET}.sqfs" -comp xz -b 1M -no-recovery >&6 || perror "mksquashfs failed ($?)."
}

export_target() {

initial_checks
copy_kernel

TARGET=$1
[ -d ${SERVER_BUILD_DIR}/${TARGET} ] || perror "Given target directory does not exist: ${SERVER_BUILD_DIR}/${TARGET}"

case "$2" in
	stage31)
		generate_stage31
		;;
	stage32)
		generate_stage32
		;;
	addons)
		generate_addons
		;;
esac	

}

clean_target() {
	TARGET=$1

	local TOOL_STR="$TOOL_STR clean_target"
	pinfo "Cleaning '${SERVER_BUILD_DIR}/${TARGET}'..."
	[ -d "${SERVER_BUILD_DIR}/${TARGET}" -a ! -h "${SERVER_BUILD_DIR}" ] && { rm -rf "${SERVER_BUILD_DIR}/${TARGET}" || perror "rm -rf failed."; }
	pinfo "Cleaning '${SERVER_BOOT_DIR}/kernel'..."
	[ -e "${SERVER_BOOT_DIR}/kernel" ] && { rm "${SERVER_BOOT_DIR}/kernel" || perror "rm failed."; }
	pinfo "Cleaning '${SERVER_BOOT_DIR}/initramfs-${TARGET}'..."
	[ -e "${SERVER_BOOT_DIR}/initramfs-${TARGET}" ] && { rm "${SERVER_BOOT_DIR}/initramfs-${TARGET}" || perror "rm failed."; }
	pinfo "Cleaning '${SERVER_BOOT_DIR}/${TARGET}.sqfs'..."
	[ -e "${SERVER_BOOT_DIR}/${TARGET}.sqfs" ] && { rm "${SERVER_BOOT_DIR}/${TARGET}.sqfs" || perror "rm failed."; }
	pinfo "Cleaning '${SERVER_BOOT_DIR}/stage32_dqfs/mnt/${TARGET}.sqfs'..."
	[ -e "${SERVER_BOOT_DIR}/stage32_sqfs/mnt/${TARGET}.sqfs" ] && { rm "${SERVER_BOOT_DIR}/stage32_sqfs/mnt/${TARGET}.sqfs" || perror "rm failed."; }
}