Skip to content

Commit

Permalink
v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
VHSgunzo committed Oct 26, 2024
1 parent cfe5fcc commit 613f30f
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 128 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[alias]
xtask = "run --manifest-path ./xtask/Cargo.toml --"

[target.x86_64-unknown-linux-musl]
rustflags = ["-Z", "remap-cwd-prefix=", "-C", "panic=abort"]
74 changes: 3 additions & 71 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,81 +31,13 @@ jobs:
rustup component add rust-src --toolchain nightly;
cargo install cross;
- name: Build x86_64
run: >
echo ==============================================x86_64==============================================;
cargo clean;
echo runimage;
cargo +nightly build --release --target x86_64-unknown-linux-musl -Z unstable-options -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort;
mv target/x86_64-unknown-linux-musl/release/uruntime uruntime-runimage-x86_64;
echo runimage-squashfs;
cargo +nightly build --release --target x86_64-unknown-linux-musl -Z unstable-options -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --no-default-features --features squashfs;
mv target/x86_64-unknown-linux-musl/release/uruntime uruntime-runimage-squashfs-x86_64;
echo runimage-dwarfs;
cargo +nightly build --release --target x86_64-unknown-linux-musl -Z unstable-options -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --no-default-features --features dwarfs;
mv target/x86_64-unknown-linux-musl/release/uruntime uruntime-runimage-dwarfs-x86_64;
echo appimage;
cargo +nightly build --release --target x86_64-unknown-linux-musl -Z unstable-options -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --features appimage;
mv target/x86_64-unknown-linux-musl/release/uruntime uruntime-appimage-x86_64;
echo appimage-squashfs;
cargo +nightly build --release --target x86_64-unknown-linux-musl -Z unstable-options -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --no-default-features --features appimage,squashfs;
mv target/x86_64-unknown-linux-musl/release/uruntime uruntime-appimage-squashfs-x86_64;
echo appimage-dwarfs;
cargo +nightly build --release --target x86_64-unknown-linux-musl -Z unstable-options -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --no-default-features --features appimage,dwarfs;
mv target/x86_64-unknown-linux-musl/release/uruntime uruntime-appimage-dwarfs-x86_64;
- name: Strip x86_64
run: >
echo ==============================================strip==============================================;
strip -s -R .comment -R .gnu.version --strip-unneeded uruntime-*-x86_64 ;
- name: Build aarch64
run: >
echo ==============================================aarch64==============================================;
cargo clean;
echo runimage;
cross build --release --target aarch64-unknown-linux-musl;
mv target/aarch64-unknown-linux-musl/release/uruntime uruntime-runimage-aarch64;
echo runimage-squashfs;
cross build --release --target aarch64-unknown-linux-musl --no-default-features --features squashfs;
mv target/aarch64-unknown-linux-musl/release/uruntime uruntime-runimage-squashfs-aarch64;
echo runimage-dwarfs;
cross build --release --target aarch64-unknown-linux-musl --no-default-features --features dwarfs;
mv target/aarch64-unknown-linux-musl/release/uruntime uruntime-runimage-dwarfs-aarch64;
echo appimage;
cross build --release --target aarch64-unknown-linux-musl --features appimage;
mv target/aarch64-unknown-linux-musl/release/uruntime uruntime-appimage-aarch64;
echo appimage-squashfs;
cross build --release --target aarch64-unknown-linux-musl --no-default-features --features appimage,squashfs;
mv target/aarch64-unknown-linux-musl/release/uruntime uruntime-appimage-squashfs-aarch64;
echo appimage-dwarfs;
cross build --release --target aarch64-unknown-linux-musl --no-default-features --features appimage,dwarfs;
mv target/aarch64-unknown-linux-musl/release/uruntime uruntime-appimage-dwarfs-aarch64;
- name: Add magic
run: >
echo ==============================================RI==============================================;
ls uruntime-runimage*|xargs -I {} sh -c 'echo -ne "RI\x02"|dd of={} bs=1 count=3 seek=8 conv=notrunc';
echo ==============================================AI==============================================;
ls uruntime-appimage*|xargs -I {} sh -c 'echo -ne "AI\x02"|dd of={} bs=1 count=3 seek=8 conv=notrunc';
- name: Build
run: cargo xtask all

- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
files: uruntime*
files: dist/uruntime*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
assets*
target
.vscode
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uruntime"
version = "0.0.1"
version = "0.0.2"
readme = "README.md"
license = "MIT"
repository = "https://github.com/VHSgunzo/uruntime"
Expand Down
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@ rustup default nightly
rustup target add x86_64-unknown-linux-musl
rustup component add rust-src --toolchain nightly
cargo xtask
# Tasks:
# x86_64 build x86_64 RunImage and AppImage uruntime
# runimage-x86_64 build x86_64 RunImage uruntime
# runimage-squashfs-x86_64 build x86_64 RunImage uruntime (SquashFS only)
# runimage-dwarfs-x86_64 build x86_64 RunImage uruntime (DwarFS only)
# appimage-x86_64 build x86_64 AppImage uruntime
# appimage-squashfs-x86_64 build x86_64 AppImage uruntime (SquashFS only)
# appimage-dwarfs-x86_64 build x86_64 AppImage uruntime (DwarFS only)
#
# aarch64 build aarch64 RunImage and AppImage uruntime
# runimage-aarch64 build aarch64 RunImage uruntime
# runimage-squashfs-aarch64 build aarch64 RunImage uruntime (SquashFS only)
# runimage-dwarfs-aarch64 build aarch64 RunImage uruntime (DwarFS only)
# appimage-aarch64 build aarch64 AppImage uruntime
# appimage-squashfs-aarch64 build aarch64 AppImage uruntime (SquashFS only)
# appimage-dwarfs-aarch64 build aarch64 AppImage uruntime (DwarFS only)
#
# all build all of the above
# for RunImage x86_64
cargo +nightly build --release --target x86_64-unknown-linux-musl -Z unstable-options -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort
mv target/x86_64-unknown-linux-musl/release/uruntime uruntime-runimage-x86_64
echo -ne 'RI\x02'|dd of=uruntime-runimage-x86_64 bs=1 count=3 seek=8 conv=notrunc
cargo xtask runimage-x86_64
# for AppImage x86_64
cargo +nightly build --release --target x86_64-unknown-linux-musl -Z unstable-options -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --features appimage
mv target/x86_64-unknown-linux-musl/release/uruntime uruntime-appimage-x86_64
echo -ne 'AI\x02'|dd of=uruntime-appimage-x86_64 bs=1 count=3 seek=8 conv=notrunc
cargo xtask appimage-aarch64
```
See [Build step in ci.yml](https://github.com/VHSgunzo/uruntime/blob/main/.github/workflows/ci.yml#L34)

Expand Down
106 changes: 56 additions & 50 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
use std::time::Instant;
use std::process::exit;
use io::{Error, Result};
use std::process::Command;
use std::env::current_exe;
use io::ErrorKind::NotFound;
use std::{env, fs, io, time};
use std::path::{Path, PathBuf};
use std::thread::{sleep, spawn};
use std::io::{Read, Seek, SeekFrom};
use std::os::unix::fs::{symlink, MetadataExt};
use std::fs::{create_dir_all, read_to_string, remove_dir, remove_file, File};
use std::{
time::Instant,
path::PathBuf,
process::{exit, Command},
thread::{sleep, spawn},
io::{Read, Seek, SeekFrom},
os::unix::fs::{symlink, MetadataExt},
{env::{self, current_exe}, fs, io, time},
fs::{create_dir, create_dir_all, read_to_string, remove_dir, remove_file, File},
};
use io::{ErrorKind::NotFound, Error, Result};

use which::which;
use cfg_if::cfg_if;
use goblin::elf::Elf;
use nix::libc::getuid;
use nix::sys::wait::waitpid;
use signal_hook::iterator::Signals;
use nix::sys::signal::{Signal, kill};
use memfd_exec::{MemFdExecutable, Stdio};
use signal_hook::consts::{SIGINT, SIGTERM, SIGQUIT};
use nix::sys::{wait::waitpid, signal::{Signal, kill}};
use nix::unistd::{access, fork, getcwd, AccessFlags, ForkResult, Pid};
use signal_hook::{consts::{SIGINT, SIGTERM, SIGQUIT}, iterator::Signals};

#[cfg(feature = "pie-ulexec")]
mod pie_ulexec {
pub use std::ffi::CString;
pub use std::os::fd::AsRawFd;
pub use goblin::elf::program_header;
pub use nix::unistd::{close, write};
pub use std::{os::fd::AsRawFd, ffi::CString};
pub use nix::sys::memfd::{memfd_create, MemFdCreateFlag};
}
#[cfg(feature = "pie-ulexec")]
Expand Down Expand Up @@ -96,7 +91,7 @@ fn get_env_var(env_var: &str) -> String {
}

fn add_to_path(dir: &PathBuf) {
let old_path = get_env_var("PATH");
let old_path = get_env_var("PATH");
if old_path.is_empty() {
env::set_var("PATH", dir)
} else {
Expand Down Expand Up @@ -319,10 +314,10 @@ fn wait_mount(path: &PathBuf, timeout: time::Duration) -> bool {
true
}

fn remove_mnt(ruid_dir: PathBuf, mnt_dir: PathBuf, mount_dir:PathBuf) {
let _ = remove_dir(&mount_dir);
let _ = remove_dir(&mnt_dir);
let _ = remove_dir(&ruid_dir);
fn remove_mnt(mount_dirs: Vec<&PathBuf> ) {
for dir in mount_dirs {
let _ = remove_dir(dir);
}
}

fn mount_image(embed: Embed, image: Image, mount_dir: &PathBuf) {
Expand Down Expand Up @@ -361,14 +356,24 @@ fn extract_image(embed: Embed, image: Image) {
cfg_if!{
if #[cfg(feature = "appimage")] {
let extract_dir = "AppDir";
let applink_dir = "squashfs-root";
} else {
let extract_dir = "RunDir";
}
}
if let Err(err) = create_dir_all(&extract_dir) {
if let Err(err) = create_dir(&extract_dir) {
eprintln!("Failed to create extract dir: {err}: {}", &extract_dir);
exit(1)
}
#[cfg(feature = "appimage")]
{
let _ = remove_file(&applink_dir);
if let Err(err) = symlink(&extract_dir, &applink_dir) {
eprintln!("Failed to create squashfs-root symlink to extract dir: {err}");
exit(1)
}
}

if image.is_dwar {
#[cfg(feature = "dwarfs")]
{
Expand Down Expand Up @@ -415,27 +420,30 @@ fn main() {
exit(1)
});

let uid = unsafe { getuid() };
let ruid_dir = env::temp_dir().join(format!(".r{uid}"));
let mnt_dir = ruid_dir.join("mnt");
let mount_dir = mnt_dir.join(random_string(8));

let mut exec_args: Vec<String> = env::args().collect();
let arg0 = exec_args.remove(0);

if !exec_args.is_empty() {
cfg_if!{
if #[cfg(feature = "appimage")] {
let arg_pfx = "appimage";
} else {
let arg_pfx = "runtime";
}
cfg_if!{
if #[cfg(feature = "appimage")] {
let arg_pfx = "appimage";
let mount_dir = env::temp_dir().join(random_string(8));
let mount_dirs = vec![&mount_dir];
} else {
let arg_pfx = "runtime";
let uid = unsafe { nix::libc::getuid() };
let ruid_dir = env::temp_dir().join(format!(".r{uid}"));
let mnt_dir = ruid_dir.join("mnt");
let mount_dir = mnt_dir.join(random_string(8));
let mount_dirs = vec![&mount_dir, &mnt_dir, &ruid_dir];

}
}
if !exec_args.is_empty() {
if exec_args[0] == format!("--{arg_pfx}-extract") {
extract_image(embed, image);
return
}
if exec_args[0] == format!("--{arg_pfx}-extract") {
if exec_args[0] == format!("--{arg_pfx}-mount") {
println!("{}", mount_dir.display());
mount_image(embed, image, &mount_dir);
return
Expand All @@ -445,29 +453,27 @@ fn main() {
match unsafe { fork() } {
Ok(ForkResult::Parent { child: child_pid }) => {
if !wait_mount(&mount_dir, time::Duration::from_millis(500)) {
remove_mnt(ruid_dir, mnt_dir, mount_dir);
remove_mnt(mount_dirs);
exit(1)
}

cfg_if!{
if #[cfg(feature = "appimage")] {
let run = format!("{}/AppRun", mount_dir.display());
let run_path = Path::new(&run);
if !run_path.is_file() {
eprintln!("AppRun not found: {:?}", run_path);
remove_mnt(ruid_dir, mnt_dir, mount_dir);
let run = mount_dir.join("AppRun");
if !run.is_file() {
eprintln!("AppRun not found: {:?}", run);
remove_mnt(mount_dirs);
exit(1)
};
env::set_var("ARGV0", arg0);
env::set_var("APPDIR", &mount_dir);
env::set_var("APPIMAGE", self_exe);
env::set_var("APPOFFSET", format!("{runtime_size}"));
} else {
let run = format!("{}/static/bash", mount_dir.display());
let run_path = Path::new(&run);
if !run_path.is_file() {
eprintln!("Static bash not found: {:?}", run_path);
remove_mnt(ruid_dir, mnt_dir, mount_dir);
let run = mount_dir.join("static").join("bash");
if !run.is_file() {
eprintln!("Static bash not found: {:?}", run);
remove_mnt(mount_dirs);
exit(1)
};
exec_args.insert(0, format!("{}/Run.sh", mount_dir.display()));
Expand Down Expand Up @@ -507,7 +513,7 @@ fn main() {
let _ = waitpid(child_pid, None);
signals_handle.close();

remove_mnt(ruid_dir, mnt_dir, mount_dir);
remove_mnt(mount_dirs);

exit(exit_code)
}
Expand Down
5 changes: 5 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "xtask"
version = "0.1.0"
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
edition = "2018"
Loading

0 comments on commit 613f30f

Please sign in to comment.