2 # url-lib.sh - functions for handling URLs (file fetching etc.)
5 # Will Woods <wwoods@redhat.com>
7 type mkuniqdir
> /dev
/null
2>&1 || .
/lib
/dracut-lib.sh
9 # fetch_url URL [OUTFILE]
10 # fetch the given URL to a locally-visible location.
11 # if OUTFILE is given, the URL will be fetched to that filename,
12 # overwriting it if present.
13 # If the URL is something mountable (e.g. nfs://) and no OUTFILE is given,
14 # the server will be left mounted until pre-pivot.
15 # the return values are as follows:
17 # 253: unknown error (file missing)
18 # 254: unhandled URL scheme / protocol
19 # 255: bad arguments / unparsable URLs
20 # other: fetch command failure (whatever curl/mount/etc return)
22 local url
="$1" outloc
="$2"
24 handler
="$(get_url_handler "$url")"
25 [ -n "$handler" ] ||
return 254
26 [ -n "$url" ] ||
return 255
27 "$handler" "$url" "$outloc"
31 # returns the first HANDLERNAME corresponding to the URL's scheme
33 local scheme
="${1%%:*}" item
=""
34 for item
in $url_handler_map; do
35 [ "$scheme" = "${item%%:*}" ] && echo "${item#*:}" && return 0
40 # add_url_handler HANDLERNAME SCHEME [SCHEME...]
41 # associate the named handler with the named scheme(s).
45 local schemes
="$*" scheme
=""
47 for scheme
in $schemes; do
48 [ "$(get_url_handler "$scheme")" = "$handler" ] && continue
49 set -- "$@" "$scheme:$handler"
51 set -- "$@" "$url_handler_map" # add new items to *front* of list
55 ### HTTP, HTTPS, FTP #################################################
57 export CURL_HOME
="/run/initramfs/url-lib"
59 curl_args
="--globoff --location --retry 3 --retry-connrefused --fail --show-error"
60 getargbool
0 rd.noverifyssl
&& curl_args
="$curl_args --insecure"
62 proxy
=$
(getarg proxy
=)
63 [ -n "$proxy" ] && curl_args
="$curl_args --proxy $proxy"
66 local url
="$1" outloc
="$2"
67 echo "$url" > /proc
/self
/fd
/0
68 if [ -n "$outloc" ]; then
69 # shellcheck disable=SC2086
70 curl
$curl_args --output "$outloc" -- "$url" ||
return $?
73 outdir
="$(mkuniqdir /tmp curl_fetch_url)"
76 # shellcheck disable=SC2086
77 curl
$curl_args --remote-name "$url" ||
return $?
79 outloc
="$outdir/$(ls -A "$outdir")"
81 if ! [ -f "$outloc" ]; then
82 warn
"Downloading '$url' failed!"
85 if [ -z "$2" ]; then echo "$outloc"; fi
87 add_url_handler curl_fetch_url http https
ftp tftp
90 echo "header = \"$1: $2\"" >> $CURL_HOME/.curlrc
93 ### TORRENT ##########################################################
95 ctorrent_args
="-E 0 -e 0"
97 ctorrent_fetch_url
() {
98 local url
="$1" outloc
="$2"
100 torrent_outloc
="$outloc.torrent"
101 echo "$url" > /proc
/self
/fd
/0
102 if [ -n "$outloc" ]; then
103 # shellcheck disable=SC2086
104 curl
$curl_args --output "$torrent_outloc" -- "$url" ||
return $?
107 outdir
="$(mkuniqdir /tmp torrent_fetch_url)"
110 # shellcheck disable=SC2086
111 curl
$curl_args --remote-name "$url" ||
return $?
113 torrent_outloc
="$outdir/$(ls -A "$outdir")"
114 outloc
=${torrent_outloc%.*}
116 if ! [ -f "$torrent_outloc" ]; then
117 warn
"Downloading '$url' failed!"
120 # shellcheck disable=SC2086
121 ctorrent
$ctorrent_args -s "$outloc" "$torrent_outloc" >&2
122 if ! [ -f "$outloc" ]; then
123 warn
"Torrent download of '$url' failed!"
126 if [ -z "$2" ]; then echo "$outloc"; fi
129 command -v ctorrent
> /dev
/null \
130 && add_url_handler ctorrent_fetch_url torrent
132 ### NFS ##############################################################
134 [ -e /lib
/nfs-lib.sh
] && .
/lib
/nfs-lib.sh
136 nfs_already_mounted
() {
137 local server
="$1" path
="$2" s
="" p
=""
138 while read -r src mnt rest ||
[ -n "$src" ]; do
139 splitsep
":" "$src" s p
141 if [ "$server" = "$s" ]; then
142 if [ "$path" = "$p" ]; then
144 elif str_starts
"$path" "$p"; then
145 echo "$mnt"/"${path#"$p"/}"
152 local url
="$1" outloc
="$2" nfs
="" server
="" path
="" options
=""
153 nfs_to_var
"$url" ||
return 255
154 local filepath
="${path%/*}" filename
="${path##*/}" mntdir
=""
156 # skip mount if server:/filepath is already mounted
157 mntdir
=$
(nfs_already_mounted
"$server" "$filepath")
158 if [ -z "$mntdir" ]; then
160 mntdir
="$(mkuniqdir /run nfs_mnt)"
161 mount_nfs
"$nfs:$server:$filepath${options:+:$options}" "$mntdir"
162 # lazy unmount during pre-pivot hook
163 inst_hook
--hook pre-pivot
--name 99url
-lib-umount-nfs-"$(basename "$mntdir")" umount
-l -- "$mntdir"
166 if [ -z "$outloc" ]; then
167 outloc
="$mntdir/$filename"
169 cp -f -- "$mntdir/$filename" "$outloc" ||
return $?
171 [ -f "$outloc" ] ||
return 253
172 if [ -z "$2" ]; then echo "$outloc"; fi
174 command -v nfs_to_var
> /dev
/null
&& add_url_handler nfs_fetch_url nfs nfs4