]> git.ipfire.org Git - thirdparty/dracut.git/blob - modules.d/90livenet/livenetroot.sh
feat(livenet): add memory size check depending on live image size
[thirdparty/dracut.git] / modules.d / 90livenet / livenetroot.sh
1 #!/bin/sh
2 # livenetroot - fetch a live image from the network and run it
3
4 type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
5
6 . /lib/url-lib.sh
7
8 PATH=/usr/sbin:/usr/bin:/sbin:/bin
9 RETRIES=${RETRIES:-100}
10 SLEEP=${SLEEP:-5}
11
12 [ -e /tmp/livenet.downloaded ] && exit 0
13
14 # args get passed from 40network/netroot
15 netroot="$2"
16 liveurl="${netroot#livenet:}"
17 info "fetching $liveurl"
18
19 if getargbool 0 'rd.writable.fsimg'; then
20
21 imgsize=$(($(curl -sIL "$liveurl" | sed -n 's/Content-Length: *\([[:digit:]]*\).*/\1/p') / (1024 * 1024)))
22
23 check_live_ram $imgsize
24 fi
25
26 imgfile=
27 #retry until the imgfile is populated with data or the max retries
28 i=1
29 while [ "$i" -le "$RETRIES" ]; do
30 imgfile=$(fetch_url "$liveurl")
31
32 # shellcheck disable=SC2181
33 if [ $? != 0 ]; then
34 warn "failed to download live image: error $?"
35 imgfile=
36 fi
37
38 if [ -n "$imgfile" -a -s "$imgfile" ]; then
39 break
40 else
41 if [ $i -ge "$RETRIES" ]; then
42 warn "failed to download live image after $i attempts."
43 exit 1
44 fi
45
46 sleep "$SLEEP"
47 fi
48
49 i=$((i + 1))
50 done > /tmp/livenet.downloaded
51
52 # TODO: couldn't dmsquash-live-root handle this?
53 if [ "${imgfile##*.}" = "iso" ]; then
54 root=$(losetup -f)
55 losetup "$root" "$imgfile"
56 else
57 root=$imgfile
58 fi
59
60 exec /sbin/dmsquash-live-root "$root"