This is a guide to create or restore images of your partitions using the great open source tool Partimage. It provides information on how to do this locally or across the network, by setting up a Partimaged Server. I guess this info is non distribution specific, since we will work from a Rescue environment.
The same operations can be performed using the
dd utility, but the usage of Partimage is recommended as it does not include the partition’s free space in the image, resulting in images smaller in size and less required time to complete the operation. Partimage is a command line tool with a basic ncurses user interface. For information about the supported filesystems, you can check its homepage.
The most important thing when saving/restoring your partitions to/from image files is that they are not mounted, so to keep away from trouble. It is also highly recommended that these operations are done from a Rescue environment. Most distributions’ installation media provide such an option in their welcome screen or else one of the various Live CDs can be used.
We will use the statically built Partimage, so download this package from its homepage. This version does not have any dependencies, since everything has been built inside it and it can be run from anywhere. I suggest you save the package to a diskette.
Act professionally and keep detailed notes of the partitions you want to save or restore. A simple mistake in the partition names could lead to loss of data. Also read carefully the partition information presented by Partimage. For a list of your partitions, issue the following command:
# fdisk -l
Save/Restore Partitions Locally
The developers of Partimage have tried to keep the command line options pretty straight forward and provide some usage examples in the utility’s help message. Change to the directory where you have extracted the partimage package and have a look by typing:
# ./partimage --help
You do not have to provide any command line options, as almost all of them are accessible through the program’s user interface. You can access it by typing:
Some of the keyboard shortcuts are:
F5 – Continue
F6 – Exit
To quickly SAVE a partition to an image file issue the command:
# ./partimage -z1 -o -d save /dev/hdb7 /mnt/storage/test_hdb7.partimg.gz
Make sure you substitute the device node (
/dev/hdb7) with your partition and the path to the image file (
/mnt/storage/test_hdb7.partimg.gz) with your own.
A little explanation of the options:
-z1: Use the
gzip algorithm to compress the image. z0 means no compression and z2 means usage of the
bzip2 algorithm, but it’s slower.
-o: Overwrite image files without confirmation.
-d: Do not ask for a description for the image. You can exclude this one if you want to save some notes about the image file you are going to create.
save: This is not an option, but the action that Partimage will perform.
To quickly RESTORE an image to a partition, type:
# ./partimage restore /dev/hdb7 /mnt/storage/test_hdb7.partimg.gz.000
Partimage adds a numeric extension to the images. This has a meaning only if you had set Partimage to automatically split the image into multiple volumes when you had saved the partition.
To see some information about an image file, issue the command:
# ./partimage imginfo /mnt/storage/test_hdb7.partimg.gz.000
Partimage saves the MBR of your disks together with your partition. You can avoid this by using the
-M option when saving a partition, but I guess it does not hurt having backups of the MBR. The MBR is not automatically restored when you restore a partition.
To restore the MBR from an image file issue the command:
# ./partimage restmbr /mnt/storage/test_hdb7.partimg.gz.000
That will be it. One thing to keep in mind is to unmount the partitions before saving or restoring them.
Save/Restore Partitions Remotely
You can use Partimage to save/restore partitions remotely. To use this feature, you have to setup the
Partimaged Server. The machine on which Partimaged is running will be the place where the partition images are saved to or read from. You run Partimage on the client machines and connect to the server in order to save or read the image files you want.
Configuring the Partimaged Server
The following operations must be performed on the server machine by root.
First of all, you need to create a new user, named
partimag. When Partimaged is run as root, it soon drops priviledges and the
partimag user is used to run the server for security. So, this user must exist. Create the user with the following command:
# useradd -c "Partimaged User" -s /sbin/nologin -r -M partimag
This creates a system account (
-r) for the
partimag user and does not create a home directory for it (
-M). Also the user cannot login to a shell (
-s /sbin/nologin). These
useradd options should be the same among the various Linux distributions, but you should check the
useradd man page to confirm that.
Next, you have to decide which will be the directory from where the Partimaged Server will run. This directory will also be used as a depot for the image files which will be saved or read by the client machines. For this article, a
/var/partimaged_storage/ directory will be used. Create this directory:
# mkdir /var/partimaged_storage
partimaged executable file from the package you have downloaded to this directory and set ownership and strict permissions on this directory and its contents:
# chown -R partimag:partimag /var/partimaged_storage # chmod -R 0700 /var/partimaged_storage
That’s it. Change to the
/var/partimaged_storage/ directory. The server can be started with the following command:
# ./partimaged --debug=1
You can use the
--port options if you intend to use another directory for image file storage or if you want the daemon to listen on a port other than the default 4025 for connections. The debug level is also set to "1", so that log information is written to the default logfile
/var/log/partimaged.log. This option can be safely omitted, but this way no log file will be created. The server can accept multiple connections.
The above command starts the Partimaged Server and a monitor screen will be displayed, so you can watch which clients connect and monitor the progress of their operations. If you want to run the server in the background, then you should start the server with the
Finally, make sure you open the tcp port you have used (the default is 4025) in your firewall configuration.
Here follow some notes about security. The version of
partimaged that comes in the package you have downloaded is not compiled with SSL or user logins support (at the moment of writing). This is probably for ease of use. This means that if security matters, you should compile the package yourself enabling all the needed options.
Because security does matter, I provide some info on how to connect to the Partimaged Server through encrypted SSH tunnels when you save/restore sensitive data. This way you can control which users can connect to the Partimaged Server by using SSH for access control and you can be sure that data does not travel in the clear across the network. Keep in mind that this is not the recommended way because SSH usually uses very strong encryption, so the whole operation could take a lot of time to complete and it will certainly devour your system resources. I suggest you compile partimaged with SSL and user logins support and make use of these features for security.
Save/Restore Partitions across the Network
Now that the Partimaged Server is up and running, you can use
Partimage on the client machines and save the partition images to the server or read the partition images from the server and restore them on the client machines.
All the options that are used when you perform these operations locally can be used in this case, but you also have to supply the Partimaged Server’s IP address and the port it listens on. Furthermore, you do not have to supply a path for the image file, since the directory from where
partimaged runs will be used to write the image files to or read them from.
So, to SAVE a partition from a client machine to the Partimaged Server issue the command (from the client machine):
./partimage --server=192.168.0.1 --port=4025 -z1 -o -d save /dev/hdb7 test_hdb7.partimg.gz
A path for the image file has not been specified. The above command also assumes that the server is bound on the
192.168.0.1 IP address.
To RESTORE an image file to the client machine issue the command:
./partimage --server=192.168.0.1 --port=4025 restore /dev/hdb7 test_hdb7.partimg.gz.000
Again, a path to the image file does not need to be specified.
The following information about connecting to the Partimaged Server through SSH tunnels is provided, but it’s not really recommended (see previous notes). It assumes that an SSH server is running on the machine where the
partimaged daemon runs. Using this way, port 4025 does not need to be open in the server’s firewall.
To create the SSH tunnel and connect through it to the Partimaged Server to SAVE a partition from a client machine, issue the following command (from the client):
ssh -f -L 24025:127.0.0.1:4025 user@remote sleep 10; ./partimage --server=127.0.0.1 --port=24025 -z1 -o -d save /dev/hdb7 test_hdb7.partimg.gz
The same goes when you RESTORE partitions to client machines:
ssh -f -L 24025:127.0.0.1:4025 user@remote sleep 10; ./partimage --server=127.0.0.1 --port=24025 restore /dev/hdb7 test_hdb7.partimg.gz.000
For more information about this way of starting SSH tunnels, you can read a comment of mine on another page.
I have used Partimage numerous times to save and restore
ext3 partitions and it has worked just fine. I also have some
xfs partitions, but I never had the need to save or restore them, so I cannot tell. Partimage supports
xfs. For information about
NTFS or other filesystem support see the Partimage homepage and also have a look at linux-ntfs.org.
I consider that the following pages should be read for more information.
Partition images with Partimage and Partimaged by George Notaras, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2006 - Some Rights Reserved