v1ral_ITS

Beginings of complying own single binaries

May 5th, 2026 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.50 KB | None | 0 0
  1. # Self-Extracting Binary Guide
  2.  
  3. ## What is This?
  4.  
  5. A **self-extracting binary** is a single file that:
  6. 1. Contains compressed data (your files)
  7. 2. Is executable like a program
  8. 3. Extracts itself to a temp directory
  9. 4. Runs your application
  10. 5. Cleans up when done
  11.  
  12. ## How It Works (Step by Step)
  13.  
  14. ### 1. The Archive Structure
  15.  
  16. ```
  17. +----------------------------------+
  18. |  #!/bin/sh                       |  <-- Shell script header
  19. |  (extraction code)               |
  20. +----------------------------------+
  21. |  GZIPPED DATA                    |  <-- Your files compressed
  22. |  (binaries, configs, etc.)       |
  23. +----------------------------------+
  24. |  CRC checksum                    |  <-- Integrity check
  25. +----------------------------------+
  26. ```
  27.  
  28. ### 2. Execution Flow
  29.  
  30. When user runs `./myapp.run`:
  31.  
  32. 1. **Shell interprets** the header script
  33. 2. **Extracts** compressed data to `/tmp/selfextract.XXXX/`
  34. 3. **Changes** to that directory
  35. 4. **Executes** your entrypoint script
  36. 5. **Returns** to original directory
  37. 6. **Cleans up** temp files (unless --notemp)
  38.  
  39. ## The `makeself` Command
  40.  
  41. ### Basic Syntax
  42.  
  43. ```bash
  44. makeself [options] <source_dir> <output_file> <label> <entrypoint>
  45. ```
  46.  
  47. ### Arguments Explained
  48.  
  49. | Argument | Description | Example |
  50. |----------|-------------|---------|
  51. | `source_dir` | Directory to pack | `./dist` or `.` |
  52. | `output_file` | Output filename | `./myapp.run` |
  53. | `label` | Description shown during extraction | `"My App v1.0"` |
  54. | `entrypoint` | Command to run after extract | `./start.sh` |
  55.  
  56. ### Common Options
  57.  
  58. ```bash
  59. # Compression options
  60. --gzip          # Fast compression (default)
  61. --bzip2         # Better compression
  62. --xz            # Best compression (slowest)
  63.  
  64. # Behavior options
  65. --notemp        # Don't use temp dir (extract in place)
  66. --nox11         # Disable X11 forwarding
  67. --quiet         # Minimal output
  68.  
  69. # Checksum options
  70. --sha256        # Use SHA256 instead of CRC
  71. ```
  72.  
  73. ## Examples
  74.  
  75. ### Example 1: Simple Python App
  76.  
  77. ```bash
  78. # Project structure:
  79. # myapp/
  80. # ├── main.py
  81. # └── config.json
  82.  
  83. makeself ./myapp ./myapp.run "My Python App" "python3 ./main.py"
  84. ```
  85.  
  86. ### Example 2: Complex Project with Launcher
  87.  
  88. ```bash
  89. # Project structure:
  90. # myapp/
  91. # ├── bin/
  92. # │   └── mybinary
  93. # ├── data/
  94. # │   └── assets/
  95. # └── start.sh
  96.  
  97. # Create launcher (start.sh):
  98. #!/bin/bash
  99. cd "$(dirname "$0")"
  100. ./bin/mybinary "$@"
  101.  
  102. # Build:
  103. makeself --gzip ./myapp ./myapp.run "My App v1.0" "./start.sh"
  104. ```
  105.  
  106. ### Example 3: With Dependencies
  107.  
  108. ```bash
  109. # For projects needing system deps, check first:
  110.  
  111. #!/bin/bash
  112. # launcher.sh
  113. cd "$(dirname "$0")"
  114.  
  115. # Check dependencies
  116. if ! command -v python3 &> /dev/null; then
  117.     echo "Error: python3 not installed"
  118.     exit 1
  119. fi
  120.  
  121. # Run app
  122. python3 ./main.py "$@"
  123. ```
  124.  
  125. ## Advanced Usage
  126.  
  127. ### Passing Arguments Through
  128.  
  129. Your entrypoint receives all arguments:
  130.  
  131. ```bash
  132. # start.sh
  133. #!/bin/bash
  134. cd "$(dirname "$0")"
  135. ./myapp "$@"  # Pass all args to actual app
  136. ```
  137.  
  138. Then run:
  139. ```bash
  140. ./myapp.run --help      # Passes --help to myapp
  141. ./myapp.run -v -c file  # Passes all args
  142. ```
  143.  
  144. ### Runtime Options (Built-in)
  145.  
  146. Self-extracting archives have these automatic options:
  147.  
  148. ```bash
  149. ./myapp.run --check     # Verify archive integrity
  150. ./myapp.run --list      # List files in archive
  151. ./myapp.run --target /path  # Extract to specific directory
  152. ./myapp.run --noexec    # Extract but don't run
  153. ./myapp.run --verbose   # Show extraction details
  154. ```
  155.  
  156. ### Not Extracting to Temp
  157.  
  158. Use `--notemp` to extract in current directory:
  159.  
  160. ```bash
  161. makeself --notemp ./src ./installer.run "Installer" "./install.sh"
  162. ```
  163.  
  164. This creates a hidden directory `.selfextract-XXXX` that gets cleaned up.
  165.  
  166. ## Build Scripts
  167.  
  168. ### Minimal Build Script
  169.  
  170. ```bash
  171. #!/bin/bash
  172. set -e
  173.  
  174. # Clean build
  175. rm -rf build
  176. mkdir build
  177.  
  178. # Copy files
  179. cp -r src/* build/
  180.  
  181. # Make launcher executable
  182. chmod +x build/start.sh
  183.  
  184. # Build
  185. makeself build myapp.run "My App" ./start.sh
  186.  
  187. # Clean
  188. rm -rf build
  189.  
  190. echo "Built: myapp.run"
  191. ```
  192.  
  193. ### Full-Featured Build Script
  194.  
  195. See `makeself-packer.sh` in this directory for a complete,
  196. configurable build script with:
  197. - Command-line options
  198. - Error handling
  199. - Progress logging
  200. - Compression selection
  201.  
  202. ## Compression Comparison
  203.  
  204. | Method | Speed | Size | Use Case |
  205. |--------|-------|------|----------|
  206. | gzip   | Fast  | ~70% | Default, good balance |
  207. | bzip2  | Slow  | ~60% | Smaller files |
  208. | xz     | Slowest | ~55% | Maximum compression |
  209.  
  210. Test with your data:
  211. ```bash
  212. makeself --gzip ./src ./app-gzip.run "Test" ./start.sh
  213. makeself --xz   ./src ./app-xz.run   "Test" ./start.sh
  214.  
  215. ls -lh app-*.run  # Compare sizes
  216. ```
  217.  
  218. ## Troubleshooting
  219.  
  220. ### "Permission denied" when running
  221. ```bash
  222. chmod +x myapp.run
  223. ```
  224.  
  225. ### Archive too big
  226. ```bash
  227. # Use better compression
  228. makeself --xz ./src ./app.run "App" ./start.sh
  229.  
  230. # Or exclude unnecessary files
  231. rm -rf src/tests src/docs
  232. makeself ./src ./app.run "App" ./start.sh
  233. ```
  234.  
  235. ### "Command not found" after extraction
  236. ```bash
  237. # Make sure your entrypoint exists in source
  238. # and has proper shebang (#!/bin/bash)
  239.  
  240. # Verify:
  241. head -1 start.sh
  242. # Should output: #!/bin/bash
  243. ```
  244.  
  245. ### Path issues in entrypoint
  246. ```bash
  247. # Always use this pattern in your launcher:
  248. cd "$(dirname "$(readlink -f "$0")")" || exit 1
  249.  
  250. # This works even if script is symlinked
  251. ```
  252.  
  253. ## Installation
  254.  
  255. ### For Current User
  256. ```bash
  257. cp myapp.run ~/.local/bin/myapp
  258. chmod +x ~/.local/bin/myapp
  259. # Make sure ~/.local/bin is in your $PATH
  260. ```
  261.  
  262. ### System-Wide
  263. ```bash
  264. sudo cp myapp.run /usr/local/bin/myapp
  265. sudo chmod +x /usr/local/bin/myapp
  266. ```
  267.  
  268. ### Create Desktop Entry (Linux)
  269. ```bash
  270. # ~/.local/share/applications/myapp.desktop
  271. cat > ~/.local/share/applications/myapp.desktop << EOF
  272. [Desktop Entry]
  273. Name=My Application
  274. Exec=/home/username/.local/bin/myapp
  275. Icon=/path/to/icon.png
  276. Type=Application
  277. Terminal=true
  278. EOF
  279. ```
  280.  
  281. ## How to Unpack (Reverse)
  282.  
  283. If you need to extract without running:
  284.  
  285. ```bash
  286. # Method 1: Use built-in option
  287. ./myapp.run --noexec --target ./extracted
  288.  
  289. # Method 2: Manually (skip first N lines)
  290. tail -n +$(( $(grep -n "^exit 0$" myapp.run | head -1 | cut -d: -f1) + 1 )) myapp.run > data.tar.gz
  291. gunzip data.tar.gz
  292. tar -xf data.tar
  293.  
  294. # Method 3: Run with trace to see where it extracts
  295. bash -x ./myapp.run 2>&1 | grep "selfextract"
  296. ```
  297.  
  298. ## References
  299.  
  300. - **makeself**: https://makeself.io/
  301. - **GitHub**: https://github.com/megastep/makeself
  302. - **Man page**: `man makeself`
Advertisement
Add Comment
Please, Sign In to add comment