summaryrefslogtreecommitdiff
path: root/share/hydra/bootless
blob: bad5f5028efd68ae6ee5fd3b76d71438a3a3a615 (plain)
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
#!/bin/bash
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

# hydra project bootless init refspec
# hydra project bootless git  update
# hydra project bootless git  commit
# hydra project bootless make /dev/sdb1

# Load.
source $APP_BASE/lib/hydra/functions || exit 1
hydra_config_load

# parameter verification
if [ $# -gt 1 ]; then
  echo "Usage: `basename $0` [target]"
  exit 1
elif [ -z "$HYDRA_FOLDER" ]; then
  echo "Parameter HYDRA_FOLDER not configured."
  exit 1
fi

# script description
cat <<EOF
Bootless Install Script
=======================

This script installs a bootable copy of the bootless repository in a device
partition. It will ask for a device partition, clone the repository in the
partition and make the device bootable.

Press any key to continue, or ^C to abort.
EOF
read tmp

if [ -e "$HYDRA_FOLDER/bootless" ]; then
  gitdir="$HYDRA_FOLDER/bootless"
elif [ -e "$HYDRA_FOLDER/conf/bootless" ]; then
  gitdir="$HYDRA_FOLDER/conf/bootless"
else
  echo "Please make a symlink $HYDRA_FOLDER/bootless pointing to your devel puppet modules."
  exit 1
fi

# git repo consistency check
( cd ${gitdir} && git status > /dev/null )
if [ $? -ne 0 ]; then
  echo "Error: '${gitdir}' is not a git repository."
  exit 1
fi

if [ -z "$1" ]; then
  echo -n "Target device: "
  read device
else
  device=$1
fi

# target device consistency check
if [ ! -b ${device} ]; then
  echo "Error: device \"${device}\" not found."
  exit 1
fi

if [ "`mount | grep ${device}`" != "" ]; then
  echo "Error: device \"${device}\" is mounted."
fi

# verifies that user is sure about that
cat <<EOF

******************
*  ATTENTION!!!  *
******************

If you continue, all data in device "${device}" will be destroyed!

EOF
echo -n "Are you sure you want to continue? Type uppercase \"YES\": "
read go

if [ "${go}" != "YES" ]; then
  echo "Aborting..."
  exit 1
fi

# formatacao e montagem
sudo mke2fs ${device}
if [ $? != 0 ]; then
  echo "Error: mke2fs failed in \"${device}\" (errno: $?)."
  exit 1;
fi
sudo tune2fs -c 0 -i 0 ${device}
if [ $? != 0 ]; then
  echo "Error: tune2fs failed in \"${device}\" (errno: $?)."
  exit 1;
fi
tmpdir=`mktemp -d`
sudo mount ${device} ${tmpdir}
if [ $? != 0 ]; then
  echo "Error: failed to mount \"${device}\" filesystem in \"${tmpdir}\" (errno: $?)."
  exit 1;
fi

# data copy
sudo git clone --depth=1 ${gitdir} ${tmpdir}/boot
if [ $? != 0 ]; then
  echo "Error: failed to clone repository \"${gitdir}\" in \"${tmpdir}\" (errno: $?)."
  exit 1;
fi

# grub legacy
#devicemap=`mktemp`
#grub-mkdevicemap --no-floppy --device-map=${devicemap}
#usbdevice=`echo ${device} | sed -e s/[0-9]\$//`
#grubroot=`grep "${usbdevice}" ${devicemap} | cut -f1`
#grubdevice=`echo ${grubroot} | sed -e s/\)/,0\)/`
#echo "root ${grubdevice}
#setup ${grubroot}
#quit" | grub --device-map=${devicemap} --batch

# grub 2
usbdevice=`echo ${device} | sed -e s/[0-9]\$//`
sudo grub-install --root-directory=${tmpdir} ${usbdevice} --force

# finalize
#rm -f ${devicemap}
sudo umount ${tmpdir}
sudo rm -rf ${tmpdir}