Skip to content

Commit fef30fe

Browse files
committed
feat: initial commit
0 parents  commit fef30fe

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dive

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Madonuko, Ultramarine Project, Fyra Labs, Et al.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# dive
2+
3+
A chroot utility (just like `arch-chroot`).

dive.nimble

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Package
2+
3+
version = "0.1.0"
4+
author = "madomado"
5+
description = "A chroot utility"
6+
license = "MIT"
7+
srcDir = "src"
8+
bin = @["dive"]
9+
10+
11+
# Dependencies
12+
13+
requires "nim >= 2.1.1"
14+
requires "cligen"
15+
requires "sweet"

src/dive.nim

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import std/[strutils, strformat, paths, os, dirs, files, osproc]
2+
import asyncdispatch
3+
import cligen, sweet
4+
import log, mounts
5+
6+
let mounteds = getMounts()
7+
8+
proc is_mountpoint(root: Path): bool =
9+
for mount in mounteds:
10+
if root == mount.mountpoint.Path:
11+
return true
12+
13+
proc mkdir(dir: Path) =
14+
try: dir.createDir
15+
except OSError:
16+
error "Cannot create dir: " & dir.string
17+
quit 1
18+
19+
proc run(cmd: string) {.async.} =
20+
info "Running command: "&cmd
21+
let p = startProcess("sh", args=["-c", cmd], options={poParentStreams})
22+
while p.running:
23+
await sleepAsync 10
24+
let rc = p.waitForExit
25+
if !!rc:
26+
fatal "Fail to execute command: "&cmd
27+
fatal "Command returned exit code: " & $rc
28+
quit 1
29+
30+
proc force_mountpoint(root: Path) {.async.} =
31+
if root.is_mountpoint: return
32+
warn fmt"{root.string} is not a mountpoint, bind-mounting…"
33+
await run fmt"mount --bind {root.string.quoteShell} {root.string.quoteShell}"
34+
35+
proc mount(path: Path, mountargs: string) {.async.} =
36+
if !path.is_mountpoint:
37+
mkdir path
38+
await run "mount "&mountargs&" "&path.string
39+
40+
proc mount_dirs(root: Path) {.async.} =
41+
await mount(root/"proc".Path, "-t proc proc") and
42+
mount(root/"sys".Path, "-t sysfs sys") and
43+
mount(root/"dev".Path, "-o bind /dev") and
44+
mount(root/"dev/pts".Path, "-o bind /dev/pts")
45+
46+
proc cp_resolv(root: Path) {.async.} =
47+
if !"/etc/resolv.conf".Path.fileExists:
48+
warn "/etc/resolv.conf does not exist"
49+
return
50+
let dest = root/"etc/resolv.conf".Path
51+
if !dest.fileExists:
52+
warn "Refusing to copy resolv.conf because it doesn't exist inside chroot"
53+
return
54+
await run "mount -c --bind /etc/resolv.conf "&dest.string
55+
56+
proc umount(path: string) {.async.} =
57+
try: await run fmt"umount {path}"
58+
finally: discard
59+
60+
proc umount_all(root: string) {.async.} =
61+
await umount(fmt"{root}/proc") and
62+
umount(fmt"{root}/sys") and
63+
umount(fmt"{root}/dev {root}/dev/pts") and
64+
umount(fmt"{root}/etc/resolv.conf")
65+
66+
proc find_shell(root: Path): string =
67+
if fileExists root/"bin/fish".Path:
68+
return "/bin/fish"
69+
if fileExists root/"bin/zsh".Path:
70+
return "/bin/zsh"
71+
if fileExists root/"bin/bash".Path:
72+
return "/bin/bash"
73+
if fileExists root/"bin/sh".Path:
74+
return "/bin/sh"
75+
warn "Cannot detect any shell in the chroot… falling back to /bin/sh"
76+
"/bin/sh"
77+
78+
proc dive(args: seq[string], verbosity = lvlNotice, keepresolv = false) =
79+
## A chroot utility
80+
if !args.len:
81+
fatal "You must provide an argument for the root directory"
82+
quit 1
83+
let root = args[0].Path
84+
let cp_resolv_fut = cp_resolv root
85+
waitFor (force_mountpoint root) and (mount_dirs root) and cp_resolv_fut
86+
waitFor cp_resolv_fut
87+
let shell = find_shell root
88+
let str_args = args[1..^1].join(" ")
89+
waitFor run fmt"SHELL={shell} chroot {root.string} {str_args}"
90+
waitFor umount_all(root.string)
91+
92+
93+
dispatch dive, help = {
94+
"args": "<root directory> [shell command]",
95+
"verbosity": "set the logging verbosity: {lvlAll, lvlDebug, lvlInfo, lvlNotice, lvlWarn, lvlError, lvlFatal, lvlNone}",
96+
}, short = {"keepresolv": 'r'}

src/log.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import std/logging
2+
3+
var logger = newConsoleLogger(fmtStr="mkfstab: $levelname: ", useStderr=true)
4+
addHandler logger
5+
6+
export debug, error, fatal, info, log, notice, warn, logging

src/mounts.nim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import std/[streams, strformat, strscans]
2+
import log
3+
4+
type Mount* = tuple[device: string, mountpoint: string, fstype: string, mountopts: string]
5+
6+
proc notSpace(input: string, match: var string, start: int): int =
7+
result = 0
8+
while input[start+result] != ' ': inc result
9+
match = input[start..start+result-1]
10+
11+
12+
proc getMounts*(): seq[Mount] =
13+
var fdmounts = newFileStream("/proc/mounts")
14+
if fdmounts == nil:
15+
# cannot open file
16+
return @[]
17+
defer: fdmounts.close()
18+
var linenum = 0
19+
var line, dev, mp, fstype, mountopts: string
20+
while fdmounts.readLine line:
21+
inc linenum
22+
if scanf(line, "${notSpace} ${notSpace} ${notSpace} ${notSpace} 0 0$.", dev, mp, fstype, mountopts):
23+
result.add (device: dev, mountpoint: mp, fstype: fstype, mountopts: mountopts)
24+
else:
25+
error fmt"Fail to parse /proc/mounts:{linenum} : {line}"

0 commit comments

Comments
 (0)