mirror of https://github.com/nocturn9x/nimd.git
Minor changes and additions
This commit is contained in:
parent
6283c82391
commit
7b7b675fef
|
@ -107,9 +107,9 @@ then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
qemumem=''
|
qemumem=''
|
||||||
if ! [[ $memory -eq "" ]]
|
if ! [[ ${memory::-1} -eq "" ]]
|
||||||
then
|
then
|
||||||
qemumem="-m ${memory}"
|
qemumem="-m ${memory}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
qemu-system-x86_64 -net nic,model=virtio,netdev=user.0 -netdev user,id=user.0 -drive file=vm.qcow2,if=virtio -enable-kvm -kernel ${kernel} -initrd ${initrd} ${qemumem} -append 'root=/dev/vda1 rw quiet modules=ext4 console=ttyS0 init=/bin/nimd'
|
qemu-system-x86_64 -net nic,model=virtio,netdev=user.0 -netdev user,id=user.0 -drive file=vm.qcow2,if=virtio -enable-kvm -kernel ${kernel} -initrd ${initrd} ${qemumem} -append 'root=/dev/vda1 rw quiet modules=ext4 init=/sbin/nimd console=ttyS0 -- -X'
|
||||||
|
|
|
@ -1 +1,18 @@
|
||||||
./scripts/boot.sh --kernel vmlinuz-linux --initrd initrd-linux.img --memory 1G
|
trap 'catch' ERR
|
||||||
|
|
||||||
|
catch() {
|
||||||
|
echo "Build failed"
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
echo "Compiling NimD"
|
||||||
|
nim -d:release -d:danger --passC:"-O3 -flto" --opt:size --hints:off --gc:orc --passL:"-static" -o:rootfs/sbin/nimd c src/main
|
||||||
|
echo "Compiling NimD subprograms"
|
||||||
|
nim -d:release -d:danger --passC:"-O3 -flto" --opt:size --hints:off --gc:orc --passL:"-static" -o:rootfs/sbin/poweroff c src/programs/poweroff
|
||||||
|
nim -d:release -d:danger --passC:"-O3 -flto" --opt:size --hints:off --gc:orc --passL:"-static" -o:rootfs/sbin/halt c src/programs/halt
|
||||||
|
nim -d:release -d:danger --passC:"-O3 -flto" --opt:size --hints:off --gc:orc --passL:"-static" -o:rootfs/sbin/reboot c src/programs/reboot
|
||||||
|
nim -d:release -d:danger --passC:"-O3 -flto" --opt:size --hints:off --gc:orc --passL:"-static" -o:rootfs/sbin/nimd-reload c src/programs/reload
|
||||||
|
echo "Setting up directory structure"
|
||||||
|
mkdir -p rootfs/etc/nimd
|
||||||
|
cp nimd.conf rootfs/etc/nimd
|
||||||
|
echo "Building and starting VM"
|
||||||
|
./boot.sh --memory 1G --kernel vmlinuz-linux --initrd initramfs-linux.img --build
|
|
@ -13,24 +13,31 @@
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
import os
|
import os
|
||||||
import net
|
import net
|
||||||
|
import posix
|
||||||
import strformat
|
import strformat
|
||||||
import shutdown
|
import shutdown
|
||||||
|
|
||||||
|
|
||||||
import ../util/logging
|
import ../util/logging
|
||||||
import ../util/misc
|
|
||||||
|
|
||||||
|
|
||||||
proc initControlSocket*(logger: Logger, path: string = "/var/run/nimd.sock"): Socket =
|
proc initControlSocket*(logger: Logger, path: string = "/var/run/nimd.sock"): Socket =
|
||||||
## Initializes NimD's control socket (an unbuffered
|
## Initializes NimD's control socket (an unbuffered
|
||||||
## TCP Unix Domain Socket) binding it to the given
|
## TCP Unix Domain Socket) binding it to the given
|
||||||
## path (defaults to /var/run/nimd.sock)
|
## path (defaults to /var/run/nimd.sock). The socket's
|
||||||
|
## permissions are set to 700 so that only root can read
|
||||||
|
## from it
|
||||||
try:
|
try:
|
||||||
logger.info(&"Initializing control socket at '{path}'")
|
logger.info(&"Initializing control socket at '{path}'")
|
||||||
if exists(path):
|
if fileExists(path):
|
||||||
removeFile(path)
|
removeFile(path)
|
||||||
result = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP, buffered=false)
|
elif dirExists(path):
|
||||||
|
removeDir(path)
|
||||||
|
result = newSocket(net.AF_UNIX, net.SOCK_STREAM, net.IPPROTO_IP, buffered=false)
|
||||||
bindUnix(result, path)
|
bindUnix(result, path)
|
||||||
|
if posix.chmod(cstring(splitPath(path).head), 700) == -1:
|
||||||
|
logger.error(&"Could not restrict access to unix socket at '{path}: {posix.strerror(posix.errno)}'")
|
||||||
|
nimDExit(logger, code=int(posix.errno))
|
||||||
except OSError:
|
except OSError:
|
||||||
logger.error(&"Error when binding unix socket at '{path}': {getCurrentExceptionMsg()}")
|
logger.error(&"Error when binding unix socket at '{path}': {getCurrentExceptionMsg()}")
|
||||||
nimDExit(logger, code=int(osLastError()))
|
nimDExit(logger, code=int(osLastError()))
|
|
@ -17,25 +17,10 @@ import strformat
|
||||||
import posix
|
import posix
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import ../util/[logging, misc]
|
import ../util/[logging, misc, cffi]
|
||||||
import shutdown
|
import shutdown
|
||||||
|
|
||||||
|
|
||||||
# Nim wrappers around C functionality in sys/mount.h on Linux
|
|
||||||
proc mount*(source: cstring, target: cstring, fstype: cstring,
|
|
||||||
mountflags: culong, data: pointer): cint {.header: "sys/mount.h", importc.}
|
|
||||||
# Since cstrings are weak references, we need to convert nim strings to cstrings only
|
|
||||||
# when we're ready to use them and only when we're sure the underlying nim string is
|
|
||||||
# in scope, otherwise garbage collection madness happens
|
|
||||||
proc mount*(source, target, fstype: string, mountflags: uint64, data: string): int = int(mount(cstring(source), cstring(target), cstring(fstype), culong(mountflags), cstring(data)))
|
|
||||||
|
|
||||||
proc umount*(target: cstring): cint {.header: "sys/mount.h", importc.}
|
|
||||||
proc umount2*(target: cstring, flags: cint): cint {.header: "sys/mount.h", importc.}
|
|
||||||
# These 2 wrappers silent the CStringConv warning
|
|
||||||
# (implicit conversion to 'cstring' from a non-const location)
|
|
||||||
proc umount*(target: string): int = int(umount(cstring(target)))
|
|
||||||
proc umount2*(target: string, flags: int): int = int(umount2(cstring(target), cint(flags)))
|
|
||||||
|
|
||||||
|
|
||||||
## Our Nim API
|
## Our Nim API
|
||||||
type
|
type
|
||||||
|
|
|
@ -22,10 +22,9 @@ import shlex
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
proc strsignal(sig: cint): cstring {.header: "string.h", importc.}
|
|
||||||
|
|
||||||
|
|
||||||
import ../util/logging
|
import ../util/logging
|
||||||
|
import ../util/cffi
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
@ -18,7 +18,7 @@ import net
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# NimD's own stuff
|
# NimD's own stuff
|
||||||
import util/[logging, constants, misc, config]
|
import util/[logging, constants, misc, config, cffi]
|
||||||
import core/[mainloop, fs, shutdown, services]
|
import core/[mainloop, fs, shutdown, services]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,9 @@
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
import os
|
||||||
import net
|
import net
|
||||||
|
import strformat
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
@ -19,7 +21,10 @@ when isMainModule:
|
||||||
try:
|
try:
|
||||||
sock.connectUnix("/var/run/nimd.sock")
|
sock.connectUnix("/var/run/nimd.sock")
|
||||||
except OSError:
|
except OSError:
|
||||||
echo getCurrentExceptionMsg()
|
echo &"Communication with NimD control socket failed: {osErrorMsg(osLastError())}"
|
||||||
quit(-1)
|
quit(int(osLastError()))
|
||||||
echo sock.trySend("r")
|
if sock.trySend("h"):
|
||||||
|
echo "Halting"
|
||||||
|
else:
|
||||||
|
echo &"Communication with NimD control socket failed: {osErrorMsg(osLastError())}"
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
|
@ -11,7 +11,9 @@
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
import os
|
||||||
import net
|
import net
|
||||||
|
import strformat
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
@ -19,7 +21,10 @@ when isMainModule:
|
||||||
try:
|
try:
|
||||||
sock.connectUnix("/var/run/nimd.sock")
|
sock.connectUnix("/var/run/nimd.sock")
|
||||||
except OSError:
|
except OSError:
|
||||||
echo getCurrentExceptionMsg()
|
echo &"Communication with NimD control socket failed: {osErrorMsg(osLastError())}"
|
||||||
quit(-1)
|
quit(int(osLastError()))
|
||||||
echo sock.trySend("p")
|
if sock.trySend("p"):
|
||||||
|
echo "Shutting down"
|
||||||
|
else:
|
||||||
|
echo &"Communication with NimD control socket failed: {osErrorMsg(osLastError())}"
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
|
@ -11,7 +11,9 @@
|
||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
import os
|
||||||
import net
|
import net
|
||||||
|
import strformat
|
||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
@ -19,7 +21,10 @@ when isMainModule:
|
||||||
try:
|
try:
|
||||||
sock.connectUnix("/var/run/nimd.sock")
|
sock.connectUnix("/var/run/nimd.sock")
|
||||||
except OSError:
|
except OSError:
|
||||||
echo getCurrentExceptionMsg()
|
echo &"Communication with NimD control socket failed: {osErrorMsg(osLastError())}"
|
||||||
quit(-1)
|
quit(int(osLastError()))
|
||||||
echo sock.trySend("r")
|
if sock.trySend("r"):
|
||||||
|
echo "Rebooting"
|
||||||
|
else:
|
||||||
|
echo &"Communication with NimD control socket failed: {osErrorMsg(osLastError())}"
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
# Copyright 2021 Mattia Giambirtone & All Contributors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
import os
|
||||||
|
import net
|
||||||
|
import strformat
|
||||||
|
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
var sock = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)
|
||||||
|
try:
|
||||||
|
sock.connectUnix("/var/run/nimd.sock")
|
||||||
|
except OSError:
|
||||||
|
echo &"Communication with NimD control socket failed: {osErrorMsg(osLastError())}"
|
||||||
|
quit(int(osLastError()))
|
||||||
|
if sock.trySend("l"):
|
||||||
|
echo "Reloading"
|
||||||
|
else:
|
||||||
|
echo &"Communication with NimD control socket failed: {osErrorMsg(osLastError())}"
|
||||||
|
sock.close()
|
|
@ -0,0 +1,30 @@
|
||||||
|
# Copyright 2021 Mattia Giambirtone & All Contributors
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
proc strsignal*(sig: cint): cstring {.header: "string.h", importc.}
|
||||||
|
proc dup3*(a1, a2, a3: cint): cint {.importc.}
|
||||||
|
# Nim wrappers around C functionality in sys/mount.h on Linux
|
||||||
|
proc mount*(source: cstring, target: cstring, fstype: cstring,
|
||||||
|
mountflags: culong, data: pointer): cint {.header: "sys/mount.h", importc.}
|
||||||
|
# Since cstrings are weak references, we need to convert nim strings to cstrings only
|
||||||
|
# when we're ready to use them and only when we're sure the underlying nim string is
|
||||||
|
# in scope, otherwise garbage collection madness happens
|
||||||
|
proc mount*(source, target, fstype: string, mountflags: uint64, data: string): int = int(mount(cstring(source), cstring(target), cstring(fstype), culong(mountflags), cstring(data)))
|
||||||
|
|
||||||
|
proc umount*(target: cstring): cint {.header: "sys/mount.h", importc.}
|
||||||
|
proc umount2*(target: cstring, flags: cint): cint {.header: "sys/mount.h", importc.}
|
||||||
|
# These 2 wrappers silent the CStringConv warning
|
||||||
|
# (implicit conversion to 'cstring' from a non-const location)
|
||||||
|
proc umount*(target: string): int = int(umount(cstring(target)))
|
||||||
|
proc umount2*(target: string, flags: int): int = int(umount2(cstring(target), cint(flags)))
|
|
@ -19,6 +19,8 @@ import posix
|
||||||
import times
|
import times
|
||||||
|
|
||||||
|
|
||||||
|
import cffi
|
||||||
|
|
||||||
type
|
type
|
||||||
LogLevel* = enum
|
LogLevel* = enum
|
||||||
Trace = 0,
|
Trace = 0,
|
||||||
|
@ -39,9 +41,6 @@ type
|
||||||
handlers*: seq[LogHandler]
|
handlers*: seq[LogHandler]
|
||||||
|
|
||||||
|
|
||||||
proc dup3(a1, a2, a3: cint): cint {.importc.}
|
|
||||||
|
|
||||||
|
|
||||||
var defaultLevel = LogLevel.Info
|
var defaultLevel = LogLevel.Info
|
||||||
var logFile = "/var/log/nimd"
|
var logFile = "/var/log/nimd"
|
||||||
var logToFileOnly: bool = false
|
var logToFileOnly: bool = false
|
||||||
|
@ -215,7 +214,7 @@ proc switchToConsole*(self: Logger) =
|
||||||
self.addHandler(createHandler(logFatalStderr, LogLevel.Fatal))
|
self.addHandler(createHandler(logFatalStderr, LogLevel.Fatal))
|
||||||
|
|
||||||
|
|
||||||
proc getDefaultLogger*(): Logger =
|
proc getDefaultLogger*: Logger =
|
||||||
## Gets a simple logger with level set
|
## Gets a simple logger with level set
|
||||||
## to LogLevel.Info and one handler per
|
## to LogLevel.Info and one handler per
|
||||||
## level that writes the given message to the
|
## level that writes the given message to the
|
||||||
|
|
|
@ -26,7 +26,6 @@ import ../core/shutdown
|
||||||
|
|
||||||
|
|
||||||
proc sleepSeconds*(amount: SomeNumber) = sleep(int(amount * 1000))
|
proc sleepSeconds*(amount: SomeNumber) = sleep(int(amount * 1000))
|
||||||
proc strsignal*(sig: cint): cstring {.header: "string.h", importc.}
|
|
||||||
proc dummySigHandler(x: cint) {.noconv.} = discard
|
proc dummySigHandler(x: cint) {.noconv.} = discard
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,15 +79,6 @@ proc reapProcess*(logger: Logger) =
|
||||||
logger.trace(&"Call to waitpid() set status to {status} and returned {returnCode}")
|
logger.trace(&"Call to waitpid() set status to {status} and returned {returnCode}")
|
||||||
|
|
||||||
|
|
||||||
proc exists*(p: string): bool =
|
|
||||||
## Returns true if a path exists,
|
|
||||||
## false otherwise
|
|
||||||
try:
|
|
||||||
discard getFileInfo(p)
|
|
||||||
result = true
|
|
||||||
except OSError:
|
|
||||||
result = false
|
|
||||||
|
|
||||||
|
|
||||||
proc setHostname*(logger: Logger): string =
|
proc setHostname*(logger: Logger): string =
|
||||||
## Sets the machine's hostname. Returns
|
## Sets the machine's hostname. Returns
|
||||||
|
@ -108,3 +98,13 @@ proc setHostname*(logger: Logger): string =
|
||||||
logger.error(&"An error occurred while setting hostname -> {getCurrentExceptionMsg()}")
|
logger.error(&"An error occurred while setting hostname -> {getCurrentExceptionMsg()}")
|
||||||
return ""
|
return ""
|
||||||
return hostname
|
return hostname
|
||||||
|
|
||||||
|
|
||||||
|
proc exists*(p: string): bool =
|
||||||
|
## Returns true if a path exists,
|
||||||
|
## false otherwise
|
||||||
|
try:
|
||||||
|
discard getFileInfo(p)
|
||||||
|
result = true
|
||||||
|
except OSError:
|
||||||
|
result = false
|
Loading…
Reference in New Issue