Virtual Machine
Total Page:16
File Type:pdf, Size:1020Kb
Topic 11: Virtual Machine Tongping Liu University of Massachusetts Amherst 1 Outline • Virtual machine concepts/types • Para-virtualization • Full-virtualization University of Massachusetts Amherst 2 Traditional OS structure App App App App Operating System Host Machine University of Massachusetts Amherst 3 Coarser Abstraction: Virtual Machine • We’ve already seen a kind of virtual machine – OS gives processes virtual memory – Each process runs on a (virtualized) CPU • Virtual machine: another abstraction – An execution environment – May or may not correspond to physical reality – Provide an interface identical to the underlying bare hardware University of Massachusetts Amherst 4 5 Virtual Machine • A Virtual Machine is a software that creates a virtualized environment between the computer platform and the end user in which the end user can operate software. University of Massachusetts Amherst 5 Virtualization • Virtualization is an abstraction layer that decouples the physical hardware from the operating system to deliver greater IT resource utilization and flexibility. • It allows multiple virtual machines, with heterogeneous operating systems to run in isolation, side-by-side on the same physical machine. 6 University of Massachusetts Amherst 6 Virtual Machine Options • How to implement a virtual machine? Java virtual 1. Interpreted virtual machines machine – Translate every VM instruction – Kind of like on-the-fly compilation – VM instruction à HW instruction(s) 2. Direct execution – Execute instructions directly VirtualBox – Emulate the hard ones University of Massachusetts Amherst 7 Java Virtual Machine The JVM is an abstract machine that reads the Java bytecode and does whatever computation it describes. The core of a JVM interpreter is basically this: do { byte opcode = fetch an opcode; switch (opcode) { case opCode1 : fetch operands for opCode1; execute action for opCode1; break; case opCode2 : fetch operands for opCode2; execute action for opCode2; break; case ... } while (more to do) University of Massachusetts Amherst 8 Full vs Interpreted • Why would I use VirtualBox instead of JVM? – Support for legacy applications – Do not force users to use a particular language – Do not force users to use a particular OS • Why would I use JVM instead of VirtualBox? – Nice properties of type-safe language – Can prove safety at compile time University of Massachusetts Amherst 9 Virtualization Types Level of isolation varies from weak to strong • Resource Containers • LXC and Docker • Emulate virtual hardware • Bochs, QEMU • Para-virtualization • Xen • Full virtualization • VMware, VirtualBox University of Massachusetts Amherst 10 Type 1 and Type 2 Hypervisor • Type 1: Running on bare machine – Xen, XenServer, VMware, KVM and OpenVZ. • Type 2: Runs as an application on an established operating system – VirtualBox University of Massachusetts Amherst 11 Foundation of “cloud” VMware IPO: $19.1 billion Xen sale: $500 million University of Massachusetts Amherst 12 Benefits of VMs to Cloud Consolidate underutilized servers X Avoid downtime with relocation Dynamically re-balance workload to guarantee application quality of service X X Enforce security policy University of Massachusetts Amherst 13 Traditional Virtual Machine Guest Guest Guest App App App Guest OS Guest OS Guest OS Virtual Machine Monitor (Hypervisor) Host Machine University of Massachusetts Amherst 14 Virtual Machine Monitor • The host software that provides virtualization is often referred to as a virtual machine monitor (VMM) or hypervisor. • The VMM gives each virtual machine an illusion of a complete computer to itself. University of Massachusetts Amherst 15 Hypervisor Benefits • Code reuse – Can run old operating systems + apps on new hardware – Original purpose of VMs by IBM in the 60s • Encapsulation – Can put entire state of an “application” in one thing – Move it, restore it, copy it, etc • Isolation, security – All interactions with hardware are mediated – Hypervisor can keep one VM from affecting another – Hypervisor cannot be corrupted by guest operating systems • Aids code development and debugging University of Massachusetts Amherst 16 Encapsulation • If I want to suspend/restore/migrate an application • I decide to write the process mem + PCB to disk • I reboot my kernel and restart the process • Will this work? – No, application state is spread out in many places – Application might involve multiple processes – Applications have state in the kernel (lost on reboot) – Residual dependencies (e.g. open files, locks, process ids, driver states, etc) University of Massachusetts Amherst 17 Encapsulation • Virtual machines capture all of these states • Can suspend/restore an application – On same machine between boots – On different machines • Migration much harder for containers – Kernel is a moving target • Doesn’t explicitly isolate and track dependent state – Every version might be different University of Massachusetts Amherst 18 Security • Can user processes corrupt the kernel? – Can overwrite logs – Overwrite kernel file – Can boot a new kernel – Exploit a bug in the system call interface • If use a hypervisor., is my data any less vulnerable? – All the state in the guest is still vulnerable (file systems, etc) • So what’s the point? – Hypervisors can observe the guest OS – Security services in hypervisor are safe, makes detection easier University of Massachusetts Amherst 19 Security • Hypervisors buggy too, why trust them more than kernels? – Narrower interface to malicious code (no system calls) – No way for kernel to call into hypervisor – Smaller, (hopefully) less complex codebase – Should be fewer bugs • Anything wrong with this argument? – Hypervisors are still complex – May be able to take over hypervisor via non-syscall interfaces – Paravirtualization (in Xen) may compromise this University of Massachusetts Amherst 20 Outline • Virtual machine concepts/types • Para-virtualization • Full-virtualization University of Massachusetts Amherst 21 Challenges of Implementing VM • Privilege modes – guest OS think it runs in kernel mode • Memory management – guest OS thinks it has access to physical memory • Performance – two levels of context switching • Protection – interference between guests may violate isolation University of Massachusetts Amherst 22 Views of the CPU • How is a process’ view of the CPU different than OS’s? – Kernel mode – Access to physical memory – Manipulation of page tables – Other “privileged instructions” – Turn on/off interrupts – Traps University of Massachusetts Amherst 23 Xen’s Approach • Start with VMM, “para-virtualize” • Goals – Performance isolation – Support many operating systems – Reduce performance overhead of virtualization University of Massachusetts Amherst 24 Xen Architecture Guest Guest Host App App App Guest OS Guest OS Domain 0 Xen Host Machine University of Massachusetts Amherst 25 Para-Virtualization • Full virtualization – Fool OS into thinking it has access to hardware • Para-virtualization – Expose real and virtual resources to OS • Why do we need para-virtualization? – Mostly because x86 architectures (used to) make full virtualization hard University of Massachusetts Amherst 26 Why Para-Virtualize? • Previous limitations of x86 – Privileged instructions fail silently – VMM must execute these instructions – Cannot rely on traps to VMM – How does VMware deal with this? • At run-time rewrite guest kernel binary • Insert traps into the VMM, when necessary University of Massachusetts Amherst 27 Why Para-Virtualize? • Previous limitations of x86 • Timing issues – May want to expose “real time” to OS – TCP time outs, RTT estimates • Support for performance optimizations – Supporting huge pages in memory; increases TLB coverage University of Massachusetts Amherst 28 Xen: Physical Memory • Allocated by hypervisor when VM is created • Why can’t we allow guest OS to update PTBR? – Might map virtual addrs to physical addrs they don’t own • VMware and Xen handled this differently – VMware maintains “shadow page tables” – Xen uses “hypercalls” – (Xen and VMware actually support both mechanisms now) University of Massachusetts Amherst 29 Xen: Hypercalls Virtual → Machine Update PTE hypercall Guest OS 1) Validation check 2) Perform update VMM Hardware MMU University of Massachusetts Amherst 30 Para-Virtualized CPU • Hypervisor runs at higher privilege than guest OS – Ring 0 à hypervisor, ring 1 à guest OS, ring 3 à guest apps • Handling exceptions – Guest registers handlers with Xen (must modify guest OS!) • System calls – Guests register "fast" handler with Xen – Xen validates handler, inserts in CPU’s handler table University of Massachusetts Amherst 31 Para-Virtualization • Pros – Better performance – Scales better than full virtualization • Cons – OS needs (minor) changes – How well does it actually scale? Depends… – Impure abstractions – Is it important to provide good abstractions? • I say yes • Bad interfaces lead to code complexity, maintainability issues University of Massachusetts Amherst 32 Outline • Virtual machine concepts/types • Para-virtualization • Full-virtualization University of Massachusetts Amherst 33 VMware Approach Host World VMM World Target Target App App Host VM App Target OS App Virtual Machine Host OS VM Driver Monitor Host Machine University of Massachusetts Amherst 34 VMware Approach • Designed without hardware support • Uses dynamic binary instrumentation https://www.geeksforgeeks.org/virtualization-vmware-full-virtualization/ University of Massachusetts Amherst 35 VMware Approach: