|
| 1 | + |
| 2 | +echo " |
| 3 | + chroot for modern Kindle |
| 4 | + Copyright (C) 2019 zaoqi |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU Affero General Public License as published |
| 8 | + by the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU Affero General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Affero General Public License |
| 17 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | +" |
| 19 | + |
| 20 | +cd "$(dirname "$0")" |
| 21 | + |
| 22 | +fail(){ |
| 23 | + echo "Failed. $*" |
| 24 | + echo "press enter to countinue." |
| 25 | + read |
| 26 | + exit 1 |
| 27 | +} |
| 28 | + |
| 29 | +BIN="$(pwd)" |
| 30 | +ROOTFS_DIR="$(pwd)/rootfs" |
| 31 | +ROOTFS_IMG="$(pwd)/rootfs.img" |
| 32 | +ROOTFS_TYPE=ext3 |
| 33 | +ROOTFS_LOCK="/tmp/kUaL_lInUx_mOuNtEd" |
| 34 | +INNER_TMP="/tmp/kUaL_lInUx" |
| 35 | +mkdir -p tmp.testext4 |
| 36 | +cp rootfs.ext4.base tmp.testext4.img || fail |
| 37 | +if mount -o loop tmp.testext4.img tmp.testext4; then |
| 38 | + echo This kernel support ext4. |
| 39 | + ROOTFS_TYPE=ext4 |
| 40 | +else |
| 41 | + mkdir -p tmp.testext3 |
| 42 | + cp rootfs.ext3.base tmp.testext3.img || fail |
| 43 | + mount -o loop tmp.testext3.img tmp.testext3 || fail "This kernel doesn't support ext4 and ext3." |
| 44 | + echo This kernel support ext3. |
| 45 | + ROOTFS_TYPE=ext3 |
| 46 | +fi |
| 47 | +umount tmp.testext4 |
| 48 | +umount tmp.testext3 |
| 49 | +rm -fr tmp.* |
| 50 | + |
| 51 | +get_rootfs_tgz_filename(){ |
| 52 | + curl http://dl-cdn.alpinelinux.org/alpine/edge/releases/armhf/ | |
| 53 | + grep '^<a.*"alpine-minirootfs-[0-9]*-armhf\.tar\.gz"' | |
| 54 | + sed 's|^<a *href="\(.*\)">.*</a>.*$|\1|' | |
| 55 | + sort | |
| 56 | + tail -1 |
| 57 | +} |
| 58 | +get_rootfs_tgz_url(){ |
| 59 | + echo "http://dl-cdn.alpinelinux.org/alpine/edge/releases/armhf/$(get_rootfs_filename)" |
| 60 | +} |
| 61 | + |
| 62 | +mount_rootfs_base(){ |
| 63 | + [ -f "$ROOTFS_LOCK" ] && fail "rootfs mounted." |
| 64 | + touch "$ROOTFS_LOCK" || fail |
| 65 | + mkdir -p "$ROOTFS_DIR" || fail |
| 66 | + mount -o loop "$ROOTFS_IMG" "$ROOTFS_DIR" || fail "cannot mount rootfs." |
| 67 | +} |
| 68 | + |
| 69 | +umount_rootfs_all(){ |
| 70 | + [ -f "$ROOTFS_LOCK" ] || fail "rootfs is not mounted." |
| 71 | + for d in /dev /dev/pts /proc /sys /tmp; do |
| 72 | + umount "$ROOTFS_DIR/$d" |
| 73 | + done |
| 74 | + umount "$ROOTFS_DIR" || fail "cannot unmount rootfs." |
| 75 | + rm "$ROOTFS_LOCK" || fail |
| 76 | +} |
| 77 | + |
| 78 | +install_rootfs(){ |
| 79 | + [ -f "$ROOTFS_LOCK" ] && fail "rootfs mounted." |
| 80 | + rm -fr "$ROOTFS_IMG" || fail |
| 81 | + mount_rootfs_base |
| 82 | + curl "$(get_rootfs_tgz_url)" | tar -xvz -C "$ROOTFS_DIR" || fail "download and extract rootfs: failed." |
| 83 | + umount_rootfs_all |
| 84 | +} |
| 85 | + |
| 86 | +mount_rootfs_all(){ |
| 87 | + mount_rootfs_base |
| 88 | + mkdir -p "$INNER_TMP" || fail |
| 89 | + mount -o bind "$INNER_TMP" "$ROOTFS_DIR/tmp" || fail "cannot bind /tmp." |
| 90 | + for d in /dev /dev/pts /proc /sys; do |
| 91 | + mount -o bind "/$d" "$ROOTFS_DIR/$d" || fail "cannot bind $d" |
| 92 | + done |
| 93 | +} |
| 94 | + |
| 95 | +do_chroot(){ |
| 96 | + [ -f "$ROOTFS_LOCK" ] || fail "rootfs is not mounted." |
| 97 | + HOME=/root chroot "$ROOTFS_DIR" "$@" |
| 98 | +} |
0 commit comments