Support >
  About cloud server >
  How to create a Linux server file system as a mirror?
How to create a Linux server file system as a mirror?
Time : 2025-04-15 15:53:00
Edit : Jtti

  How to create a Linux file system into an image file, also known as "system packaging". This type of operation is not only suitable for migration and backup, but also for batch deployment, custom system installation, and even as a snapshot archive for version rollback.

  In the Linux world, an image generally refers to a complete system file structure, which is packaged into a file in a read-only form and can be mounted, restored, and deployed. It is not a compressed package or a copied folder, but a snapshot at the structure level.

  For example, .iso installation images, .img system images, Docker images, local .qcow2/.raw virtual disks, etc. are all some form of "file system images".

  What we need to do is actually just one sentence: turn the running file system into an image file that can be restored, deployed, and migrated at any time.

  Overview of common methods: 3 mainstream methods:

  DD mirror method: The tool uses dd + partition identification, the advantage is original cloning, strong versatility, suitable for disk overall backup / disaster recovery

  File system development: Tools tar/rsync + mkfs + mount, the advantage is smaller and customizable, suitable for streamlined system, container construction

  Loop mirror method: Tools truncate + mkfs + mount, the advantage is no physical device, flexible mounting, suitable for image production and testing

  We will focus on "Loop mirror method" + "file system method" below, because they are more flexible, more universal, and more suitable for modern cloud servers, development and testing and other scenarios.

  Practical: Create Linux file system images step by step

  Step 1: Preparation

  First select a directory as the backup source, such as the root directory / (not recommended in the production environment), or a specific system directory, such as /opt/myapp, /srv/www, etc. It is recommended to clean up irrelevant content first, such as logs and caches, to avoid encapsulating garbage into the image.

  Step 2: Create an empty image file

  We use truncate to create an empty file of a fixed size:

 truncate -s 2G myfilesystem.img

  Here we create a 2GB image file. You can adjust the size according to your actual needs.

  Step 3: Format this image as ext4 (or other file systems)

 mkfs.ext4 myfilesystem.img

  The system will prompt you that this is a normal file, it doesn't matter, it will be treated as a "device".

  Step 4: Mount this image file

 mkdir /mnt/mirrorfs
 mount -o loop myfilesystem.img /mnt/mirrorfs

  Now, this image is like a real hard disk, mounted under /mnt/mirrorfs.

  Step 5: Copy the file system to this mount point

  You can choose to copy the entire system or a directory as needed.

  Copy the entire system (excluding mount points such as proc and sys):

 rsync -aAXv / --exclude={"/proc/*","/dev/*","/sys/*","/tmp/*","/mnt/*","/run/*","/media/*","/lost+found"} /mnt/mirrorfs/

  Or just copy a directory:

 rsync -a /opt/myapp/ /mnt/mirrorfs/

  Step 6: Unmount and save the image

 umount /mnt/mirrorfs

  Now your myfilesystem.img is a complete file system image file.

  You can: copy it to other servers, mount it with mount -o loop to view, mount it with a virtual machine/container to start the system, and burn it to a physical disk for deployment and use

  Packaging a system is not as simple as copying files, but "replicating a recoverable ecosystem". A complete Linux server image not only saves time, but is also an important means to improve deployment quality and disaster recovery reliability. You don't need any "black technology", just master a few basic commands to easily complete the construction of the system image.

JTTI-Eom
JTTI-Defl
JTTI-COCO
JTTI-Selina
JTTI-Ellis
Title
Email Address
Type
Sales Issues
Sales Issues
System Problems
After-sales problems
Complaints and Suggestions
Marketing Cooperation
Information
Code
Submit