2687 |
rexy |
1 |
# Mageia (Mageia + ALCASAR) ISO builder
|
|
|
2 |
# By Aurélien DUBOIS
|
|
|
3 |
|
2672 |
rexy |
4 |
#!/bin/bash
|
|
|
5 |
|
|
|
6 |
#######################
|
|
|
7 |
###### Variables ######
|
|
|
8 |
################################################################################
|
|
|
9 |
|
|
|
10 |
# Script variables
|
|
|
11 |
CURRENT_DIR="$(readlink -f "$(dirname $0)")"
|
|
|
12 |
RESSOURCES="$CURRENT_DIR/ressources"
|
|
|
13 |
SCRIPTS_DIR="$CURRENT_DIR/scripts"
|
|
|
14 |
SCRIPTS_TMP_DIR="$CURRENT_DIR/scripts_tmp"
|
|
|
15 |
BUILDER_SCRIPT="build-image.sh"
|
|
|
16 |
LOG_FILE="${CURRENT_DIR}/mageiarBuilder.log"
|
|
|
17 |
|
|
|
18 |
|
|
|
19 |
#######################
|
|
|
20 |
###### Functions ######
|
|
|
21 |
################################################################################
|
|
|
22 |
# Print the given error message and exit 1
|
|
|
23 |
function errorExit()
|
|
|
24 |
{
|
|
|
25 |
tput bold
|
|
|
26 |
echo -e "ERROR : $1 \nAborting\n" >&2
|
|
|
27 |
echo "$1" > "$LOG_FILE"
|
|
|
28 |
tput sgr0
|
|
|
29 |
exit 1
|
|
|
30 |
}
|
|
|
31 |
#-------------------------------------------------------------------------------
|
|
|
32 |
function printBold()
|
|
|
33 |
{
|
|
|
34 |
tput bold 2>/dev/null
|
|
|
35 |
echo $1
|
|
|
36 |
tput sgr0 2>/dev/null
|
|
|
37 |
}
|
|
|
38 |
#######################
|
|
|
39 |
###### Main ######
|
|
|
40 |
################################################################################
|
|
|
41 |
|
|
|
42 |
# Check root
|
|
|
43 |
[[ $(whoami) != 'root' ]] && errorExit 'Please run as root'
|
|
|
44 |
|
|
|
45 |
# Check options
|
|
|
46 |
while getopts "a:h" option; do
|
|
|
47 |
case $option in
|
|
|
48 |
a) # Alcasar image
|
|
|
49 |
alcasarTar="$(echo $OPTARG | rev | cut -d '/' -f 1 | rev)"
|
|
|
50 |
[[ ! -f "$OPTARG" ]] && errorExit "could not find $OPTARG"
|
|
|
51 |
# Copy the image in the ressource directory to be accessible from the docker
|
|
|
52 |
printBold "Copying $OPTARG" in $RESSOURCES
|
|
|
53 |
rsync -usP "$OPTARG" "$RESSOURCES/$alcasarTar" || \
|
|
|
54 |
errorExit "could not copy $OPTARG to $RESSOURCES"
|
|
|
55 |
|
|
|
56 |
;;
|
|
|
57 |
*)
|
|
|
58 |
echo "Usage : buildMageiar [-a alcasar.tar.gz]"
|
|
|
59 |
exit 1
|
|
|
60 |
;;
|
|
|
61 |
esac
|
|
|
62 |
done
|
|
|
63 |
|
|
|
64 |
# Check if auto_inst.cfg.pl is present
|
|
|
65 |
[[ ! -f "$RESSOURCES/auto_inst.cfg.pl_template" ]] && errorExit "No auto_inst.cfg.pl_template file in $RESSOURCES"
|
|
|
66 |
|
|
|
67 |
#-------------------------------------------------------------------------------
|
|
|
68 |
# If needed, download Alcasar image
|
|
|
69 |
#-------------------------------------------------------------------------------
|
|
|
70 |
|
|
|
71 |
if [[ -z $alcasarTar ]]; then
|
|
|
72 |
latestAlcasarVersion="-$(dig version.alcasar.net txt | \
|
|
|
73 |
grep "ANSWER SECTION" -A 1 | \
|
|
|
74 |
grep version | cut -d '"' -f2 || \
|
|
|
75 |
errorExit "could not retrieve Alcasar latest version")"
|
|
|
76 |
alcasarTar="alcasar$latestAlcasarVersion.tar.gz"
|
|
|
77 |
|
|
|
78 |
printBold "Downloading Alcasar image"
|
|
|
79 |
wget -O "$RESSOURCES/$alcasarTar" "ftp.alcasar.net:/pub/stable/$alcasarTar" || errorExit 'could not download Alcasar'
|
|
|
80 |
fi
|
|
|
81 |
|
|
|
82 |
#
|
|
|
83 |
# If Mageia image isn't provided here, it will be downloaded in the docker.
|
|
|
84 |
# This is because the version has to match the one in the docker
|
|
|
85 |
#
|
|
|
86 |
|
|
|
87 |
#-------------------------------------------------------------------------------
|
|
|
88 |
# Run docker image
|
|
|
89 |
#-------------------------------------------------------------------------------
|
|
|
90 |
systemctl start docker || errorExit 'could not start docker service'
|
|
|
91 |
|
|
|
92 |
# Copy script folder to temporary one
|
|
|
93 |
cp -r "$SCRIPTS_DIR" "$SCRIPTS_TMP_DIR"
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
printBold "Running mageia docker"
|
|
|
97 |
docker run --privileged --rm --volume "$RESSOURCES":/var/iso --volume "$SCRIPTS_TMP_DIR":/root/ -it mageia "/root/$BUILDER_SCRIPT" "$alcasarTar"
|
|
|
98 |
|
|
|
99 |
[[ -d "$SCRIPTS_TMP_DIR" ]] && rm -Rf "$SCRIPTS_TMP_DIR" 2>/dev/null
|
|
|
100 |
|
|
|
101 |
# Retrieve created iso image
|
|
|
102 |
mv "$RESSOURCES"/Mageia*Alcasar* "$CURRENT_DIR" 2>/dev/null
|
|
|
103 |
|
|
|
104 |
# Give it to SUDO_USER if exists
|
|
|
105 |
[[ ! -z $SUDO_USER ]] && chown -R $SUDO_USER "$CURRENT_DIR"/* 2>/dev/null
|
|
|
106 |
|
|
|
107 |
#-------------------------------------------------------------------------------
|
|
|
108 |
# Stopping
|
|
|
109 |
#-------------------------------------------------------------------------------
|
|
|
110 |
read -p 'Stop docker service ? [Y/n] : ' ynAnswer
|
|
|
111 |
[[ ! $ynAnswer = [nN] ]] && systemctl stop docker
|
|
|
112 |
#-------------------------------------------------------------------------------
|