From 33119d2b8e4b48367421ed8e66aa4b11e639b2ac Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 23 Jun 2021 21:53:20 +0300 Subject: [PATCH] chore: add an option to launch cluster with bad RTC state This is useful for time sync testing. Signed-off-by: Andrey Smirnov --- cmd/talosctl/cmd/mgmt/cluster/create.go | 4 ++++ pkg/provision/providers/qemu/launch.go | 18 ++++++++++++++++-- pkg/provision/providers/qemu/node.go | 1 + pkg/provision/request.go | 5 +++++ website/content/docs/v0.11/Reference/cli.md | 1 + 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/talosctl/cmd/mgmt/cluster/create.go b/cmd/talosctl/cmd/mgmt/cluster/create.go index 6c910d006d..fd089a5e7f 100644 --- a/cmd/talosctl/cmd/mgmt/cluster/create.go +++ b/cmd/talosctl/cmd/mgmt/cluster/create.go @@ -107,6 +107,7 @@ var ( configPatch string configPatchControlPlane string configPatchJoin string + badRTC bool ) // createCmd represents the cluster up command. @@ -491,6 +492,7 @@ func create(ctx context.Context) (err error) { NanoCPUs: nanoCPUs, Disks: disks, SkipInjectingConfig: skipInjectingConfig, + BadRTC: badRTC, } if i == 0 { @@ -542,6 +544,7 @@ func create(ctx context.Context) (err error) { Disks: disks, Config: cfg, SkipInjectingConfig: skipInjectingConfig, + BadRTC: badRTC, }) } @@ -809,5 +812,6 @@ func init() { createCmd.Flags().StringVar(&configPatch, "config-patch", "", "patch generated machineconfigs (applied to all node types)") createCmd.Flags().StringVar(&configPatchControlPlane, "config-patch-control-plane", "", "patch generated machineconfigs (applied to 'init' and 'controlplane' types)") createCmd.Flags().StringVar(&configPatchJoin, "config-patch-join", "", "patch generated machineconfigs (applied to 'join' type)") + createCmd.Flags().BoolVar(&badRTC, "bad-rtc", false, "launch VM with bad RTC state (QEMU only)") Cmd.AddCommand(createCmd) } diff --git a/pkg/provision/providers/qemu/launch.go b/pkg/provision/providers/qemu/launch.go index 0a9973fb76..6eb1f5a431 100644 --- a/pkg/provision/providers/qemu/launch.go +++ b/pkg/provision/providers/qemu/launch.go @@ -48,6 +48,7 @@ type LaunchConfig struct { EnableKVM bool BootloaderEnabled bool NodeUUID uuid.UUID + BadRTC bool // Talos config Config string @@ -219,7 +220,7 @@ func checkPartitions(config *LaunchConfig) (bool, error) { // launchVM runs qemu with args built based on config. // -//nolint:gocyclo +//nolint:gocyclo,cyclop func launchVM(config *LaunchConfig) error { bootOrder := config.DefaultBootOrder @@ -227,10 +228,16 @@ func launchVM(config *LaunchConfig) error { bootOrder = "nc" } + cpuArg := "max" + + if config.BadRTC { + cpuArg += ",-kvmclock" + } + args := []string{ "-m", strconv.FormatInt(config.MemSize, 10), "-smp", fmt.Sprintf("cpus=%d", config.VCPUCount), - "-cpu", "max", + "-cpu", cpuArg, "-nographic", "-netdev", fmt.Sprintf("tap,id=net0,ifname=%s,script=no,downscript=no", config.tapName), "-device", fmt.Sprintf("virtio-net-pci,netdev=net0,mac=%s", config.vmMAC), @@ -284,6 +291,13 @@ func launchVM(config *LaunchConfig) error { } } + if config.BadRTC { + args = append(args, + "-rtc", + "base=2011-11-11T11:11:00,clock=rt", + ) + } + fmt.Fprintf(os.Stderr, "starting %s with args:\n%s\n", config.QemuExecutable, strings.Join(args, " ")) cmd := exec.Command( config.QemuExecutable, diff --git a/pkg/provision/providers/qemu/node.go b/pkg/provision/providers/qemu/node.go index c1cab532df..0c3e6ac108 100644 --- a/pkg/provision/providers/qemu/node.go +++ b/pkg/provision/providers/qemu/node.go @@ -115,6 +115,7 @@ func (p *provisioner) createNode(state *vm.State, clusterReq provision.ClusterRe PFlashImages: pflashImages, MonitorPath: state.GetRelativePath(fmt.Sprintf("%s.monitor", nodeReq.Name)), EnableKVM: opts.TargetArch == runtime.GOARCH, + BadRTC: nodeReq.BadRTC, DefaultBootOrder: defaultBootOrder, BootloaderEnabled: opts.BootloaderEnabled, NodeUUID: nodeUUID, diff --git a/pkg/provision/request.go b/pkg/provision/request.go index ff64731376..5c681b5a92 100644 --- a/pkg/provision/request.go +++ b/pkg/provision/request.go @@ -148,6 +148,11 @@ type NodeRequest struct { // BootOrder can be forced to be "nc" (PXE boot) via the API in QEMU provisioner. DefaultBootOrder string + // Testing features + + // BadRTC resets RTC to well known time in the past (QEMU provisioner). + BadRTC bool + // PXE-booted VMs PXEBooted bool TFTPServer string diff --git a/website/content/docs/v0.11/Reference/cli.md b/website/content/docs/v0.11/Reference/cli.md index 5f313f1553..e4ce050852 100644 --- a/website/content/docs/v0.11/Reference/cli.md +++ b/website/content/docs/v0.11/Reference/cli.md @@ -90,6 +90,7 @@ talosctl cluster create [flags] ``` --arch string cluster architecture (default "amd64") + --bad-rtc launch VM with bad RTC state (QEMU only) --cidr string CIDR of the cluster network (IPv4, ULA network for IPv6 is derived in automated way) (default "10.5.0.0/24") --cni-bin-path strings search path for CNI binaries (VM only) (default [/home/user/.talos/cni/bin]) --cni-bundle-url string URL to download CNI bundle from (VM only) (default "https://github.com/talos-systems/talos/releases/download/v0.11.0-alpha.1/talosctl-cni-bundle-${ARCH}.tar.gz")