summaryrefslogtreecommitdiffstats
path: root/installer/tpl/installer.tpl.sh
blob: d5ff629cb1eef5c8ab4e3f880ca0cd3cf07515c1 (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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/bin/bash
# -----------------------------------------------------------------------------
# Copyright (c) 2011 - 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 suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# PreBoot USB stick installer
# -----------------------------------------------------------------------------

ROOT_DIR=$(dirname $(readlink -f $0))
SELF=$(readlink -f $0)
TMP_DIR="/tmp/pbstick-$$"

DEBUG=0
FORCE=0

VERSION=""
VDATE=""

SHORT_OPTS=":vfdh"
LONG_OPTS="version,force,debug,help"

print_usage() {
  echo "Usage: $(basename $SELF) [OPTIONS] DEVICE"
  echo -e "  -d  --debug   \t give more debug output"
  echo -e "  -f  --force   \t don't ask questions"
  echo -e "  -h  --help    \t print help"
  echo -e "  -v  --version \t print version information"
  echo "DEVICE is the device used by the usb stick (e.g. \"sdX\" for /dev/sdX)."
}

TEMP=`getopt -o $SHORT_OPTS --long $LONG_OPTS \
     -n "$0" -- "$@"`

if [ $? != 0 ] ; then 
  echo -e "[Error] Unknown option(s).\n"
  print_usage
  exit 1
fi

eval set -- "$TEMP"

while true ; do
        case "$1" in
                -v|--version)
                     echo  "OpenSLX PreBoot USB stick installer ($VERSION - $VDATE)."
                     exit 0
                   ;;
                -h|--help)
                     print_usage
                     exit 0
                   ;;
                -f|--force) echo "[Info]  Disable user-interaction."; FORCE=1; shift ;;
                -d|--debug) echo "[Info]  Enabled debugmode."; DEBUG=1; shift ;;
                --) shift ; break ;;
                *) echo "[Error] Internal error!" ; exit 1 ;;
        esac
done


if [ $(whoami) != "root" ]; then
  echo -e "[Error] You should be root."
  exit 1;
fi

OUT_DEV=$1
shift

if [ ! -z $1 ]; then
    echo -e "[Error] Too many parameters. \n"
    print_usage
    exit 1
fi

if [ -z $OUT_DEV ]; then
    echo -e "[Error] No output device specified.\n"
    print_usage
    exit 1
fi

if [ ! -e "/dev/$OUT_DEV" ]; then
    echo -e "[Error] There is no /dev/${OUT_DEV}."
    exit 1
fi

if [ -z $(which uuencode) ]; then
    echo -e "[Error] uuencode is missing (if you are on a debian/ubuntu system: apt-get install sharutils)"
    exit 1
fi


# unpack script payload

mkdir -p $TMP_DIR

set_quiet () {
	if [ "x$DEBUG" != "x1" ]; then
	  touch $TMP_DIR/stdout.log
	  touch $TMP_DIR/stderr.log
	  exec 6>&1 > $TMP_DIR/stdout.log
	  exec 2>  $TMP_DIR/stderr.log
	fi
}

unset_quiet () {
	if [ "x$DEBUG" != "x1" ]; then
  	  exec 1>&6 6>&-
	  exec 2>&-
	fi
}

set_quiet
match=$(grep --text --line-number '^PAYLOAD:$' $SELF | cut -d ':' -f 1)
payload_start=$((match + 1))
cd $TMP_DIR
tail -n +$payload_start $SELF | uudecode | tar -xjf -
cd $ROOT_DIR




PATH="$TMP_DIR/bin/:$PATH"

if [ "x$FORCE" != "x1" ]; then
  unset_quiet
  echo "Device \"/dev/${OUT_DEV}\" will be repartitioned and formatted."
  echo -n "Do you really want to proceed? Then type in 'yes': "
  read cont
  [ "x${cont}" != "xyes" ] && exit 1
  set_quiet
fi

for i in 1 2 3 4 5 6 7 8 9; do
   umount /dev/${OUT_DEV}$i &> /dev/null
done

# get the total size of the device
DISKSIZE=$(sfdisk -s /dev/${OUT_DEV} 2>/dev/null || echo 0)
DISKSIZE=$((${DISKSIZE}/1024))

# get the size of boot stuff and calculate the left over free space on device
KSIZE=$(ls -l ${TMP_DIR}/boot/kernel|awk '{print $5}')
ISIZE=$(ls -l ${TMP_DIR}/boot/initramfs|awk '{print $5}')
IMAGESIZE=$(((${KSIZE}+${ISIZE}+6000000)/1048576))
FREE=$((${DISKSIZE}-${IMAGESIZE}))

# create bootable vfat at the end of the device if big enough, otherwise use
# the entire device
if [ ${FREE} -ge 50 ] ; then
  sfdisk /dev/${OUT_DEV} -uM << EOF
,${FREE},L,
,,6,*
EOF
else
  NO_USRHOME=true
  sfdisk /dev/${OUT_DEV}  << EOF
,,6,*
EOF
fi

sync

# copy mbr to stick
dd bs=440 count=1 conv=notrunc if=$TMP_DIR/share/mbr.bin of=/dev/${OUT_DEV} $DEBUG_OUT

# create filesystems on newly createt partitions
if [ "x$NO_USRHOME" != "xtrue" ]; then
  mkfs.vfat -F 16 -n  openslx-stick /dev/${OUT_DEV}2
  mkfs.ext2 -L openslx-usrhome /dev/${OUT_DEV}1
else
  mkfs.vfat -F 16 -n  openslx-stick /dev/${OUT_DEV}1
fi

mkdir -p /media/openslx-stick /media/openslx-stick-usrhome


if [ "x$NO_USRHOME" != "xtrue" ]; then
  mount /dev/${OUT_DEV}2 /media/openslx-stick
  mount /dev/${OUT_DEV}1 /media/openslx-stick-usrhome
else
  mount /dev/${OUT_DEV}1 /media/openslx-stick
fi

mkdir -p /media/openslx-stick/boot

cp -v $TMP_DIR/boot/* /media/openslx-stick/boot/

umount /media/openslx-stick 
umount /media/openslx-stick-usrhome $DEBUG_OUT

rm -rf /media/openslx-stick* $DEBUG_OUT

if [ "x$NO_USRHOME" != "xtrue" ]; then
  ${TMP_DIR}/bin/syslinux --install -d /boot -f /dev/${OUT_DEV}2
else
  ${TMP_DIR}/bin/syslinux --install -d /boot -f /dev/${OUT_DEV}1
fi

unset_quiet

echo "Successfully created OpenSLX PreBoot USB Stick .."

exit 0