Index: trunk/src/VBox/Main/idl/VirtualBox.xidl
===================================================================
--- trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 41913)
+++ trunk/src/VBox/Main/idl/VirtualBox.xidl	(revision 41914)
@@ -3667,8 +3667,32 @@
   </enum>
 
+  <enum
+    name="AutostopType" extends="$unknown"
+    uuid="6bb96740-cf34-470d-aab2-2cd48ea2e10e"
+    >
+
+    <desc>
+    Autostop types, used with <link to="IMachine::autostopType" />.
+    </desc>
+
+    <const name="Disabled"           value="1">
+      <desc>Stopping the VM during system shutdown is disabled.</desc>
+    </const>
+    <const name="SaveState"          value="2">
+      <desc>The state of the VM will be saved when the system shuts down.</desc>
+    </const>
+    <const name="PowerOff"           value="3">
+      <desc>The VM is powered off when the system shuts down.</desc>
+    </const>
+    <const name="AcpiShutdown"       value="4">
+      <desc>An ACPI shutdown event is generated.</desc>
+    </const>
+
+  </enum>
+
 
   <interface
     name="IMachine" extends="$unknown"
-    uuid="e7c78fee-f20d-4b4f-ac28-37af30d79ddf"
+    uuid="d6406544-910e-43b2-a8fb-5528e56ce3b4"
     wsmap="managed"
     >
@@ -4319,4 +4343,22 @@
         business accessing the VMCPU or VM structures, and are therefore unable
         to get any pointers to these.
+      </desc>
+    </attribute>
+
+    <attribute name="autostartEnabled" type="boolean">
+      <desc>
+        Enables autostart of the VM during system boot.
+      </desc>
+    </attribute>
+
+    <attribute name="autostartDelay" type="unsigned long">
+      <desc>
+        Number of seconds to wait until the VM should be started during system boot.
+      </desc>
+    </attribute>
+
+    <attribute name="autostopType" type="AutostopType">
+      <desc>
+        Action type to do when the system is shutting down.
       </desc>
     </attribute>
Index: trunk/src/VBox/Main/include/MachineImpl.h
===================================================================
--- trunk/src/VBox/Main/include/MachineImpl.h	(revision 41913)
+++ trunk/src/VBox/Main/include/MachineImpl.h	(revision 41914)
@@ -300,4 +300,5 @@
 
         settings::Debugging  mDebugging;
+        settings::Autostart  mAutostart;
     };
 
@@ -477,4 +478,10 @@
     STDMETHOD(COMGETTER(AllowTracingToAccessVM))(BOOL *pfAllow);
     STDMETHOD(COMSETTER(AllowTracingToAccessVM))(BOOL fAllow);
+    STDMETHOD(COMGETTER(AutostartEnabled))(BOOL *pfEnabled);
+    STDMETHOD(COMSETTER(AutostartEnabled))(BOOL fEnabled);
+    STDMETHOD(COMGETTER(AutostartDelay))(ULONG *puDelay);
+    STDMETHOD(COMSETTER(AutostartDelay))(ULONG uDelay);
+    STDMETHOD(COMGETTER(AutostopType))(AutostopType_T *penmAutostopType);
+    STDMETHOD(COMSETTER(AutostopType))(AutostopType_T enmAutostopType);
 
     // IMachine methods
@@ -795,6 +802,8 @@
                          const Guid &aCurSnapshotId,
                          Snapshot *aParentSnapshot);
-    HRESULT loadHardware(const settings::Hardware &data, const settings::Debugging *pDbg);
+    HRESULT loadHardware(const settings::Hardware &data, const settings::Debugging *pDbg,
+                         const settings::Autostart *pAutostart);
     HRESULT loadDebugging(const settings::Debugging *pDbg);
+    HRESULT loadAutostart(const settings::Autostart *pAutostart);
     HRESULT loadStorageControllers(const settings::Storage &data,
                                    const Guid *puuidRegistry,
@@ -836,5 +845,6 @@
     void copyMachineDataToSettings(settings::MachineConfigFile &config);
     HRESULT saveAllSnapshots(settings::MachineConfigFile &config);
-    HRESULT saveHardware(settings::Hardware &data, settings::Debugging *pDbg);
+    HRESULT saveHardware(settings::Hardware &data, settings::Debugging *pDbg,
+                         settings::Autostart *pAutostart);
     HRESULT saveStorageControllers(settings::Storage &data);
     HRESULT saveStorageDevices(ComObjPtr<StorageController> aStorageController,
@@ -1206,4 +1216,5 @@
                              const settings::Hardware &hardware,
                              const settings::Debugging *pDbg,
+                             const settings::Autostart *pAutostart,
                              const settings::Storage &storage,
                              IN_GUID aSnapshotId,
Index: trunk/src/VBox/Main/src-server/MachineImpl.cpp
===================================================================
--- trunk/src/VBox/Main/src-server/MachineImpl.cpp	(revision 41913)
+++ trunk/src/VBox/Main/src-server/MachineImpl.cpp	(revision 41914)
@@ -6548,4 +6548,105 @@
 }
 
+STDMETHODIMP Machine::COMGETTER(AutostartEnabled)(BOOL *pfEnabled)
+{
+    CheckComArgOutPointerValid(pfEnabled);
+    AutoCaller autoCaller(this);
+    HRESULT hrc = autoCaller.rc();
+    if (SUCCEEDED(hrc))
+    {
+        AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
+        *pfEnabled = mHWData->mAutostart.fAutostartEnabled;
+    }
+    return hrc;
+}
+
+STDMETHODIMP Machine::COMSETTER(AutostartEnabled)(BOOL fEnabled)
+{
+    AutoCaller autoCaller(this);
+    HRESULT hrc = autoCaller.rc();
+    if (SUCCEEDED(hrc))
+    {
+        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
+        hrc = checkStateDependency(MutableStateDep);
+        if (SUCCEEDED(hrc))
+        {
+            hrc = mHWData.backupEx();
+            if (SUCCEEDED(hrc))
+            {
+                setModified(IsModified_MachineData);
+                mHWData->mAutostart.fAutostartEnabled = fEnabled != FALSE;
+            }
+        }
+    }
+    return hrc;
+}
+
+STDMETHODIMP Machine::COMGETTER(AutostartDelay)(ULONG *puDelay)
+{
+    CheckComArgOutPointerValid(puDelay);
+    AutoCaller autoCaller(this);
+    HRESULT hrc = autoCaller.rc();
+    if (SUCCEEDED(hrc))
+    {
+        AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
+        *puDelay = mHWData->mAutostart.uAutostartDelay;
+    }
+    return hrc;
+}
+
+STDMETHODIMP Machine::COMSETTER(AutostartDelay)(ULONG uDelay)
+{
+    AutoCaller autoCaller(this);
+    HRESULT hrc = autoCaller.rc();
+    if (SUCCEEDED(hrc))
+    {
+        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
+        hrc = checkStateDependency(MutableStateDep);
+        if (SUCCEEDED(hrc))
+        {
+            hrc = mHWData.backupEx();
+            if (SUCCEEDED(hrc))
+            {
+                setModified(IsModified_MachineData);
+                mHWData->mAutostart.uAutostartDelay = uDelay;
+            }
+        }
+    }
+    return hrc;
+}
+
+STDMETHODIMP Machine::COMGETTER(AutostopType)(AutostopType_T *penmAutostopType)
+{
+    CheckComArgOutPointerValid(penmAutostopType);
+    AutoCaller autoCaller(this);
+    HRESULT hrc = autoCaller.rc();
+    if (SUCCEEDED(hrc))
+    {
+        AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
+        *penmAutostopType = mHWData->mAutostart.enmAutostopType;
+    }
+    return hrc;
+}
+
+STDMETHODIMP Machine::COMSETTER(AutostopType)(AutostopType_T enmAutostopType)
+{
+    AutoCaller autoCaller(this);
+    HRESULT hrc = autoCaller.rc();
+    if (SUCCEEDED(hrc))
+    {
+        AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
+        hrc = checkStateDependency(MutableStateDep);
+        if (SUCCEEDED(hrc))
+        {
+            hrc = mHWData.backupEx();
+            if (SUCCEEDED(hrc))
+            {
+                setModified(IsModified_MachineData);
+                mHWData->mAutostart.enmAutostopType = enmAutostopType;
+            }
+        }
+    }
+    return hrc;
+}
 
 
@@ -7893,5 +7994,5 @@
 
     // hardware data
-    rc = loadHardware(config.hardwareMachine, &config.debugging);
+    rc = loadHardware(config.hardwareMachine, &config.debugging, &config.autostart);
     if (FAILED(rc)) return rc;
 
@@ -7964,4 +8065,5 @@
                                             data.hardware,
                                             &data.debugging,
+                                            &data.autostart,
                                             data.storage,
                                             data.uuid.ref(),
@@ -8011,8 +8113,10 @@
  *  Loads settings into mHWData.
  *
- *  @param data     Reference to the hardware settings.
- *  @param pDbg     Pointer to the debugging settings.
+ *  @param data           Reference to the hardware settings.
+ *  @param pDbg           Pointer to the debugging settings.
+ *  @param pAutostart     Pointer to the autostart settings.
  */
-HRESULT Machine::loadHardware(const settings::Hardware &data, const settings::Debugging *pDbg)
+HRESULT Machine::loadHardware(const settings::Hardware &data, const settings::Debugging *pDbg,
+                              const settings::Autostart *pAutostart)
 {
     AssertReturn(!isSessionMachine(), E_FAIL);
@@ -8266,4 +8370,6 @@
         if (FAILED(rc))
             return rc;
+
+        mHWData->mAutostart = *pAutostart;
     }
     catch(std::bad_alloc &)
@@ -9106,5 +9212,5 @@
     /// @todo Live Migration:        config.fTeleported = (mData->mMachineState == MachineState_Teleported);
 
-    HRESULT rc = saveHardware(config.hardwareMachine, &config.debugging);
+    HRESULT rc = saveHardware(config.hardwareMachine, &config.debugging, &config.autostart);
     if (FAILED(rc)) throw rc;
 
@@ -9179,9 +9285,12 @@
  *  given node is empty.
  *
- *  @param data     Reference to the settings object for the hardware config.
- *  @param pDbg     Pointer to the settings object for the debugging config
- *                  which happens to live in mHWData.
+ *  @param data           Reference to the settings object for the hardware config.
+ *  @param pDbg           Pointer to the settings object for the debugging config
+ *                        which happens to live in mHWData.
+ *  @param pAutostart     Pointer to the settings object for the autostart config
+ *                        which happens to live in mHWData.
  */
-HRESULT Machine::saveHardware(settings::Hardware &data, settings::Debugging *pDbg)
+HRESULT Machine::saveHardware(settings::Hardware &data, settings::Debugging *pDbg,
+                              settings::Autostart *pAutostart)
 {
     HRESULT rc = S_OK;
@@ -9417,4 +9526,5 @@
 
         *pDbg = mHWData->mDebugging;
+        *pAutostart = mHWData->mAutostart;
     }
     catch(std::bad_alloc &)
Index: trunk/src/VBox/Main/src-server/SnapshotImpl.cpp
===================================================================
--- trunk/src/VBox/Main/src-server/SnapshotImpl.cpp	(revision 41913)
+++ trunk/src/VBox/Main/src-server/SnapshotImpl.cpp	(revision 41914)
@@ -773,5 +773,5 @@
         data.strStateFile.setNull();
 
-    HRESULT rc = m->pMachine->saveHardware(data.hardware, &data.debugging);
+    HRESULT rc = m->pMachine->saveHardware(data.hardware, &data.debugging, &data.autostart);
     if (FAILED(rc)) return rc;
 
@@ -1081,4 +1081,5 @@
                                           const settings::Hardware &hardware,
                                           const settings::Debugging *pDbg,
+                                          const settings::Autostart *pAutostart,
                                           const settings::Storage &storage,
                                           IN_GUID aSnapshotId,
@@ -1157,5 +1158,5 @@
     /* load hardware and harddisk settings */
 
-    HRESULT rc = loadHardware(hardware, pDbg);
+    HRESULT rc = loadHardware(hardware, pDbg, pAutostart);
     if (SUCCEEDED(rc))
         rc = loadStorageControllers(storage,
Index: trunk/src/VBox/Main/xml/Settings.cpp
===================================================================
--- trunk/src/VBox/Main/xml/Settings.cpp	(revision 41913)
+++ trunk/src/VBox/Main/xml/Settings.cpp	(revision 41914)
@@ -1763,4 +1763,5 @@
                   && (llChildSnapshots      == s.llChildSnapshots)          // deep compare
                   && debugging              == s.debugging
+                  && autostart              == s.autostart
                 )
            );
@@ -3198,4 +3199,29 @@
 
 /**
+ * Called for reading the <Autostart> element under <Machine> or <Snapshot>.
+ */
+void MachineConfigFile::readAutostart(const xml::ElementNode *pElmAutostart, Autostart *pAutostart)
+{
+    Utf8Str strAutostop;
+
+    if (!pElmAutostart || m->sv < SettingsVersion_v1_13)
+        return;
+
+    pElmAutostart->getAttributeValue("enabled", pAutostart->fAutostartEnabled);
+    pElmAutostart->getAttributeValue("delay", pAutostart->uAutostartDelay);
+    pElmAutostart->getAttributeValue("autostop", strAutostop);
+    if (strAutostop == "Disabled")
+        pAutostart->enmAutostopType = AutostopType_Disabled;
+    else if (strAutostop == "SaveState")
+        pAutostart->enmAutostopType = AutostopType_SaveState;
+    else if (strAutostop == "PowerOff")
+        pAutostart->enmAutostopType = AutostopType_PowerOff;
+    else if (strAutostop == "AcpiShutdown")
+        pAutostart->enmAutostopType = AutostopType_AcpiShutdown;
+    else
+        throw ConfigFileError(this, pElmAutostart, N_("Invalid value '%s' for Autostart/@autostop attribute"), strAutostop.c_str());
+}
+
+/**
  * Called initially for the <Snapshot> element under <Machine>, if present,
  * to store the snapshot's data into the given Snapshot structure (which is
@@ -3271,4 +3297,5 @@
 
     readDebugging(elmSnapshot.findChildElement("Debugging"), &snap.debugging);
+    readAutostart(elmSnapshot.findChildElement("Autostart"), &snap.autostart);
 }
 
@@ -3421,4 +3448,6 @@
             else if (pelmMachineChild->nameEquals("Debugging"))
                 readDebugging(pelmMachineChild, &debugging);
+            else if (pelmMachineChild->nameEquals("Autostart"))
+                readAutostart(pelmMachineChild, &autostart);
         }
 
@@ -4373,4 +4402,33 @@
 
 /**
+ * Creates a <Autostart> node under elmParent and then writes out the XML
+ * keys under that. Called for both the <Machine> node and for snapshots.
+ *
+ * @param pElmParent    Pointer to the parent element.
+ * @param pAutostart    Pointer to the autostart settings.
+ */
+void MachineConfigFile::buildAutostartXML(xml::ElementNode *pElmParent, const Autostart *pAutostart)
+{
+    const char *pcszAutostop = NULL;
+
+    if (m->sv < SettingsVersion_v1_13 || pAutostart->areDefaultSettings())
+        return;
+
+    xml::ElementNode *pElmAutostart = pElmParent->createChild("Autostart");
+    pElmAutostart->setAttribute("enabled", pAutostart->fAutostartEnabled);
+    pElmAutostart->setAttribute("delay", pAutostart->uAutostartDelay);
+
+    switch (pAutostart->enmAutostopType)
+    {
+        case AutostopType_Disabled:     pcszAutostop = "Disabled";     break;
+        case AutostopType_SaveState:    pcszAutostop = "SaveState";    break;
+        case AutostopType_PowerOff:     pcszAutostop = "PowerOff";     break;
+        case AutostopType_AcpiShutdown: pcszAutostop = "AcpiShutdown"; break;
+        default:         Assert(false); pcszAutostop = "Disabled";     break;
+    }
+    pElmAutostart->setAttribute("autostop", pcszAutostop);
+}
+
+/**
  * Writes a single snapshot into the DOM tree. Initially this gets called from MachineConfigFile::write()
  * for the root snapshot of a machine, if present; elmParent then points to the <Snapshots> node under the
@@ -4402,4 +4460,5 @@
                                     // since snapshots never get written then
     buildDebuggingXML(pelmSnapshot, &snap.debugging);
+    buildAutostartXML(pelmSnapshot, &snap.autostart);
 
     if (snap.llChildSnapshots.size())
@@ -4550,4 +4609,5 @@
                                pllElementsWithUuidAttributes);
     buildDebuggingXML(&elmMachine, &debugging);
+    buildAutostartXML(&elmMachine, &autostart);
 }
 
@@ -4667,6 +4727,7 @@
     if (m->sv < SettingsVersion_v1_13)
     {
-        // VirtualBox 4.2 adds tracing.
-        if (!debugging.areDefaultSettings())
+        // VirtualBox 4.2 adds tracing and autostart.
+        if (   !debugging.areDefaultSettings()
+            || !autostart.areDefaultSettings())
             m->sv = SettingsVersion_v1_13;
     }
