Difference between revisions of "Create root filesystem snapshots with LVM"

From ArchWiki
Jump to: navigation, search
Line 25: Line 25:
 
{{ic|/etc/systemd/system/mk-lvm-snapshots.service}} containing:
 
{{ic|/etc/systemd/system/mk-lvm-snapshots.service}} containing:
 
{{bc|[Unit]
 
{{bc|[Unit]
Descriptionm=ake LVM snapshots
+
Description=make LVM snapshots
 
Requires=local-fs-pre.target
 
Requires=local-fs-pre.target
 
DefaultDependencies=no
 
DefaultDependencies=no
Line 40: Line 40:
  
 
Adapt the {{ic|lvcreate}} command to match your root volume group and volume name.
 
Adapt the {{ic|lvcreate}} command to match your root volume group and volume name.
Adjust the snapshot size if necessary.
+
Adjust the snapshot size if necessary. If additional filesystems should be snapshotted
 +
during startup you may extend the {{ic|ExecStart}} property with addtional lvcreate commands,
 +
separated with {{ic| ; }}.
  
 
Create a new systemd target {{ic|/etc/systemd/system/make-snapshots.target}} with this service:
 
Create a new systemd target {{ic|/etc/systemd/system/make-snapshots.target}} with this service:
Line 48: Line 50:
 
Adapt the base target, if {{ic|multi-user.target}} is not your default target.
 
Adapt the base target, if {{ic|multi-user.target}} is not your default target.
  
Enable the new service with {{ic|systemctl enable mk-lvm-snapshots.service}}.
+
Enable the new service with {{ic|# systemctl enable mk-lvm-snapshots.service}}.
  
 
If the system is started with the new target, LVM snapshot(s) are created just after mounting the local filesystems.
 
If the system is started with the new target, LVM snapshot(s) are created just after mounting the local filesystems.
Line 65: Line 67:
 
Remember to adjust {{ic|custom.cfg}} if {{ic|grub.cfg}} changes.
 
Remember to adjust {{ic|custom.cfg}} if {{ic|grub.cfg}} changes.
  
After restarting the system with this grub entry {{ic|lvs}} should show up the newly created snapshot.
+
After restarting the system with this grub entry {{ic|# lvs}} should show up the newly created snapshot.
  
 
{{Tip|To get the messages of the new service use
 
{{Tip|To get the messages of the new service use
{{ic|journalctl -u mk-lvm-snapshots.service}}.}}
+
{{ic|# journalctl -u mk-lvm-snapshots.service}}.}}
  
  
Line 79: Line 81:
  
 
During backup you can continue to use your system normally, since all changes to your regular volumes are invisible in
 
During backup you can continue to use your system normally, since all changes to your regular volumes are invisible in
the snapshots. Do not forget to delete the snapshot volume after the backup -- changes to your regular volume will
+
the snapshots. Do not forget to delete the snapshot volume after the backup – changes to your regular volume will
 
use up space in the snapshot due to the copy-on-write operations. If the snapshot space becomes fully used LVM
 
use up space in the snapshot due to the copy-on-write operations. If the snapshot space becomes fully used LVM
 
will deny further writes to your regular volumes, which should be avoided.
 
will deny further writes to your regular volumes, which should be avoided.
Line 88: Line 90:
  
 
If you want to permantly stick to the updates just drop the snapshot. If you want to revert to the snapshotted
 
If you want to permantly stick to the updates just drop the snapshot. If you want to revert to the snapshotted
state issue a {{ic|lvchange --merge}} for the snapshot. During the next restart of the system the snapshot
+
state issue a {{ic|# lvchange --merge}} for the snapshot. During the next restart of the system the snapshot
 
is merged back into your regular volume. All changes to the volume happened after the snapshot are undone.
 
is merged back into your regular volume. All changes to the volume happened after the snapshot are undone.
  

Revision as of 15:05, 27 April 2013

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary end


Prerequisites

You need a system with LVM root filesystem and systemd. Check if LVM snapshots are correctly setup.


Setup

During system start a clean snapshot of the root volume is created using a new service. Create /etc/systemd/system/mk-lvm-snapshots.service containing:

[Unit]
Description=make LVM snapshots
Requires=local-fs-pre.target
DefaultDependencies=no
Conflicts=shutdown.target
After=local-fs-pre.target
Before=local-fs.target

[Install]
WantedBy=make-snapshots.target

[Service]
Type=oneshot
ExecStart=/usr/sbin/lvcreate -L10G -n snap-root -s lvmvolume/root

Adapt the lvcreate command to match your root volume group and volume name. Adjust the snapshot size if necessary. If additional filesystems should be snapshotted during startup you may extend the ExecStart property with addtional lvcreate commands, separated with  ; .

Create a new systemd target /etc/systemd/system/make-snapshots.target with this service:

[Unit]
Description=Make Snapshots
Requires=multi-user.target

Adapt the base target, if multi-user.target is not your default target.

Enable the new service with # systemctl enable mk-lvm-snapshots.service.

If the system is started with the new target, LVM snapshot(s) are created just after mounting the local filesystems. To get a grub menu entry starting this target create /boot/grub/custom.cfg based on the grub.cfg entry for your normal startup. The kernel command line is extended to start the new make-snapshots.target:

### make snapshots ###
menuentry 'Arch GNU/Linux, make snapshots' --class arch --class gnu-linux --class gnu --class os {
...
        echo    'Loading Linux core repo kernel ...'
        linux   /boot/vmlinuz-linux root=/dev/mapper/lvmvolume-root ro systemd.unit=make-snapshots.target
        echo    'Loading initial ramdisk ...'
        initrd  /boot/initramfs-linux.img
} 

Remember to adjust custom.cfg if grub.cfg changes.

After restarting the system with this grub entry # lvs should show up the newly created snapshot.

Tip: To get the messages of the new service use # journalctl -u mk-lvm-snapshots.service.


Usage

Backup

To use this functionality for a full system backup, restart your system with the snapshot creation target. Mount the snapshot volume (and further volumes, if required), preferably using the read only (-o) option. Then backup your system, for example with tar as described in Full_System_Backup_with_tar.

During backup you can continue to use your system normally, since all changes to your regular volumes are invisible in the snapshots. Do not forget to delete the snapshot volume after the backup – changes to your regular volume will use up space in the snapshot due to the copy-on-write operations. If the snapshot space becomes fully used LVM will deny further writes to your regular volumes, which should be avoided.

Revert updates

An other use for LVM snapshots is testing and reverting of updates. In this case create a snapshot for the system in a known good state and perform updates or changes afterwards.

If you want to permantly stick to the updates just drop the snapshot. If you want to revert to the snapshotted state issue a # lvchange --merge for the snapshot. During the next restart of the system the snapshot is merged back into your regular volume. All changes to the volume happened after the snapshot are undone.

Known issues

Due to bug 681582 shutting down the system with active snapshots may hang for some time.