summaryrefslogtreecommitdiff
path: root/share/hydra/bootless
blob: 819a4649fd575839fa1a33180f6f1bc28ee3ffbf (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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/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/>.

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

# Set bootless folder
function hydra_bootless_folder {
  # Check for a bootless repository
  if [ -e "$HYDRA_FOLDER/bootless" ]; then
    BOOTLESS_DIR="$HYDRA_FOLDER/bootless"
  elif [ -e "$HYDRA_FOLDER/conf/bootless" ]; then
    BOOTLESS_DIR="$HYDRA_FOLDER/conf/bootless"
  else
    echo "Please make a symlink $HYDRA_FOLDER/bootless pointing to your bootless repository."
    exit 1
  fi
}

# Sync bootless folder
function hydra_bootless_rsync {
  # Exclude git folder and follow git-annex symlinks
  $sudo rsync -CLavz --exclude boot $1/ $2/

  # Make sure there's a symlink to itself
  ( cd $2 && ln -s . boot )
}

# Make a boot device
function hydra_bootless_make {
  # Set folder
  hydra_bootless_folder

  # Set sudo config
  local sudo device rsync
  if [ "`whoami`" != 'root' ]; then
    sudo="sudo"
  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
  
  # Git repo consistency check
  ( cd ${BOOTLESS_DIR} && git status > /dev/null )
  if [ $? -ne 0 ]; then
    echo "Error: '${BOOTLESS_DIR}' is not a git repository."
    exit 1
  fi
  
  if [ -z "$1" ]; then
    echo -n "Target device: "
    read device
  else
    device=$1
  fi

  if [ "$2" == "--rsync" ]; then
    rsync="true"
  fi

  usbdevice=`echo ${device} | sed -e s/[0-9]\$//g`
  
  # Issue a warning
  hydra_bootless_warning ${device}
  
  if [ "$?" != "0" ]; then
    echo "Aborting..."
    exit 1
  fi

  hydra_bootless_device ${usbdevice} ${device}

  # Mount
  tmpdir=`mktemp -d`
  $sudo mount ${device} ${tmpdir}
  if [ $? != 0 ]; then
    echo "Error: failed to mount \"${device}\" filesystem in \"${tmpdir}\" (errno: $?)."
    exit 1
  fi
  
  # Copy data
  if [ "$rsync" == "true" ]; then
    hydra_bootless_rsync ${BOOTLESS_DIR} ${tmpdir}/boot
  else
    #$sudo git clone --depth=1 file:///${BOOTLESS_DIR} ${tmpdir}/boot
    $sudo git clone file:///${BOOTLESS_DIR} ${tmpdir}/boot
    if [ $? != 0 ]; then
      echo "Error: failed to clone repository \"${BOOTLESS_DIR}\" in \"${tmpdir}\" (errno: $?)."
      exit 1
    fi

    (
      cd ${tmpdir}/boot && git annex init && git config annex.sshcaching false && git annex untrust here && git annex sync && git annex get .
    )

    if [ $? != 0 ]; then
      echo "Error: failed to get files into repository \"${BOOTLESS_DIR}\" in \"${tmpdir}\" (errno: $?)."
      exit 1
    fi
  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
  #$sudo grub-install --root-directory=${tmpdir} ${usbdevice} --force
  $sudo grub-install --boot-directory=${tmpdir} --allow-floppy --force ${usbdevice}
  if [ $? != 0 ]; then
    echo "Error: grub-install failed (errno: $?)."
    exit 1
  fi

  # TODO: consider an alternative scenario:
  # https://www.gnu.org/software/grub/manual/html_node/Installing-GRUB-using-grub_002dinstall.html#Installing-GRUB-using-grub_002dinstall
  #sudo losetup /dev/loop0 /dev/sdb1
  #sudo mount /dev/loop0 /media/usb/
  #sudo grub-install --boot-directory=/media/usb/ --allow-floppy --force /dev/loop0
  
  # Finalize
  #rm -f ${devicemap}
  $sudo umount ${tmpdir}
  $sudo rm -rf ${tmpdir}
}

# Update a boot device
function hydra_bootless_update {
  # Set folder
  hydra_bootless_folder

  # Set sudo config
  local sudo
  if [ "`whoami`" != 'root' ]; then
    sudo="sudo"
  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
  elif [ ! -d "${tmpdir}/boot" ]; then
    echo "Error: bootless repository not found at ${device}."
    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

  # Copy data
  if [ -d "${tmpdir}/boot/.git" ]; then
    #( cd ${tmpdir}/boot && $sudo git pull origin master )
    ( cd ${tmpdir}/boot && $sudo git annex sync && git annex get . )
  else
    hydra_bootless_rsync ${BOOTLESS_DIR} ${tmpdir}/boot
  fi

  # Finalize
  $sudo umount ${tmpdir}
  $sudo rm -rf ${tmpdir}
}

# Repository initiator
function hydra_bootless_init {
  mkdir -p $HYDRA_FOLDER

  if [ ! -z "$1" ]; then
    # Clone from url
    git clone $1 $HYDRA_FOLDER/bootless &&
      ( cd $HYDRA_FOLDER/bootless && git annex init && git config annex.sshcaching false && git annex sync )
    exit $?
  fi

  # Create a fresh repository
  mkdir -p $HYDRA_FOLDER/bootless/{default,custom,grub}
  mkdir -p $HYDRA_FOLDER/bootless/default/{debian,memtest,ubuntu}
  touch $HYDRA_FOLDER/bootless/{default,custom,grub}/.empty
  touch $HYDRA_FOLDER/bootless/default/{debian,memtest,ubuntu}/.empty
  ( cd $HYDRA_FOLDER/bootless && ln -s . boot)

  if [ -f "/boot/memtest86+.bin" ]; then
    cp /boot/memtest86+.bin $HYDRA_FOLDER/bootless/default/memtest
  else
    echo "No memtest image found. Please install memtest86+ package"
    echo "and manually copy /boot/memtest86+.bin if you want memtest support"
  fi

  # Grub configuration
  cat > $HYDRA_FOLDER/bootless/grub/grub.cfg <<-EOF    
# This is grub.cfg for use with Bootless Management System

### BEGIN header ###
if [ -s $prefix/grubenv ]; then
  load_env
fi
set default="0"
if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}

function load_video {
}

set timeout=5
### END header ###

### BEGIN debian_theme ###
set menu_color_normal=white/blue
set menu_color_highlight=yellow/red
### END debian_theme ###
EOF

  # Initialize git repository
  (
  cd $HYDRA_FOLDER/bootless
  git init
  git annex init
  git config annex.sshcaching false
  git add boot
  git add {default,custom,grub}/.empty
  git add default/{debian,memtest,ubuntu}/.empty
  git add grub/grub.cfg
  git annex add .
  git commit -a -m "Initial import"
  )

  echo "Now add your boot images and edit $HYDRA_FOLDER/bootless/grub/grub.cfg to suit your needs."
}

# Git wrapper
function hydra_bootless_git {
  # Set folder
  hydra_bootless_folder

  (
  cd $BOOTLESS_DIR && git $*
  )
}

# Create a target device
function hydra_bootless_device {
  local device="$1"
  local subdevice="$2"

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

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

  # Remove old partitions
  for partition in `$sudo parted -s -- ${device} print | awk '/^ / {print $1}'`; do
    $sudo parted -s -- ${device} rm $partition
  done

  # Create a single partition
  $sudo parted -s -- ${device} mkpart primary ext2 2 -1
  $sudo parted -s -- ${device} set 1 boot on

  # Create filesystem
  $sudo mke2fs ${subdevice}
  if [ $? != 0 ]; then
    echo "Error: mke2fs failed in \"${subdevice}\" (errno: $?)."
    exit 1
  fi

  # Tune
  $sudo tune2fs -c 0 -i 0 ${subdevice}
  if [ $? != 0 ]; then
    echo "Error: tune2fs failed in \"${subdevice}\" (errno: $?)."
    exit 1
  fi
}

function hydra_bootless_warning {
  local device=$1
  local go

  echo "******************"
  echo "*  ATTENTION!!!  *"
  echo "******************"
  echo ""
  echo "If you continue, all data in device \"${device}\" will be destroyed!"
  echo ""
  echo -n "Are you sure you want to continue? Type uppercase \"YES\": "
  read go

  if [ "${go}" != "YES" ]; then
    false
  else
    true
  fi
}

# Generate a bootless disk image
function hydra_bootless_image {
  local output="$1"
  local device="$2"

  # Fix parameters
  if echo ${output} | grep -q "/dev/"; then
    output="bootless.img"
    device="$1"
  elif [ -z "$output" ]; then
    output="bootless.img"
  fi

  # Set folder
  hydra_bootless_folder

  # Set sudo config
  local sudo
  if [ "`whoami`" != 'root' ]; then
    sudo="sudo"
  fi

  # Copy data
  tmpdir=`mktemp -d`
  hydra_bootless_rsync ${BOOTLESS_DIR} ${tmpdir}/boot

  # Make rescue disk
  grub-mkrescue -o ${output} ${tmpdir}

  # Optionally copy to removable media
  if [ ! -z "$device" ]; then
    # Issue a warning
    hydra_bootless_warning ${device}

    if [ "$?" != "0" ]; then
      echo "Skipping copy of ${output} to ${device}..."
    else
      # Check if device is mounted
      if [ "`mount | grep ${device}`" != "" ]; then
        echo "Error: device \"${device}\" is mounted."
        echo "Skipping copy of ${output} to ${device}..."
      else
        $sudo dd if=${output} of=${device}
      fi
    fi

  fi

  # Cleanup
  rm -rf ${tmpdir}

  # Finish
  if [ -z "$device" ]; then
    echo "Image saved at ${output}"
  else
    echo "Removing image ${output}..."
    rm ${output}
  fi

  echo "Done."
}

# Usage
function hydra_bootless_usage {
  echo "Usage: `basename $0` <action> [arguments]"
  exit 1
}

# Check for requirements
for req in parted git-annex; do
  hydra_install_package $req
done

# TODO: properly support $subdevice in parted
#       or always use first device (like /dev/sdb1)
# Parameter verification
if [ -z "$1" ]; then
  hydra_bootless_usage
elif [ -z "$HYDRA_FOLDER" ]; then
  echo "Parameter HYDRA_FOLDER not configured."
  exit 1
elif [ "$1" == "make" ]; then
  shift
  hydra_bootless_make $*
elif [ "$1" == "init" ]; then
  shift
  hydra_bootless_init $*
elif [ "$1" == "update" ]; then
  shift
  hydra_bootless_update $*
elif [ "$1" == "git" ]; then
  shift
  hydra_bootless_git $*
elif [ "$1" == "image" ]; then
  shift
  hydra_bootless_image $*
else
  hydra_bootless_usage
fi