alpine-make-rootfs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #!/bin/sh
  2. # vim: set ts=4:
  3. #---help---
  4. # Usage: alpine-make-rootfs [options] [--] <dest> [<script> [<script-opts...>]]
  5. #
  6. # This script creates Alpine Linux rootfs for containers. It must be run as
  7. # root - to create files with correct permissions and use chroot (optional).
  8. # If $APK is not available on the host system, then static apk-tools
  9. # specified by $APK_TOOLS_URI is downloaded and used.
  10. #
  11. # Arguments:
  12. # <dest> Path where to write the rootfs. It may be:
  13. # - path with suffix .tar, .tar.bz2, .tbz, .tar.gz, .tgz,
  14. # or tar.xz to create a TAR archive;
  15. # - other path to keep rootfs as a directory;
  16. # - or "-" to dump TAR archive (w/o compression) to STDOUT.
  17. #
  18. # <script> Path of script to execute after installing base system in
  19. # the prepared rootfs and before clean-up. Use "-" to read
  20. # the script from STDIN; if it doesn't start with a shebang,
  21. # "#!/bin/sh -e" is prepended.
  22. #
  23. # <script-opts> Arguments to pass to the script.
  24. #
  25. # Options and Environment Variables:
  26. # -b --branch ALPINE_BRANCH Alpine branch to install; used only when
  27. # --repositories-file is not specified. Default is
  28. # latest-stable.
  29. #
  30. # -s --fs-skel-dir FS_SKEL_DIR Path of directory which content to recursively copy
  31. # (using rsync) into / of the rootfs.
  32. #
  33. # --fs-skel-chown FS_SKEL_CHOWN Force all files from FS_SKEL_DIR to be owned by the
  34. # specified USER:GROUP.
  35. #
  36. # --keys-dir KEYS_DIR Path of directory with Alpine keys to copy into
  37. # the rootfs. Default is /etc/apk/keys. If does not exist,
  38. # keys for x86_64 embedded in this script will be used.
  39. #
  40. # -m --mirror-uri ALPINE_MIRROR URI of the Aports mirror to fetch packages; used only
  41. # when --repositories-file is not specified. Default is
  42. # http://dl-cdn.alpinelinux.org/alpine.
  43. #
  44. # -C --no-cleanup (CLEANUP) Don't umount and remove temporary directories when done.
  45. #
  46. # --no-default-pkgs (DEFAULT_PKGS) Don't install the default base packages (alpine-baselayout
  47. # busybox busybox-suid musl-utils), i.e. only the packages
  48. # specified in --packages will be installed. Use only if
  49. # you know what are you doing!
  50. #
  51. # -p --packages PACKAGES Additional packages to install into the rootfs.
  52. #
  53. # -r --repositories-file REPOS_FILE Path of repositories file to copy into the rootfs.
  54. # If not specified, a repositories file will be created with
  55. # Alpine's main and community repositories on --mirror-uri.
  56. #
  57. # -c --script-chroot (SCRIPT_CHROOT) Bind <script>'s directory at /mnt inside the rootfs dir
  58. # and chroot into the rootfs before executing <script>.
  59. # Otherwise <script> is executed in the current directory
  60. # and $ROOTFS variable points to the rootfs directory.
  61. #
  62. # -d --temp-dir TEMP_DIR Path where to create a temporary directory; used for
  63. # downloading apk-tools when not available on the host
  64. # sytem or for rootfs when <dest> is "-" (i.e. STDOUT).
  65. # This path must not exist! Defaults to using `mkdir -d`.
  66. #
  67. # -t --timezone TIMEZONE Timezone to set (e.g. Europe/Prague). Default is to leave
  68. # timezone UTC.
  69. #
  70. # -h --help Show this help message and exit.
  71. #
  72. # -v --version Print version and exit.
  73. #
  74. # APK APK command to use. Default is "apk".
  75. #
  76. # APK_OPTS Options to pass into apk on each execution.
  77. # Default is "--no-progress".
  78. #
  79. # APK_TOOLS_URI URL of apk-tools binary to download if $APK is not found
  80. # on the host system. Default is x86_64 apk.static from
  81. # https://gitlab.alpinelinux.org/alpine/apk-tools/-/packages.
  82. #
  83. # APK_TOOLS_SHA256 SHA-256 checksum of $APK_TOOLS_URI.
  84. #
  85. # Each option can be also provided by environment variable. If both option and
  86. # variable is specified and the option accepts only one argument, then the
  87. # option takes precedence.
  88. #
  89. # https://github.com/alpinelinux/alpine-make-rootfs
  90. #---help---
  91. set -eu
  92. readonly PROGNAME='alpine-make-rootfs'
  93. readonly VERSION='0.6.1'
  94. # Base Alpine packages to install in rootfs.
  95. readonly ALPINE_BASE_PKGS='alpine-baselayout busybox busybox-suid musl-utils'
  96. # Alpine APK keys for verification of packages for x86_64.
  97. readonly ALPINE_KEYS='
  98. alpine-devel@lists.alpinelinux.org-4a6a0840.rsa.pub:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1yHJxQgsHQREclQu4Ohe\nqxTxd1tHcNnvnQTu/UrTky8wWvgXT+jpveroeWWnzmsYlDI93eLI2ORakxb3gA2O\nQ0Ry4ws8vhaxLQGC74uQR5+/yYrLuTKydFzuPaS1dK19qJPXB8GMdmFOijnXX4SA\njixuHLe1WW7kZVtjL7nufvpXkWBGjsfrvskdNA/5MfxAeBbqPgaq0QMEfxMAn6/R\nL5kNepi/Vr4S39Xvf2DzWkTLEK8pcnjNkt9/aafhWqFVW7m3HCAII6h/qlQNQKSo\nGuH34Q8GsFG30izUENV9avY7hSLq7nggsvknlNBZtFUcmGoQrtx3FmyYsIC8/R+B\nywIDAQAB
  99. alpine-devel@lists.alpinelinux.org-5261cecb.rsa.pub:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwlzMkl7b5PBdfMzGdCT0\ncGloRr5xGgVmsdq5EtJvFkFAiN8Ac9MCFy/vAFmS8/7ZaGOXoCDWbYVLTLOO2qtX\nyHRl+7fJVh2N6qrDDFPmdgCi8NaE+3rITWXGrrQ1spJ0B6HIzTDNEjRKnD4xyg4j\ng01FMcJTU6E+V2JBY45CKN9dWr1JDM/nei/Pf0byBJlMp/mSSfjodykmz4Oe13xB\nCa1WTwgFykKYthoLGYrmo+LKIGpMoeEbY1kuUe04UiDe47l6Oggwnl+8XD1MeRWY\nsWgj8sF4dTcSfCMavK4zHRFFQbGp/YFJ/Ww6U9lA3Vq0wyEI6MCMQnoSMFwrbgZw\nwwIDAQAB
  100. alpine-devel@lists.alpinelinux.org-6165ee59.rsa.pub:MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAutQkua2CAig4VFSJ7v54\nALyu/J1WB3oni7qwCZD3veURw7HxpNAj9hR+S5N/pNeZgubQvJWyaPuQDm7PTs1+\ntFGiYNfAsiibX6Rv0wci3M+z2XEVAeR9Vzg6v4qoofDyoTbovn2LztaNEjTkB+oK\ntlvpNhg1zhou0jDVYFniEXvzjckxswHVb8cT0OMTKHALyLPrPOJzVtM9C1ew2Nnc\n3848xLiApMu3NBk0JqfcS3Bo5Y2b1FRVBvdt+2gFoKZix1MnZdAEZ8xQzL/a0YS5\nHd0wj5+EEKHfOd3A75uPa/WQmA+o0cBFfrzm69QDcSJSwGpzWrD1ScH3AK8nWvoj\nv7e9gukK/9yl1b4fQQ00vttwJPSgm9EnfPHLAtgXkRloI27H6/PuLoNvSAMQwuCD\nhQRlyGLPBETKkHeodfLoULjhDi1K2gKJTMhtbnUcAA7nEphkMhPWkBpgFdrH+5z4\nLxy+3ek0cqcI7K68EtrffU8jtUj9LFTUC8dERaIBs7NgQ/LfDbDfGh9g6qVj1hZl\nk9aaIPTm/xsi8v3u+0qaq7KzIBc9s59JOoA8TlpOaYdVgSQhHHLBaahOuAigH+VI\nisbC9vmqsThF2QdDtQt37keuqoda2E6sL7PUvIyVXDRfwX7uMDjlzTxHTymvq2Ck\nhtBqojBnThmjJQFgZXocHG8CAwEAAQ==
  101. '
  102. # List of directories to remove when empty.
  103. readonly UNNECESSARY_DIRS='
  104. /home /media/cdrom /media/floppy /media/usb /mnt /srv /usr/local/bin
  105. /usr/local/lib /usr/local/share
  106. '
  107. # An opaque string used to detect changes in resolv.conf.
  108. readonly RESOLVCONF_MARK="### created by $PROGNAME ###"
  109. # Name used as a "virtual package" for temporarily installed packages.
  110. readonly VIRTUAL_PKG=".make-$PROGNAME"
  111. : ${APK:="apk"}
  112. : ${APK_OPTS:="--no-progress"}
  113. : ${APK_TOOLS_URI:="https://gitlab.alpinelinux.org/api/v4/projects/5/packages/generic/v2.12.10/x86_64/apk.static"}
  114. : ${APK_TOOLS_SHA256:="d7506bb11327b337960910daffed75aa289d8bb350feab624c52965be82ceae8"}
  115. # Set pipefail if supported.
  116. if ( set -o pipefail 2>/dev/null ); then
  117. set -o pipefail
  118. fi
  119. # For compatibility with systems that does not have "realpath" command.
  120. if ! command -v realpath >/dev/null; then
  121. alias realpath='readlink -f'
  122. fi
  123. die() {
  124. printf '\033[1;31mERROR:\033[0m %s\n' "$@" >&2 # bold red
  125. exit 1
  126. }
  127. einfo() {
  128. printf '\n\033[1;36m> %s\033[0m\n' "$@" >&2 # bold cyan
  129. }
  130. # Prints help and exists with the specified status.
  131. help() {
  132. sed -En '/^#---help---/,/^#---help---/p' "$0" | sed -E 's/^# ?//; 1d;$d;'
  133. exit ${1:-0}
  134. }
  135. # Cleans the host system. This function is executed before exiting the script.
  136. cleanup() {
  137. set +eu
  138. trap '' EXIT HUP INT TERM # unset trap to avoid loop
  139. if [ "$INSTALL_HOST_PKGS" = yes ] && _apk info --quiet --installed $VIRTUAL_PKG; then
  140. _apk del $VIRTUAL_PKG >&2
  141. fi
  142. if [ -d "$TEMP_DIR" ]; then
  143. rm -Rf "$TEMP_DIR"
  144. fi
  145. if [ -d "$rootfs" ]; then
  146. umount_recursively "$rootfs" \
  147. || die "Failed to unmount mounts inside $rootfs!"
  148. [ "$rootfs" = "$ROOTFS_DEST" ] || rm -Rf "$rootfs"
  149. fi
  150. }
  151. _apk() {
  152. "$APK" $APK_OPTS "$@"
  153. }
  154. # Writes Alpine APK keys embedded in this script into directory $1.
  155. dump_alpine_keys() {
  156. local dest_dir="$1"
  157. local content file line
  158. mkdir -p "$dest_dir"
  159. for line in $ALPINE_KEYS; do
  160. file=${line%%:*}
  161. content=${line#*:}
  162. printf -- "-----BEGIN PUBLIC KEY-----\n$content\n-----END PUBLIC KEY-----\n" \
  163. > "$dest_dir/$file"
  164. done
  165. }
  166. # Binds the directory $1 at the mountpoint $2 and sets propagation to private.
  167. mount_bind() {
  168. mkdir -p "$2"
  169. mount --bind "$1" "$2"
  170. mount --make-private "$2"
  171. }
  172. # Prepares chroot at the specified path.
  173. prepare_chroot() {
  174. local dest="$1"
  175. mkdir -p "$dest"/proc
  176. mount -t proc none "$dest"/proc
  177. mount_bind /dev "$dest"/dev
  178. mount_bind /sys "$dest"/sys
  179. install -D -m 644 /etc/resolv.conf "$dest"/etc/resolv.conf
  180. echo "$RESOLVCONF_MARK" >> "$dest"/etc/resolv.conf
  181. }
  182. # Sets up timezone $1 in Alpine rootfs.
  183. setup_timezone() {
  184. local timezone="$1"
  185. local rootfs="${2:-}"
  186. _apk add --root "$rootfs" tzdata
  187. install -D "$rootfs"/usr/share/zoneinfo/$timezone \
  188. "$rootfs"/etc/zoneinfo/$timezone
  189. ln -sf zoneinfo/$timezone "$rootfs"/etc/localtime
  190. _apk del --root "$rootfs" tzdata
  191. }
  192. # Unmounts all filesystems under the directory tree $1 (must be absolute path).
  193. umount_recursively() {
  194. local mount_point="$(realpath "$1")"
  195. cat /proc/mounts \
  196. | cut -d ' ' -f 2 \
  197. | { grep "^$mount_point/" || true; } \
  198. | sort -r \
  199. | xargs -r -n 1 umount
  200. }
  201. # Downloads the specified file using wget and checks checksum.
  202. wgets() (
  203. local url="$1"
  204. local sha256="$2"
  205. local dest="${3:-.}"
  206. cd "$dest" \
  207. && wget -T 10 --no-verbose "$url" \
  208. && echo "$sha256 ${url##*/}" | sha256sum -c
  209. )
  210. # Writes STDIN into file $1 and sets it executable bit. If the content does not
  211. # start with a shebang, prepends "#!/bin/sh -e" before the first line.
  212. write_script() {
  213. local filename="$1"
  214. cat > "$filename.tmp"
  215. if ! grep -q -m 1 '^#!' "$filename.tmp"; then
  216. echo "#!/bin/sh -e" > "$filename"
  217. fi
  218. cat "$filename.tmp" >> "$filename"
  219. rm "$filename.tmp"
  220. chmod +x "$filename"
  221. }
  222. #============================= M a i n ==============================#
  223. opts=$(getopt -n $PROGNAME -o b:m:Cp:r:s:cd:t:hV \
  224. -l branch:,fs-skel-chown:,fs-skel-dir:,keys-dir:,mirror-uri:,no-cleanup,no-default-pkgs,packages:,repositories-file:,script-chroot,temp-dir:,timezone:,help,version \
  225. -- "$@") || help 1 >&2
  226. eval set -- "$opts"
  227. while [ $# -gt 0 ]; do
  228. n=2
  229. case "$1" in
  230. -b | --branch) ALPINE_BRANCH="$2";;
  231. -s | --fs-skel-dir) FS_SKEL_DIR="$2";;
  232. --fs-skel-chown) FS_SKEL_CHOWN="$2";;
  233. --keys-dir) KEYS_DIR="$2";;
  234. -m | --mirror-uri) ALPINE_MIRROR="$2";;
  235. -C | --no-cleanup) CLEANUP='no'; n=1;;
  236. --no-default-pkgs) DEFAULT_PKGS='no'; n=1;;
  237. -p | --packages) PACKAGES="${PACKAGES:-} $2";;
  238. -r | --repositories-file) REPOS_FILE="$2";;
  239. -c | --script-chroot) SCRIPT_CHROOT='yes'; n=1;;
  240. -d | --temp-dir) TEMP_DIR="$2";;
  241. -t | --timezone) TIMEZONE="$2";;
  242. -h | --help) help 0;;
  243. -V | --version) echo "$PROGNAME $VERSION"; exit 0;;
  244. --) shift; break;;
  245. esac
  246. shift $n
  247. done
  248. [ $# -ne 0 ] || help 1 >&2
  249. ROOTFS_DEST="$1"; shift
  250. SCRIPT=
  251. [ $# -eq 0 ] || { SCRIPT="$1"; shift; }
  252. [ "$(id -u)" -eq 0 ] || die 'This script must be run as root!'
  253. [ ! -e "${TEMP_DIR:-}" ] || die "Temp path $TEMP_DIR must not exist!"
  254. : ${ALPINE_BRANCH:="latest-stable"}
  255. : ${ALPINE_MIRROR:="http://dl-cdn.alpinelinux.org/alpine"}
  256. : ${CLEANUP:="yes"}
  257. : ${DEFAULT_PKGS:="yes"}
  258. : ${FS_SKEL_CHOWN:=}
  259. : ${FS_SKEL_DIR:=}
  260. : ${KEYS_DIR:="/etc/apk/keys"}
  261. : ${PACKAGES:=}
  262. : ${REPOS_FILE:=}
  263. : ${SCRIPT_CHROOT:="no"}
  264. : ${TEMP_DIR:="$(mktemp -d /tmp/$PROGNAME.XXXXXX)"}
  265. : ${TIMEZONE:=}
  266. case "$ALPINE_BRANCH" in
  267. [0-9]*) ALPINE_BRANCH="v$ALPINE_BRANCH";;
  268. esac
  269. host_pkgs=''
  270. case "$ROOTFS_DEST" in
  271. *.tar.bz2 | *.tbz) tar_opts='-cj';;
  272. *.tar.gz | *.tgz) tar_opts='-cz';;
  273. *.tar.xz) tar_opts='-cJ'; host_pkgs="$host_pkgs xz";;
  274. *.tar | -) tar_opts='-c';;
  275. *) tar_opts='';;
  276. esac
  277. [ -z "$FS_SKEL_DIR" ] || host_pkgs="$host_pkgs rsync"
  278. rootfs_pkgs="$PACKAGES"
  279. if [ "$DEFAULT_PKGS" = 'yes' ]; then
  280. rootfs_pkgs="$ALPINE_BASE_PKGS $rootfs_pkgs"
  281. fi
  282. [ "$rootfs_pkgs" ] || die 'No packages specified to be installed!'
  283. rootfs="$ROOTFS_DEST"
  284. if [ "$ROOTFS_DEST" = '-' ]; then
  285. rootfs="$TEMP_DIR/rootfs"
  286. elif [ "$tar_opts" ]; then
  287. rootfs="${rootfs%.*}"
  288. rootfs="${rootfs%.tar}"
  289. fi
  290. if [ "$SCRIPT" = '-' ]; then
  291. SCRIPT="$TEMP_DIR/setup.sh"
  292. write_script "$SCRIPT"
  293. fi
  294. if [ "$SCRIPT" ]; then
  295. SCRIPT=$(realpath "$SCRIPT")
  296. fi
  297. if [ -f /etc/alpine-release ]; then
  298. : ${INSTALL_HOST_PKGS:="yes"}
  299. else
  300. : ${INSTALL_HOST_PKGS:="no"}
  301. fi
  302. [ "$CLEANUP" = no ] || trap cleanup EXIT HUP INT TERM
  303. #-----------------------------------------------------------------------
  304. if [ "$INSTALL_HOST_PKGS" = yes ] && [ "$host_pkgs" ]; then
  305. einfo "Installing $host_pkgs on host system"
  306. _apk add -t $VIRTUAL_PKG $host_pkgs >&2
  307. fi
  308. #-----------------------------------------------------------------------
  309. if ! command -v "$APK" >/dev/null; then
  310. einfo "$APK not found, downloading static apk-tools"
  311. wgets "$APK_TOOLS_URI" "$APK_TOOLS_SHA256" "$TEMP_DIR"
  312. APK="$TEMP_DIR/apk.static"
  313. chmod +x "$APK"
  314. fi
  315. #-----------------------------------------------------------------------
  316. einfo 'Installing system'
  317. mkdir -p "$rootfs"/etc/apk/keys
  318. if [ -f "$REPOS_FILE" ]; then
  319. install -m 644 "$REPOS_FILE" "$rootfs"/etc/apk/repositories
  320. else
  321. cat > "$rootfs"/etc/apk/repositories <<-EOF
  322. $ALPINE_MIRROR/$ALPINE_BRANCH/main
  323. $ALPINE_MIRROR/$ALPINE_BRANCH/community
  324. EOF
  325. fi
  326. if [ -d "$KEYS_DIR" ]; then
  327. cp "$KEYS_DIR"/* "$rootfs"/etc/apk/keys/
  328. else
  329. dump_alpine_keys "$rootfs"/etc/apk/keys/
  330. fi
  331. _apk add --root "$rootfs" --update-cache --initdb $rootfs_pkgs >&2
  332. if ! [ -f "$rootfs"/etc/alpine-release ]; then
  333. if _apk info --root "$rootfs" --quiet alpine-release >/dev/null; then
  334. _apk add --root "$rootfs" alpine-release
  335. else
  336. # In Alpine <3.17, this package contains /etc/os-release,
  337. # /etc/alpine-release and /etc/issue, but we don't wanna install all
  338. # its dependencies (e.g. openrc).
  339. _apk fetch --root "$rootfs" --stdout alpine-base \
  340. | tar -xz -C "$rootfs" etc >&2
  341. fi
  342. fi
  343. # Disable root log in without password.
  344. sed -i 's/^root::/root:*:/' "$rootfs"/etc/shadow
  345. [ -e "$rootfs"/var/run ] || ln -s /run "$rootfs"/var/run
  346. #-----------------------------------------------------------------------
  347. if [ "$TIMEZONE" ]; then
  348. einfo "Setting timezone $TIMEZONE"
  349. setup_timezone "$TIMEZONE" "$rootfs" >&2
  350. fi
  351. #-----------------------------------------------------------------------
  352. if [ "$FS_SKEL_DIR" ]; then
  353. einfo "Copying content of $FS_SKEL_DIR into rootfs"
  354. [ "$FS_SKEL_CHOWN" ] \
  355. && rsync_opts="--chown $FS_SKEL_CHOWN" \
  356. || rsync_opts='--numeric-ids'
  357. rsync --archive --info=NAME2 --whole-file $rsync_opts "$FS_SKEL_DIR"/ "$rootfs"/ >&2
  358. # rsync may modify perms of the rootfs dir itself, so make sure it's correct.
  359. install -d -m 0755 -o root -g root "$rootfs"
  360. fi
  361. #-----------------------------------------------------------------------
  362. if [ "$SCRIPT" ]; then
  363. script_name="${SCRIPT##*/}"
  364. if [ "$SCRIPT_CHROOT" = 'no' ]; then
  365. einfo "Executing script: $script_name $*"
  366. ROOTFS="$rootfs" "$SCRIPT" "$@" >&2 || die 'Script failed'
  367. else
  368. einfo "Executing script in chroot: $script_name $*"
  369. _apk add --root "$rootfs" -t "$VIRTUAL_PKG" apk-tools >&2
  370. prepare_chroot "$rootfs"
  371. mount_bind "${SCRIPT%/*}" "$rootfs"/mnt
  372. chroot "$rootfs" \
  373. /bin/sh -c "cd /mnt && ./$script_name \"\$@\"" -- "$@" >&2 \
  374. || die 'Script failed'
  375. umount_recursively "$rootfs"
  376. fi
  377. fi
  378. #-----------------------------------------------------------------------
  379. einfo 'Cleaning-up rootfs'
  380. if _apk info --root "$rootfs" --quiet --installed "$VIRTUAL_PKG"; then
  381. _apk del --root "$rootfs" --purge "$VIRTUAL_PKG" >&2
  382. fi
  383. if grep -qw "$RESOLVCONF_MARK" "$rootfs"/etc/resolv.conf 2>/dev/null; then
  384. rm "$rootfs"/etc/resolv.conf
  385. fi
  386. rm -Rf "$rootfs"/dev/*
  387. if [ -f "$rootfs"/sbin/apk ]; then
  388. rm -Rf "$rootfs"/var/cache/apk/*
  389. else
  390. rm -Rf "$rootfs"/etc/apk "$rootfs"/lib/apk "$rootfs"/var/cache/apk
  391. fi
  392. for dir in $UNNECESSARY_DIRS; do
  393. rmdir -p "$rootfs$dir" 2>/dev/null || true
  394. done
  395. #-----------------------------------------------------------------------
  396. if [ "$tar_opts" ]; then
  397. einfo 'Creating rootfs archive'
  398. tar -C "$rootfs" $tar_opts --numeric-owner -f "$ROOTFS_DEST" .
  399. if [ -f "$ROOTFS_DEST" ] && [ "${SUDO_UID:-}" ] && [ "${SUDO_GID:-}" ]; then
  400. chown "$SUDO_UID:$SUDO_GID" "$ROOTFS_DEST"
  401. fi
  402. ls -la "$ROOTFS_DEST" >&2
  403. fi