Change a computer but preserve the OS (Arch Linux) and data


This is interesting. Previously, if I want to change a computer, I will (re-)install the OS and other software on the target computer. Then only move the data.

Since Arch Linux is a minimalistic distro, and installation only available with network connection. This will be exhaustive to install the OS on the target computer. Not only wasting time downloading, but also setup the configurations, such as web server, FTP server, hibernation, power save, etc. User’s configurations are easier, because I can just copy all the files in /home/$USER directory to the target computer. The only problem may be the differences of the owner ID and group ID. But that is not really a big deal. So, the main problem is the root (/).

Cloning the partition is not a good solution. Firstly, the target computer is different, I wish to preserve the original OS. If cloning the partition, the target computer needs to have extra space for the cloned partition. Secondly, configuration such as GRUB, fstab, and other partition related configuration will not work. To fix them, I need to find all these configurations, but how do I get them?

Luckily, Arch Linux is a minimalist distro, which has its great advantage. So, this is what I did.

Firstly, I get the latest installation ISO. Then, partition the target computer with SystemRescueCD. (I tried with LVM, but at the end failed, because the first partition is started at sector 63. Installing GRUB where the /boot is in the LVM requires larger partition space.)

Then, boot into the Arch Linux live media, and follow all the installation steps as in the official installation guide. As expected, the installation of the “base” requires network connection. It is around 100+ MB. (I really hope that, if it is possible, Arch Linux official installation ISO will contain at least “base” group, so that it is possible to install a minimal OS without network connection.)

After installing everything, I made sure that the original OS and Arch Linux are bootable through GRUB.

Then the essential part is transferring the whole data from old computer to new computer. Firstly, I need to transfer the data from old to an external harddisk. Thus, I used the following command in old computer (based on this page),

tar -czpvf backup.tar.gz --one-file-system --exclude=/backup.tar.gz \
--exclude=/dev --exclude=/tmp --exclude=/sys --exclude=/proc \
--exclude=/mnt --exclude=/media --exclude=/boot \
--exclude=/etc/{hostname,fstab,localtime,locale.conf,vconsole.conf} /
#exclude any folder that we don't want

Tar is the best solution (for me) to preserve all the file permission and ownership. Besides that, tar also allows to compress the data (with gz). If the size doesn’t matter, we can omit the “-z” flag and the file can be named as “backup.tar” without “.gz” suffix. So, several directories (folders) are excluded, because these may be computer dependent. The /boot must be excluded, because the partitions of the computers are different. And finally some files in /etc needs to be excluded. These files are actually the files which we need to edit during the installation of the Arch Linux, especially fstab. (That is why I said luckily Arch Linux is minimalistic.) Then, use the same method to backup the /home and any other data.

Next, to extract the data from the tar, I didn’t use the Arch Linux in the new computer itself. This is because I afraid that overwriting some of the files (especially the running process) are not allowed or causing damage of the system. Thus, I use SystemRescueCD and mount the root partition of Arch Linux, Then,

tar -xzpvf backup.tar.gz -C /mnt/arch --numeric-owner

Then, I also use the same way to extract other data.

Finally, restart. Everything work fine. The only thing I need to reconfigure is the /etc/suspend.conf from uswsusp-git because of the partition differences. Besides that, I also reset the pacman-key as mentioned here.

Advertisements

Extracting files from zip which contains non-UTF8 filename in Linux


Previously, I have made a post about extracting SHIFT_JIS filename encoding in zip file.

However, this method does not work when the filenames contain GBK (simplified Chinese) encoding. As a result, I found a general solution for the non-UTF8 encoding.

The method is almost the same, but more generic way.

Firstly, the problem we face is after extracting the files, the filenames are unreadable. Not only that, we cannot convert the filename even we are using “convmv”, “iconv”, or even “uconv”. This is normally caused by our OS locale setting. To make our OS (Linux) more generic to read almost any languages (East Asia languages, right-to-left langues, etc), our OS is normally has the UTF8 locale. It may be en_US.UTF8, ja_JP.UTF8, zh_CN.UTF8, zh_TW.UTF8, en_GB.UTF8, etc.

The problem we extract the non-UTF8 filenames from zip in UTF8 environment will cause our filenames irrecoverable. This is because the non-UTF8 is write as UTF8 without any conversion. That is why, when we want to convert the filenames with these mojibake, it is always fail, no matter we are in UTF8 or non-UTF8 environment. Because we are not using the correct encoding when extracting the file.

But (in my opinion) there is no extraction tool allows conversion of the filename. As a result, we need to preserve the default encoding from the files, to write in our UTF8 environment.

In my old post, I use a non-UTF8 language, that is ja_JP for extracting SHIFT_JIS. Thus, a more generic way is extract our filename with LANG=”C”. That is ANSI C language without any UTF8 encoding.

env LANG=C 7z x file.zip

As a result, you will see the filenames have a lot of question marks.

Then now, we can convert the file with our real encoding.

convmv -f gbk -t utf8 --notest -r * #for filename which is GBK coded