aboutsummaryrefslogtreecommitdiff
path: root/bootless
blob: 6f72da81fd33ecd08893ac1919a5c3bdd0aa613e (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
#!/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/>.

# Loader
function bootless_load {
  local dest
  local base

  # Determine if we are in a local or system-wide install.
  if [ -h "$0" ]; then
    dest="$(readlink $0)"

    # Check again as the caller might be a symlink as well
    if [ -h "$dest" ]; then
      base="`dirname $dest`"
      dest="$(dirname $(readlink $dest))"
    else
      base="`dirname $0`"
      dest="`dirname $dest`"
    fi

    # Deal with relative or absolute links
    if [ "`basename $dest`" == "$dest" ]; then
      export APP_BASE="$base"
    else
      export APP_BASE="$dest"
    fi
  else
    export APP_BASE="`dirname $0`"
  fi
}

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

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

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

# Repository initiator
function bootless_init {
  if [ -e "$BOOTLESS_DIR" ]; then
    echo "folder $BOOTLESS_DIR already exists"
    exit 1
  fi

  if [ ! -z "$1" ]; then
    # Clone from url
    git clone $1 $BOOTLESS_DIR

    # Support for git-annex
    #( cd $BOOTLESS_DIR && git annex init && git config annex.sshcaching false && git annex sync )
    exit $?
  fi

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

  if [ -f "/boot/memtest86+.bin" ]; then
    cp /boot/memtest86+.bin $BOOTLESS_DIR/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
  cp $APP_BASE/templates/grub.cfg   $BOOTLESS_DIR/grub/
  cp $APP_BASE/templates/custom.cfg $BOOTLESS_DIR/custom/

  # Initialize git repository
  #(
  #cd $BOOTLESS_DIR
  #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/or edit $BOOTLESS_DIR/custom/custom.cfg to suit your needs."
}

function 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 bootless_image {
  local output="$1"
  local device="$2"

  if [ ! -d "$BOOTLESS_DIR" ]; then
    echo "missing $BOOTLESS_DIR folder, please initialize first"
    exit 1
  fi

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

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

  # Copy data
  tmpdir=`mktemp --tmpdir=/tmp -d`
  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
    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."
}

# Check image/device integrity
function bootless_check {
  echo "Not implemented yet :("
  exit 1
}

# Load
bootless_load $*

# Config
NAME="bootless"
BOOTLESS_VERSION="0.0.1"
BOOTLESS_ACTION="$1"
BOOTLESS_DIR="$2"

# Output name and version
echo "$NAME $BOOTLESS_VERSION"

# Parameter verification
if [ -z "$BOOTLESS_DIR" ]; then
  bootless_usage
elif [ "$BOOTLESS_ACTION" == "init" ]; then
  shift 2
  bootless_init $*
elif [ "$BOOTLESS_ACTION" == "image" ]; then
  shift 2
  bootless_image $*
elif [ "$BOOTLESS_ACTION" == "check" ]; then
  shift 2
  bootless_check $*
else
  bootless_usage
fi