mirror of
https://github.com/siderolabs/talos.git
synced 2025-08-30 11:01:12 +02:00
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package rootfs
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/constants"
|
|
"github.com/autonomy/dianemo/initramfs/cmd/init/pkg/etc"
|
|
"github.com/autonomy/dianemo/initramfs/pkg/userdata"
|
|
yaml "gopkg.in/yaml.v2"
|
|
)
|
|
|
|
func ip() string {
|
|
addrs, err := net.InterfaceAddrs()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
for _, address := range addrs {
|
|
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
if ipnet.IP.To4() != nil {
|
|
return ipnet.IP.String()
|
|
}
|
|
}
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
// Prepare creates the files required by the installed binaries and libraries.
|
|
func Prepare(s string, userdata userdata.UserData) (err error) {
|
|
// Create /etc/hosts.
|
|
hostname, err := os.Hostname()
|
|
if err != nil {
|
|
return
|
|
}
|
|
ip := ip()
|
|
if err = etc.Hosts(s, hostname, ip); err != nil {
|
|
return
|
|
}
|
|
// Create /etc/resolv.conf.
|
|
if err = etc.ResolvConf(s); err != nil {
|
|
return
|
|
}
|
|
// Create /etc/os-release.
|
|
if err = etc.OSRelease(s); err != nil {
|
|
return
|
|
}
|
|
// Save the user data to disk.
|
|
data, err := yaml.Marshal(&userdata)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err = ioutil.WriteFile(path.Join(constants.NewRoot, constants.UserDataPath), data, 0400); err != nil {
|
|
return
|
|
}
|
|
|
|
return nil
|
|
}
|