From 2066adbd818a5f2221316974ed46f00901f81d16 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Tue, 7 Jan 2020 14:54:57 +0800 Subject: [PATCH 1/5] hostman: distro name OpenWrt instead of OpenWRT --- pkg/hostman/guestfs/fsdriver/linux.go | 4 ++-- pkg/hostman/guestman/qemu-kvmhelper.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index 39799fe968..4850aee419 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -1362,7 +1362,7 @@ func NewOpenWrtRootFs(part IDiskPartition) IRootFsDriver { } func (d *SOpenWrtRootFs) GetName() string { - return "OpenWRT" + return "OpenWrt" } func (d *SOpenWrtRootFs) String() string { @@ -1391,7 +1391,7 @@ func (d *SOpenWrtRootFs) DeployHostname(rootFs IDiskPartition, hn, domain string func (d *SOpenWrtRootFs) GetReleaseInfo(rootFs IDiskPartition) *deployapi.ReleaseInfo { ver, _ := rootFs.FileGetContents("/etc/openwrt_version", false) return &deployapi.ReleaseInfo{ - Distro: "OpenWRT", + Distro: "OpenWrt", Version: string(ver), Arch: d.GetArch(rootFs), } diff --git a/pkg/hostman/guestman/qemu-kvmhelper.go b/pkg/hostman/guestman/qemu-kvmhelper.go index d39653ac25..7604b5251c 100644 --- a/pkg/hostman/guestman/qemu-kvmhelper.go +++ b/pkg/hostman/guestman/qemu-kvmhelper.go @@ -41,7 +41,7 @@ const ( OS_NAME_ANDROID = "Android" OS_NAME_VMWARE = "VMWare" OS_NAME_CIRROS = "Cirros" - OS_NAME_OPENWRT = "OpenWRT" + OS_NAME_OPENWRT = "OpenWrt" MODE_READLINE = "readline" MODE_CONTROL = "control" From 50a5cc2570b83aa399159368cb880beea40648c4 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Tue, 7 Jan 2020 15:17:54 +0800 Subject: [PATCH 2/5] hostman: openwrt: deploy hostname with board config --- pkg/hostman/guestfs/fsdriver/linux.go | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index 4850aee419..c3145e2fc2 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -1373,7 +1373,35 @@ func (d *SOpenWrtRootFs) RootSignatures() []string { return []string{"/bin", "/etc/", "/lib", "/sbin", "/overlay", "/etc/openwrt_release", "/etc/openwrt_version"} } +func (d *SOpenWrtRootFs) featureBoardConfig(rootFs IDiskPartition) bool { + if rootFs.Exists("/etc/board.d", false) { + return true + } + return false +} + +func (d *SOpenWrtRootFs) putBoardConfig(rootFs IDiskPartition, f, c string) error { + if err := rootFs.FilePutContents(f, c, false, false); err != nil { + return err + } + if err := rootFs.Chmod(f, 0755, false); err != nil { + return err + } + return nil +} + func (d *SOpenWrtRootFs) DeployHostname(rootFs IDiskPartition, hn, domain string) error { + if d.featureBoardConfig(rootFs) { + f := "/etc/board.d/00-00-onecloud-hostname" + c := fmt.Sprintf(`. /lib/functions/uci-defaults.sh +board_config_update +ucidef_set_hostname '%s' +board_config_flush +exit 0 +`, hn) + return d.putBoardConfig(rootFs, f, c) + } + spath := "/etc/config/system" if !rootFs.Exists(spath, false) { return nil From 79f1d1d85e11ac247d6356689835f490883b98d0 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Tue, 7 Jan 2020 15:47:10 +0800 Subject: [PATCH 3/5] hostman: openwrt: support deploy networking scripts --- pkg/hostman/guestfs/fsdriver/linux.go | 45 +++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index c3145e2fc2..5b5acb00ee 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -1416,6 +1416,51 @@ exit 0 return rootFs.FilePutContents(spath, cont, false, false) } +func (d *SOpenWrtRootFs) DeployNetworkingScripts(rootFs IDiskPartition, nics []*types.SServerNic) error { + if d.featureBoardConfig(rootFs) { + macs := "" + for _, nic := range nics { + macs = "," + nic.Mac + } + f := "/etc/board.d/00-01-onecloud-network" + c := fmt.Sprintf(`. /lib/functions/uci-defaults.sh +[ -d /sys/class/net ] || exit 0 + +board_config_update +macs='%s' +i=0 + +oc_set_ifname() { + local net="$1"; shift + local ifname="$1"; shift + + if type ucidef_set_interface &>/dev/null; then + ucidef_set_interface "$net" ifname "$ifname" protocol dhcp + elif type ucidef_set_interface_raw &>/dev/null; then + ucidef_set_interface_raw "$net" "$ifname" "dhcp" + else + echo "no ucidef function to do network ifname config" >&2 + exit 0 + fi +} + +for ifname in $(ls /sys/class/net/); do + p="/sys/class/net/$ifname" + mac="$(cat "$p/address")" + if [ "${macs#*,$mac}" != "$macs" ]; then + oc_set_ifname "lan$i" "$ifname" + i="$(($i + 1))" + fi +done + +board_config_flush +exit 0 +`, macs) + return d.putBoardConfig(rootFs, f, c) + } + return nil +} + func (d *SOpenWrtRootFs) GetReleaseInfo(rootFs IDiskPartition) *deployapi.ReleaseInfo { ver, _ := rootFs.FileGetContents("/etc/openwrt_version", false) return &deployapi.ReleaseInfo{ From 8964b9a4069f96eeb26cb8381e7b167bd4ee5db3 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Tue, 7 Jan 2020 17:01:52 +0800 Subject: [PATCH 4/5] hostman: split DeployAuthorizedKeys() --- pkg/hostman/guestfs/fsdriver/base.go | 48 +++++++++++++++++----------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/pkg/hostman/guestfs/fsdriver/base.go b/pkg/hostman/guestfs/fsdriver/base.go index 51c74f9273..5897a9404b 100644 --- a/pkg/hostman/guestfs/fsdriver/base.go +++ b/pkg/hostman/guestfs/fsdriver/base.go @@ -120,41 +120,51 @@ func (l *sGuestRootFsDriver) IsResizeFsPartitionSupport() bool { return true } +const ( + modeAuthorizedKeysRWX = syscall.S_IRUSR | syscall.S_IWUSR | syscall.S_IXUSR + modeAuthorizedKeysRW = syscall.S_IRUSR | syscall.S_IWUSR +) + +func deployAuthorizedKeys(rootFs IDiskPartition, authFile string, uid, gid int, pubkeys *deployapi.SSHKeys, replace bool) error { + var oldKeys = "" + if !replace { + bOldKeys, _ := rootFs.FileGetContents(authFile, false) + oldKeys = string(bOldKeys) + } + newKeys := MergeAuthorizedKeys(oldKeys, pubkeys) + if err := rootFs.FilePutContents(authFile, newKeys, false, false); err != nil { + return fmt.Errorf("Put keys to %s: %v", authFile, err) + } + if err := rootFs.Chown(authFile, uid, gid, false); err != nil { + return fmt.Errorf("Chown %s to uid: %d, gid: %d: %v", authFile, uid, gid, err) + } + if err := rootFs.Chmod(authFile, uint32(modeAuthorizedKeysRW), false); err != nil { + return fmt.Errorf("Chmod %s to %d error: %v", authFile, uint32(modeAuthorizedKeysRW), err) + } + return nil +} + func DeployAuthorizedKeys(rootFs IDiskPartition, usrDir string, pubkeys *deployapi.SSHKeys, replace bool) error { usrStat := rootFs.Stat(usrDir, false) if usrStat != nil { sshDir := path.Join(usrDir, ".ssh") authFile := path.Join(sshDir, "authorized_keys") - modeRwxOwner := syscall.S_IRUSR | syscall.S_IWUSR | syscall.S_IXUSR - modeRwOwner := syscall.S_IRUSR | syscall.S_IWUSR fStat, _ := usrStat.Sys().(*syscall.Stat_t) + uid := int(fStat.Uid) + gid := int(fStat.Gid) if !rootFs.Exists(sshDir, false) { - err := rootFs.Mkdir(sshDir, modeRwxOwner, false) + err := rootFs.Mkdir(sshDir, modeAuthorizedKeysRWX, false) if err != nil { log.Errorln(err) return err } - err = rootFs.Chown(sshDir, int(fStat.Uid), int(fStat.Gid), false) + err = rootFs.Chown(sshDir, uid, gid, false) if err != nil { log.Errorln(err) return err } } - var oldKeys = "" - if !replace { - bOldKeys, _ := rootFs.FileGetContents(authFile, false) - oldKeys = string(bOldKeys) - } - newKeys := MergeAuthorizedKeys(oldKeys, pubkeys) - if err := rootFs.FilePutContents(authFile, newKeys, false, false); err != nil { - return fmt.Errorf("Put keys to %s: %v", authFile, err) - } - if err := rootFs.Chown(authFile, int(fStat.Uid), int(fStat.Gid), false); err != nil { - return fmt.Errorf("Chown %s to uid: %d, gid: %d: %v", authFile, fStat.Uid, fStat.Gid, err) - } - if err := rootFs.Chmod(authFile, uint32(modeRwOwner), false); err != nil { - return fmt.Errorf("Chmod %s to %d error: %v", authFile, uint32(modeRwOwner), err) - } + return deployAuthorizedKeys(rootFs, authFile, uid, gid, pubkeys, replace) } return nil } From b90a1e8013c62553978e687e47ad0b608fe1fc82 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Tue, 7 Jan 2020 17:05:45 +0800 Subject: [PATCH 5/5] hostman: openwrt: deploy public key for root --- pkg/hostman/guestfs/fsdriver/linux.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index 5b5acb00ee..a417ea903e 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -1372,7 +1372,6 @@ func (d *SOpenWrtRootFs) String() string { func (d *SOpenWrtRootFs) RootSignatures() []string { return []string{"/bin", "/etc/", "/lib", "/sbin", "/overlay", "/etc/openwrt_release", "/etc/openwrt_version"} } - func (d *SOpenWrtRootFs) featureBoardConfig(rootFs IDiskPartition) bool { if rootFs.Exists("/etc/board.d", false) { return true @@ -1390,6 +1389,19 @@ func (d *SOpenWrtRootFs) putBoardConfig(rootFs IDiskPartition, f, c string) erro return nil } +func (d *SOpenWrtRootFs) DeployPublicKey(rootFs IDiskPartition, selUsr string, pubkeys *deployapi.SSHKeys) error { + if selUsr == "root" && rootFs.Exists("/etc/dropbear", false) { + var ( + authFile = "/etc/dropbear/authorized_keys" + uid = 0 + gid = 0 + replace = false + ) + return deployAuthorizedKeys(rootFs, authFile, uid, gid, pubkeys, replace) + } + return d.sLinuxRootFs.DeployPublicKey(rootFs, selUsr, pubkeys) +} + func (d *SOpenWrtRootFs) DeployHostname(rootFs IDiskPartition, hn, domain string) error { if d.featureBoardConfig(rootFs) { f := "/etc/board.d/00-00-onecloud-hostname"