Generate resolv.conf, copy localtime

This commit is contained in:
yo 2022-07-10 20:48:24 +02:00
parent e1f6b4f6f9
commit 0c7293ae66

View File

@ -1,19 +1,21 @@
package cmd
import (
"errors"
"fmt"
"github.com/c-robinson/iplib"
log "github.com/sirupsen/logrus"
"net"
"os"
"fmt"
"net"
"errors"
"regexp"
"reflect"
"strconv"
"strings"
"io/ioutil"
"crypto/rand"
"gocage/jail"
"encoding/hex"
"github.com/c-robinson/iplib"
log "github.com/sirupsen/logrus"
)
func mountProcFs(jail *Jail) error {
@ -998,6 +1000,48 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
return nil
}
func generateResolvConf(jail *Jail) error {
if jail.Config.Resolver != "/etc/resolv.conf" && jail.Config.Resolver != "none" && jail.Config.Resolver != "/dev/null" {
f, err := os.Create(fmt.Sprintf("%s/etc/resolv.conf", jail.RootPath))
if err != nil {
return err
}
defer f.Close()
for _, l := range strings.Split(jail.Config.Resolver, ";") {
f.WriteString(fmt.Sprintf("%s\n", l))
}
} else if jail.Config.Resolver == "none" {
read, err := ioutil.ReadFile("/etc/resolv.conf")
if err != nil {
return fmt.Errorf("Error opening /etc/resolv.conf: %v", err)
}
err = ioutil.WriteFile(fmt.Sprintf("%s/etc/resolv.conf", jail.RootPath), read, 0644)
if err != nil {
return fmt.Errorf("Error writing to %s/etc/resolv.conf: %v", jail.RootPath, err)
}
} else if jail.Config.Resolver == "/dev/null" {
// Just do nothing
}
return nil
}
func copyLocalTime(jail *Jail) error {
if jail.Config.Host_time > 0 {
read, err := ioutil.ReadFile("/etc/localtime")
if err != nil {
return fmt.Errorf("Error opening /etc/localtime: %v", err)
}
err = ioutil.WriteFile(fmt.Sprintf("%s/etc/localtime", jail.RootPath), read, 0644)
if err != nil {
return fmt.Errorf("Error writing to %s/etc/localtime: %v", jail.RootPath, err)
}
}
return nil
}
// TODO: Umount all FS
// TODO: Delete devfs ruleset
// TODO: Remove /var/run/jail-${internalname}.conf
@ -1304,7 +1348,7 @@ func StartJail(args []string) {
fmt.Printf(" > Setup default ipv4 gateway:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter)
out, err := executeCommand(cmd)
if err == nil && len(out) > 0 {
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
} else {
fmt.Printf(" > Setup default ipv4 gateway: OK\n")
@ -1314,30 +1358,43 @@ func StartJail(args []string) {
fmt.Printf(" > Setup default ipv6 gateway:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add -6 default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter6)
out, err := executeCommand(cmd)
if err == nil && len(out) > 0 {
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
} else {
fmt.Printf(" > Setup default ipv6 gateway: OK\n")
}
}
err = jailZfsDatasets(cj)
if err != nil {
fmt.Printf("%v\n", err)
return
if cj.Config.Jail_zfs > 0 {
fmt.Printf(" > Jail ZFS datasets:\n")
err = jailZfsDatasets(cj)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Printf(" > Jail ZFS datasets: OK\n")
}
// WIP 26/06/2022 : https://github.com/iocage/iocage/blob/master/iocage_lib/ioc_start.py#L792
// TODO
//generateResolvConf(cj)
err = generateResolvConf(cj)
if err != nil {
fmt.Printf("%s\n", err)
}
if cj.Config.Host_time > 0 {
err = copyLocalTime(cj)
if err != nil {
fmt.Printf("%s\n", err)
}
}
// Start services
if len(cj.Config.Exec_start) > 0 {
fmt.Printf(" > Start services:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d %s", cj.Config.Exec_fib, cj.JID, cj.Config.Exec_start)
out, err := executeCommand(cmd)
if err == nil && len(out) > 0 {
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
} else {
fmt.Printf(" > Start services: OK\n")
@ -1346,6 +1403,18 @@ func StartJail(args []string) {
// TODO: Execute Exec_poststart
if len(cj.Config.Exec_poststart) > 0 {
fmt.Printf(" > Execute post-start:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d %s", cj.Config.Exec_fib, cj.JID, cj.Config.Exec_poststart)
out, err := executeCommand(cmd)
if err != nil && len(out) > 0 {
fmt.Printf("Error: %v: %s\n", err, out)
} else {
fmt.Printf(" > Execute post-start: OK\n")
}
}
/*
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))