Skip to content

Commit

Permalink
docs: add help for rsdk-chroot/install
Browse files Browse the repository at this point in the history
Signed-off-by: ZHANG Yuntian <[email protected]>
  • Loading branch information
RadxaYuntian committed Aug 20, 2024
1 parent 789424f commit 1b4c641
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 89 deletions.
4 changes: 3 additions & 1 deletion docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
# Command References

- [rsdk](cmd/rsdk.md)
- [rsdk-shell](cmd/rsdk-shell.md)
- [rsdk-build](cmd/rsdk-build.md)
- [rsdk-chroot](cmd/rsdk-chroot.md)
- [rsdk-install](cmd/rsdk-install.md)
- [rsdk-shell](cmd/rsdk-shell.md)
1 change: 1 addition & 0 deletions docs/cmd/rsdk-chroot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun rsdk chroot --help -->
1 change: 1 addition & 0 deletions docs/cmd/rsdk-install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- cmdrun rsdk install --help -->
207 changes: 120 additions & 87 deletions src/libexec/rsdk/rsdk-chroot
Original file line number Diff line number Diff line change
@@ -1,103 +1,136 @@
#!/usr/bin/env bash

SCRIPT_NAME="$(basename "$0")"
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
# shellcheck source=src/lib/rsdk/utils.sh
source "$SCRIPT_DIR/../../lib/rsdk/utils.sh"
main() {
local SCRIPT_NAME SCRIPT_DIR
SCRIPT_NAME="$(basename "$0")"
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
# shellcheck source=src/lib/rsdk/utils.sh
source "$SCRIPT_DIR/../../lib/rsdk/utils.sh"

if [[ -f "/.dockerenv" ]]; then
error "$EXIT_RUNNING_IN_CONTAINER"
fi
local TEMP
if ! TEMP="$(getopt -o "h" -l "help" -n "$0" -- "$@")"; then
return
fi
eval set -- "$TEMP"

if [[ $SCRIPT_NAME == "rsdk-chroot" ]] && (($# < 1)); then
error "$EXIT_TOO_FEW_ARGUMENTS"
elif [[ $SCRIPT_NAME == "rsdk-install" ]] && (($# < 2)); then
error "$EXIT_TOO_FEW_ARGUMENTS"
fi
while true; do
TEMP="$1"
shift
case "$TEMP" in
-h | --help)
rsdk help "${SCRIPT_NAME#rsdk-}"
return
;;
--)
break
;;
*)
error "$EXIT_UNKNOWN_OPTION" "$TEMP"
;;
esac
done

disk="$1"
shift
if (($# == 0)); then
"$0" --help
return
fi

RSDK_TEMP="$(mktemp -d)"
if [[ -f "/.dockerenv" ]]; then
error "$EXIT_RUNNING_IN_CONTAINER"
fi

if [[ -f $disk ]]; then
disk_file="$disk"
trap 'set +e; sudo -n true && (sudo umount -R "$RSDK_TEMP"; sudo kpartx -d "$disk_file"; sync); rm -rf "$RSDK_TEMP"' SIGINT SIGQUIT SIGTSTP EXIT
sudo kpartx -a "$disk"
echo "Target is a file. Trying to find rootfs partition..."
if ! disk="$(sudo blkid -t LABEL=rootfs -o device | grep /dev/mapper/loop | tail -n 1)" &&
! disk="$(sudo blkid -t PARTLABEL=rootfs -o device | grep /dev/mapper/loop | tail -n 1)" &&
! disk="$(sudo blkid -t LABEL=opi_root -o device | grep /dev/mapper/loop | tail -n 1)"; then
error "$EXIT_BLKDEV_NO_ROOTDEV" "$disk_file"
if [[ $SCRIPT_NAME == "rsdk-chroot" ]] && (($# < 1)); then
error "$EXIT_TOO_FEW_ARGUMENTS"
elif [[ $SCRIPT_NAME == "rsdk-install" ]] && (($# < 2)); then
error "$EXIT_TOO_FEW_ARGUMENTS"
fi
disk="${disk%p*}p"
fi

if [[ ! -b $disk ]] && [[ $disk != /dev/mapper/loop* ]]; then
error "$EXIT_NOT_BLOCK_DEVICE" "$disk"
fi
disk="$1"
shift

if [[ -b "$disk"5 ]]; then
# legacy debos-radxa image
sudo mount "$disk"5 "$RSDK_TEMP"
sudo mount "$disk"4 "$RSDK_TEMP/boot"
elif [[ -b "$disk"3 ]]; then
# latest rbuild/rsdk image
sudo mount "$disk"3 "$RSDK_TEMP"
sudo mount "$disk"2 "$RSDK_TEMP/boot/efi"
sudo mount "$disk"1 "$RSDK_TEMP/config"
elif [[ -b "$disk"2 ]]; then
sudo mount "$disk"2 "$RSDK_TEMP"
case "$(sudo blkid "$disk"1 -s LABEL -o value)" in
"armbi_boot" | "opi_boot" | "boot")
# new armbian image / Orange Pi / Rockchip SDK image
sudo mount "$disk"1 "$RSDK_TEMP/boot"
;;
*)
# old rbuild image
RSDK_TEMP="$(mktemp -d)"

if [[ -f $disk ]]; then
disk_file="$disk"
trap 'set +e; sudo -n true && (sudo umount -R "$RSDK_TEMP"; sudo kpartx -d "$disk_file"; sync); rm -rf "$RSDK_TEMP"' SIGINT SIGQUIT SIGTSTP EXIT
sudo kpartx -a "$disk"
echo "Target is a file. Trying to find rootfs partition..."
if ! disk="$(sudo blkid -t LABEL=rootfs -o device | grep /dev/mapper/loop | tail -n 1)" &&
! disk="$(sudo blkid -t PARTLABEL=rootfs -o device | grep /dev/mapper/loop | tail -n 1)" &&
! disk="$(sudo blkid -t LABEL=opi_root -o device | grep /dev/mapper/loop | tail -n 1)"; then
error "$EXIT_BLKDEV_NO_ROOTDEV" "$disk_file"
fi
disk="${disk%p*}p"
fi

if [[ ! -b $disk ]] && [[ $disk != /dev/mapper/loop* ]]; then
error "$EXIT_NOT_BLOCK_DEVICE" "$disk"
fi

if [[ -b "$disk"5 ]]; then
# legacy debos-radxa image
sudo mount "$disk"5 "$RSDK_TEMP"
sudo mount "$disk"4 "$RSDK_TEMP/boot"
elif [[ -b "$disk"3 ]]; then
# latest rbuild/rsdk image
sudo mount "$disk"3 "$RSDK_TEMP"
sudo mount "$disk"2 "$RSDK_TEMP/boot/efi"
sudo mount "$disk"1 "$RSDK_TEMP/config"
;;
esac
elif [[ -b "$disk"1 ]]; then
# old armbian image
sudo mount "$disk"1 "$RSDK_TEMP"
else
error "$EXIT_BLKDEV_NO_ROOTDEV" "$disk"
fi
elif [[ -b "$disk"2 ]]; then
sudo mount "$disk"2 "$RSDK_TEMP"
case "$(sudo blkid "$disk"1 -s LABEL -o value)" in
"armbi_boot" | "opi_boot" | "boot")
# new armbian image / Orange Pi / Rockchip SDK image
sudo mount "$disk"1 "$RSDK_TEMP/boot"
;;
*)
# old rbuild image
sudo mount "$disk"1 "$RSDK_TEMP/config"
;;
esac
elif [[ -b "$disk"1 ]]; then
# old armbian image
sudo mount "$disk"1 "$RSDK_TEMP"
else
error "$EXIT_BLKDEV_NO_ROOTDEV" "$disk"
fi

case "$SCRIPT_NAME" in
rsdk-chroot)
sudo systemd-nspawn -D "$RSDK_TEMP" "$@"
;;
rsdk-install)
file="${1:-}"
ext="${file##*.}"
case "$ext" in
deb)
sudo cp "$file" "$RSDK_TEMP"
sudo systemd-nspawn -D "$RSDK_TEMP" bash -c \
"dpkg -i '/$(basename "$file")' && \
apt-get install -y --fix-missing --allow-downgrades"
case "$SCRIPT_NAME" in
rsdk-chroot)
sudo systemd-nspawn -D "$RSDK_TEMP" "$@"
;;
dtbo)
sudo cp "$file" "$RSDK_TEMP/boot/dtbo"
sudo systemd-nspawn -D "$RSDK_TEMP" u-boot-update
;;
dtb)
sudo find "$RSDK_TEMP/usr/lib/"linux-image-*/ -name "$(basename "$file")" -exec mv "{}" "{}.bak" \; -exec cp "$file" "{}" \;
;;
*)
error "$EXIT_UNSUPPORTED_OPTION" "$file"
rsdk-install)
file="${1:-}"
ext="${file##*.}"
case "$ext" in
deb)
sudo cp "$file" "$RSDK_TEMP"
sudo systemd-nspawn -D "$RSDK_TEMP" bash -c \
"dpkg -i '/$(basename "$file")' && \
apt-get install -y --fix-missing --allow-downgrades"
;;
dtbo)
sudo cp "$file" "$RSDK_TEMP/boot/dtbo"
sudo systemd-nspawn -D "$RSDK_TEMP" u-boot-update
;;
dtb)
sudo find "$RSDK_TEMP/usr/lib/"linux-image-*/ -name "$(basename "$file")" -exec mv "{}" "{}.bak" \; -exec cp "$file" "{}" \;
;;
*)
error "$EXIT_UNSUPPORTED_OPTION" "$file"
;;
esac
;;
esac
;;
esac

sudo umount -R "$RSDK_TEMP"
if cut -f 2 -d " " /proc/mounts | grep -q "$RSDK_TEMP"; then
echo "Failed to umount $RSDK_TEMP. Your kernel might report some issues." >&2
exit 1
else
rm -rf "$RSDK_TEMP"
sync
fi
sudo umount -R "$RSDK_TEMP"
if cut -f 2 -d " " /proc/mounts | grep -q "$RSDK_TEMP"; then
echo "Failed to umount $RSDK_TEMP. Your kernel might report some issues." >&2
exit 1
else
rm -rf "$RSDK_TEMP"
sync
fi
}

main "$@"
63 changes: 62 additions & 1 deletion src/libexec/rsdk/rsdk-help
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ rsdk-help-build() {
rsdk-${FUNCNAME[0]##rsdk-help-}
====
Build RadxaOS system image.
## Command usage
$ rsdk build <options> [product] <suite> <edition>
$ rsdk build [options] <product> [suite] [edition]
## Options
Expand Down Expand Up @@ -45,6 +47,65 @@ $(find "$SCRIPT_DIR/../../../externals/keyrings/" -maxdepth 1 -mindepth 1 -type
EOF
}

rsdk-help-chroot() {
echo "rsdk-${FUNCNAME[0]##rsdk-help-}"
cat <<'EOF'
====
Enter chroot environment for a given disk or disk image.
Requires `systemd-nspawn` (provided by `systemd-container` package in Debian).
## Command usage
$ rsdk chroot <target path>
## Supported target type
* Raw block devices (i.e. ordinary disks)
* System image file
* Requires `kpartx` (provided by `multipath-tools` package in Debian)
Following distributions may be used in addition to `rsdk` image:
* `debox-radxa` image
* `rbuild` image
* `rsdk` image
* `Armbian` image
* Orange Pi image
* Rockchip SDK image
EOF
}

rsdk-help-install() {
echo "rsdk-${FUNCNAME[0]##rsdk-help-}"
cat <<'EOF'
====
Install a build artifacts to a given disk or disk image.
Requires `systemd-nspawn` (provided by `systemd-container` package in Debian).
## Command usage
$ rsdk install <target path> <artifact path>
## Supported target type
* Raw block devices (i.e. ordinary disks)
* System image file
* Requires `kpartx` (provided by `multipath-tools` package in Debian)
## Supported artifact type
* `*.deb`: Debian package
* `*.dtb`: Device tree file
* `*.dtbo`: Device tree overlay file
Only `rsdk` image supports all artifact types.
Other distributions may have issues on some artifact types.
EOF
}

main() {
local SCRIPT_DIR
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
Expand Down

0 comments on commit 1b4c641

Please sign in to comment.