Create root filesystem snapshots with LVM: Difference between revisions
m (Fix i18n link.) |
m (Improve layout) |
||
(23 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:System recovery]] | [[Category:System recovery]] | ||
[[zh- | [[ja:LVM によるルートファイルシステムのスナップショット]] | ||
[[zh-hans:Create root filesystem snapshots with LVM]] | |||
{{Related articles start}} | {{Related articles start}} | ||
{{Related|LVM}} | {{Related|LVM}} | ||
{{Related|Full | {{Related|Full system backup with tar}} | ||
{{Related articles end}} | {{Related articles end}} | ||
This article describes how to set up root filesystem LVM snapshot creation during system start. Such snapshots can be used for [[Full | This article describes how to set up root filesystem LVM snapshot creation during system start. Such snapshots can be used for [[Full system backup with tar|full system backups]] with minimal downtime or testing system updates with the option to revert them. | ||
== Prerequisites == | == Prerequisites == | ||
Line 15: | Line 16: | ||
== Setup == | == Setup == | ||
=== Option 1: Helper Scripts === | |||
{{ | An AUR package is available, {{AUR|make-lvm-snapshot}}, which can help automate the following process. It implements similar service and target files to those included in this article. The projects [https://gitlab.com/Marcool04/make-lvm-snapshot README] contains important setup information. | ||
Description | === Option 2: Manual Configuration === | ||
Requires | |||
DefaultDependencies | If you prefer a fully "manual" setup, you will need to write a systemd service to create a clean snapshot of the root volume during system startup. | ||
Conflicts | |||
After | Create: | ||
Before | |||
{{hc|/etc/systemd/system/mk-lvm-snapshots.service|2= | |||
[Unit] | |||
Description=make LVM snapshots | |||
Requires=local-fs-pre.target | |||
Wants=local-fs.target | |||
DefaultDependencies=no | |||
Conflicts=shutdown.target | |||
After=local-fs-pre.target | |||
Before=local-fs.target | |||
[Install] | [Install] | ||
WantedBy | WantedBy=make-snapshots.target | ||
[Service] | [Service] | ||
Type | Type=oneshot | ||
ExecStart | ExecStart=/usr/sbin/lvcreate -L10G -n snap-root -s lvmvolume/root | ||
}} | |||
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. If additional filesystems should be snapshotted | 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, | during startup you may extend the {{ic|ExecStart}} property with addtional lvcreate commands, | ||
separated with {{ic| ; }} (consider there is a space before and after the semicolon, see | separated with {{ic| ; }} (consider there is a space before and after the semicolon, see {{man|5|systemd.service|COMMAND LINES}} for details). | ||
{{Note|You should test the {{ic| | {{Note|You should test the {{ic|lvcreate}} command in the running system until it works as desired. | ||
Remove the test snapshots with {{ic| | Remove the test snapshots with {{ic|lvremove}}. The snapshots taken from a running system are not as consistent as snapshots taken in single user mode or during startup.}} | ||
not as consistent as snapshots taken in single user mode or during startup.}} | |||
Create a new systemd target | Create a new systemd target: | ||
Enable | {{hc|/etc/systemd/system/make-snapshots.target|2= | ||
[Unit] | |||
Description=Make Snapshots | |||
Requires=''multi-user.target'' | |||
}} | |||
Adapt the base target, if {{ic|multi-user.target}} is not your default target. If you want to get into your desktop, you need to change it to {{ic|graphical.target}}. | |||
[[Enable]] {{ic|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 54: | Line 68: | ||
{{ic|grub.cfg}} entry for your normal startup. The kernel command line is extended to start the new | {{ic|grub.cfg}} entry for your normal startup. The kernel command line is extended to start the new | ||
{{ic|make-snapshots.target}}: | {{ic|make-snapshots.target}}: | ||
{{bc|### make snapshots ### | |||
{{bc|1= | |||
### make snapshots ### | |||
menuentry 'Arch GNU/Linux, make snapshots' --class arch --class gnu-linux --class gnu --class os { | menuentry 'Arch GNU/Linux, make snapshots' --class arch --class gnu-linux --class gnu --class os { | ||
... | ... | ||
echo 'Loading Linux core repo kernel ...' | echo 'Loading Linux core repo kernel ...' | ||
linux /boot/vmlinuz-linux root | linux /boot/vmlinuz-linux root=/dev/lvmvolume/root ro '''systemd.unit=make-snapshots.target''' | ||
echo 'Loading initial ramdisk ...' | echo 'Loading initial ramdisk ...' | ||
initrd /boot/initramfs-linux.img | initrd /boot/initramfs-linux.img | ||
} }} | } | ||
}} | |||
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| | 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 | {{Tip|To get the messages of the new service, run {{ic|journalctl -u mk-lvm-snapshots.service}} as root.}} | ||
{{ic| | |||
== Usage == | == Usage == | ||
=== Backup === | === Backup === | ||
To use this functionality for a full system backup, restart your system with the snapshot creation target. | 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 ( | Mount the snapshot volume (and further volumes, if required), preferably using the read only ({{ic|-o}}) option. | ||
Then backup your system, for example with tar as described in [[Full | 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 | During backup you can continue to use your system normally, since all changes to your regular volumes are invisible in | ||
Line 84: | Line 101: | ||
=== Revert updates === | === Revert updates === | ||
Another 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. | in a known good state and perform updates or changes afterwards. | ||
If you want to permantly stick to the updates just drop the snapshot with | If you want to permantly stick to the updates just drop the snapshot with ''lvremove''. If you want to revert to the snapshotted | ||
state issue a {{ic| | state issue a {{ic|lvconvert --merge}} for the snapshot. During the next restart of the system (use the default target) 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. | ||
{{Note|After merging the snapshot no longer exists. Recreate a new snapshot if further testing with rollback | {{Note|After merging the snapshot no longer exists. Recreate a new snapshot if further testing with rollback | ||
option is desired.}} | option is desired.}} | ||
Latest revision as of 09:08, 30 April 2023
This article describes how to set up root filesystem LVM snapshot creation during system start. Such snapshots can be used for full system backups with minimal downtime or testing system updates with the option to revert them.
Prerequisites
You need a system with LVM root filesystem and systemd. Ensure that LVM snapshots prerequisites are correctly setup.
Setup
Option 1: Helper Scripts
An AUR package is available, make-lvm-snapshotAUR, which can help automate the following process. It implements similar service and target files to those included in this article. The projects README contains important setup information.
Option 2: Manual Configuration
If you prefer a fully "manual" setup, you will need to write a systemd service to create a clean snapshot of the root volume during system startup.
Create:
/etc/systemd/system/mk-lvm-snapshots.service
[Unit] Description=make LVM snapshots Requires=local-fs-pre.target Wants=local-fs.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 ;
(consider there is a space before and after the semicolon, see systemd.service(5) § COMMAND LINES for details).
lvcreate
command in the running system until it works as desired.
Remove the test snapshots with lvremove
. The snapshots taken from a running system are not as consistent as snapshots taken in single user mode or during startup.Create a new systemd target:
/etc/systemd/system/make-snapshots.target
[Unit] Description=Make Snapshots Requires=multi-user.target
Adapt the base target, if multi-user.target
is not your default target. If you want to get into your desktop, you need to change it to graphical.target
.
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/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.
journalctl -u mk-lvm-snapshots.service
as root.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, and LVM is not able to automatically grow the snapshot, LVM will deny further writes to your regular volumes or drop the snapshot, which should be avoided.
Revert updates
Another 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 with lvremove. If you want to revert to the snapshotted
state issue a lvconvert --merge
for the snapshot. During the next restart of the system (use the default target) the snapshot
is merged back into your regular volume. All changes to the volume happened after the snapshot are undone.