Principle of Software Version Control in Linux
In Linux, sometimes we will face to a software which has many editions, such as gcc and java. In different scenes, maybe we need different editions of the same software, so we must save all of them. But how can we switch them?
Linux uses the symlink to allow user easily switching between programs.
Then, we use the gcc as an example to illustrate the principle of software version control.
Hypothetically speaking, the test mechine has two gcc, gcc-9 and gcc-11.
Where is the gcc ?
There are only one directory is related to the execution of gcc, it is /usr/bin/
In /usr/bin
, we will find the following contents which are related to the gcc
:
gcc -> gcc-11
gcc-11 -> x86_64-linux-gnu-gcc-11
x86_64-linux-gnu-gcc-11
gcc-9 -> x86_64-linux-gnu-gcc-9
x86_64-linux-gnu-gcc-9
We can find that only the x86_64-linux-gnu-gcc-9
and x86_64-linux-gnu-gcc-11
is the true executable file. The other files are all the symlinks.
Acording to the file structure, we can know that the key factor which influences the software edition we use is the gcc
symlink. If we can change the target which is pointed by it, we can switch the different editions.
The design idea of update-alternatives
According to the above discussion, if we are the author of update-alternatives
command line tools, what we need to do is just edit the gcc symlink directivity.
update-alternatives
lets the /usr/bin/gcc
point to a symlink /etc/alternatives/gcc
(at the beginning, the /usr/bin/gcc
points to the /usr/bin/gcc-9
or /usr/bin/gcc-11
directly, which is created by this tool. And /etc/alternatives/gcc
is also a symlink which points to the /usr/bin/gcc-9
or /usr/bin/gcc-11
. When we want to use the gcc-9
, /etc/alternatives/gcc
will points to the /usr/bin/gcc-9
, or else it will points to the /usr/bin/gcc-11
.
It means that update-alternatives
can just edit the directivity of the /etc/alternatives/gcc
to edit the edition of software.
The hole flow looks like the following picture:
The usage method of update-alternatives
The key point is to understand the relationship between the symlinks.
We can get the usage method from help option:
–install
- is the symlink pointing to /etc/alternatives/
.
is the master name for this link group.
is the location of one of the alternative target files.
is an integer; options with higher numbers have higher priority in automatic mode.
The relationship between link, name and path looks like the following pictures:
So the following command is an example:
|
|
We can use update-alternatives --list gcc
to inspect the config result, we will get the following info:
/usr/bin/gcc-11
/usr/bin/gcc-9
And we can use update-alternatives --config gcc
to chose an edition of gcc, it looks like