Difference between pages "Bash by Example, Part 1" and "Install/de/Partitioning"

From Funtoo
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
{{Article
<noinclude>
|Subtitle=Fundamental programming in the Bourne again shell (bash)
{{InstallPart|the process of partitioning and filesystem creation}}
|Author=Drobbins
</noinclude>
|Next in Series=Bash by Example, Part 2
===Vorbereiten der Festplatte ===
}}
 
=== Introduction ===
Diese Sektion handelt über die verschiedenen Möglichkeiten Funtoo Linux auf einer Festplatte zu installieren und zu booten.
You might wonder why you ought to learn Bash programming. Well, here are a couple of compelling reasons:
 
==== Einleitung ====
 
Früher gab es nur eine Variante einen PC zu booten, alle Desktop- und Servercomputer hatten einen voreingestellten PC  BIOS, alle Festplatten nutzten den Master Boot Record (MBR) um das System zu booten und unsere Festplatten waren  mit dem MBR Partitionsschema in verschiedene Regionen partitioniert. Das war einfach wie's gemacht wurde. Und uns gefiel es!
 
Dann kamen EFI und UEFI, neue firmware designt das System zu booten, gemeinsam mit GTP Partitionstabellen um Partitionen auf Festplatten größer als 2.2TB zu definieren.
Plötzlich haben wir eine breite Wahl von Optionen, Linux Systeme zu installieren und zu booten. Damit haben wir nun eine komplexere Situation als damals.
 
Nehmen wir einen Moment um die verfügbaren Optionen, zur Konfiguration der Festplatte um Linux zu booten, zu besprechen.
Diese Installationsanleitung nutzt und empfiehlt die old-school Methode des BIOS bootens mit hilfe des MBR. Es funktioniert und (außer in seltenen Fällen) ist universal unterstützt.
Mit dieser Methode ist nichts falsch, solange deine Systemfestplatte nur bis zu 2TB groß ist. Solange wird diese Methode die volle Kapazität deiner Festplatte nutzen.
 
Es gibt aber einige Situationen, in denen diese old-school Methode nicht optimal ist. Falls du eine Systemfestplatte >2TB hast, dann erlauben dir MBR Partitionen keinen Zugang zum gesamten Speicher.
Das ist also ein Grund gegen diese Methode. Ein Weiterer ist, dass es "PC" Systeme gibt, welche das booten via BIOS nicht mehr unterstützen und dich zwingen via UEFI zu booten.
Aus Mitleid für die PC-Nutzer, die in diese Zwickmühle geraten, decken wir das Booten via UEFI zusätzlich in dieser Installationsanleitung ab .
 
Unsere empfehlung ist immer noch die old-school Methode, es seiden du hast Gründe dagegen.
Der Bootloader, den wir nutzen um den Linux Kernel zu laden, heißt GRUB. Also nennen wir die Methode  '''BIOS + GRUB(MBR) ''' Methode.
Es ist die traditionelle Methode um ein Linux System bootbar zu machen.
 
Falls du via UEFI booten willst, empfehlen wir dir nicht den MBR zum booten zu nutzen, was nur manche Systeme unterstützen, sondern wir empfehlen UEFI zu nutzen um GRUB zu laden.
GRUB wird dann das Linux System booten. Wir referenzieren zu dieser Methode mit '''UEFI + GRUB (GPT)'''.
 
Und ja, es gibt noch weitere Methoden, von denen einige auf der [[Boot Methods]] Seite dokumentiert sind.
Unsere Empfehlung war immer die  '''BIOS + GRUB (GPT)'' Methode, welche allerdings nun nicht mehr konsistent und hardwareübergreifend unterstützt wird.
 
'''Die größte Frage ist immer -- Welche Bootmethode sollst du nutzen?''' Hier ist mein Gedankengang.
 
;Grundsatz 1 - Old School: Falls du verlässlich via System Rescue CD booten kannst und dir ein leicht blaues Menü angezeigt wird, dann bootet die CD via BIOS und es ist sehr wahrscheinlich, das du auch Funtoo Linux via BIOS booten kannst. Also gehe old-school und nutze diese Methode, es sei denn du hast Gründe via UEFI zu booten. Zum Beispiel eine Systemfestplatte >2.2TB  In diesem Fall beachte Grundsatz 2, wenn dein System UEFI unterstützt.
 
;Grundsatz 2 - New School: Falls du verlässlich via System Rescue CD booten kannst und dir ein schwarz und weißes Menü, --Glückwunsch, dein System ist konfiguriert UEFI zu unterstützen. Das bedeutet das du bereit bist Funtoo Linux einzurichten um via UEFI zu booten. Dein System könnte immer noch das Booten übers BIOS unterstützen, aber versuch es einfach mal mit UEFI als erstes. Du kannst in deiner BIOS Konfiguration herum stochern und damit spielen.
 
;Was ist der große Unterschied zwischen Old School und New School?: Hier ist der Deal. Falls du mit old-school MBR Partitionen gehst, deine <code>/boot</code> Partition wird ein ext2 Dateisystem haben, und du wirst <code>fdisk</code>nutzen um MBR Partitionen zu erstellen. Fallse du mit new-school GPT Partitionen und booten via UEFI gehst, wird deine <code>/boot</code> Partition ein  vfat Dateisystem haben, da UEFI dies lesen kann,außerdem wirst du <code>gdisk</code> nutzen um GPT Partitionen zu erstellen. Und du wirst GRUB ein wenig anders installieren. Das ist alles was es zu wissen gibt, für den Fall das du neugierig warst.
 
;Notiere auch: Um Funtoo Linux via new-school UEFI Methode bootbar zu machen ist es notwendig, dass die System Rescue CD via UEFI gebooted wurde -- und du den ursprünglichen schwarz und weißen Bildschirm siehst. Ansonsten wird UEFI nicht aktiv sein und du wirst nicht in der Lage sein Funtoo Linux korrekt zu installieren.
 
{{Note|'''Einige Motherboards unterstützen UEFI nicht richtig.''' Informiere dich. Zum Beispiel, das Award BIOS in meinem Gigabyte GA-990FXA-UD7 rev 1.1 hat eine Option das Booten via UEFI für CD/DVD zu aktivieren. '''Das ist aber nicht ausreichend um UEFI für Festplatten zu nutzen und Funtoo Linux zu installieren.''' UEFI muss für entfernbare Datenträger und fixierte Datenträger unterstützt werden. (Damit du deine neue Funtoo Installation booten kannst) Tatsächlich haben die neueren Revisionen des Boards(rev 3.0) volle UEFI unterstützung. Das könnte der dritte Grundsatze sein -- kenne die Hardware. }}
 
==== Old-School (BIOS/MBR) Methode ====
 
{{Note|Falls du via System Rescue CD booten kannst und dir ein leicht blaues Menü angezeigt wird nutze diese Methode. Falls du die new-school Methode nutzen wirst, [[#New-School (UEFI/GPT) Methode|Klicke hier um direkt zu UEFI/GPT zu springen.]]}}
 
===== Vorbereitung  =====
 
Erstens, es ist ein gute Idee sicherzustellen, dass du die richtige Festplatte zum Partitionieren gefunden hast. Versuche es mit diesem Befehl und verifiziere, dass  <code>/dev/sda</code> die Festplatte zum Partitionieren ist:
 
<console>
# ##i##fdisk -l /dev/sda
 
Disk /dev/sda: 640.1 GB, 640135028736 bytes, 1250263728 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
 
 
#        Start          End    Size  Type            Name
1        2048  1250263694  596.2G  Linux filesyste Linux filesystem
</console>
 
Nun, ist es empfohlen, das du alle existierende MBR oder GTP Partitionstabellen auf der Festplatte, welche das System beim Booten verwirren könnten löschst. Wir machen dies mit <code>sgdisk</code>:
{{fancywarning|Dies wird deine existierende Partitionen unerreichbar machen! Du bist "stark" gewarnt und  geraten alle wichtigen/kritischen Daten vorher zu sichern.}}
 
<console>
# ##i##sgdisk --zap-all /dev/sda
 
Creating new GPT entries.
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
</console>
 
Diese Ausgabe ist nichts beängstigendes, da der Befehl trotzdem erfolgreich war:
 
<console>
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory.
***************************************************************
</console>
 
===== Partitionierung =====
 
Nun werden wir  <code>fdisk</code> nutzen um die MBR Partitionstabelle und die Partitionen zu erstellen:
 
<console>
# ##i##fdisk /dev/sda
</console>
 
Innerhalb <code>fdisk</code>, folge diesen Schritten:
 
'''Leere die Partitionstabelle''':
 
<console>
Command (m for help): ##i##o ↵
</console>
 
'''Erstelle Partition 1''' (boot):
 
<console>
Command (m for help): ##i##n ↵
Partition type (default p): ##i##↵
Partition number (1-4, default 1): ##i##↵
First sector: ##i##↵
Last sector: ##i##+128M ↵
</console>
 
'''Erstelle Partition 2''' (swap):
 
<console>
Command (m for help): ##i##n ↵
Partition type (default p): ##i##↵
Partition number (2-4, default 2): ##i##↵
First sector: ##i##↵
Last sector: ##i##+2G ↵
Command (m for help): ##i##t ↵
Partition number (1,2, default 2): ##i## ↵
Hex code (type L to list all codes): ##i##82 ↵
</console>
 
'''Erstelle root Partition:'''
 
<console>
Command (m for help): ##i##n ↵
Partition type (default p): ##i##↵
Partition number (3,4, default 3): ##i##↵
First sector: ##i##↵
Last sector: ##i##↵
</console>
 
'''Verifiziere die Partitionstabelle:'''
 
<console>
Command (m for help): ##i##p
 
Disk /dev/sda: 298.1 GiB, 320072933376 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x82abc9a6
 
Device    Boot    Start      End    Blocks  Id System
/dev/sda1          2048    264191    131072  83 Linux
/dev/sda2        264192  4458495  2097152  82 Linux swap / Solaris
/dev/sda3        4458496 625142447 310341976  83 Linux
</console>
 
'''Schreibe die Partitionstabelle auf die Festplatte:'''
 
<console>
Command (m for help): ##i##w
</console>
 
Deine neue MBR Partitionstabelle wird nun auf die Systemfestplatte geschrieben.
 
{{Note|Du bist noch nicht fertig mit der Partitionierung! Jetzt, springe zu [[#Creating filesystems|Creating filesystems]].}}
 
==== New-School (UEFI/GPT) Methode ====
 
{{Note|Falls du verlässlich via System Rescue CD booten kannst und dir ein schwarz und weißes Menü angezeigt wurde, verwende diese Methode. Ansonsten diese Methode wird nicht funktionieren!}}
 
Die <tt>gdisk</tt>Befehle um GTP Partitionstabellen zu erstellen lauten wie folgt. Passe die Große jeweils an, falls notwendig, diese Voreinstellungen werden für die meisten Nutzer funktionieren.Beginne <code>gdisk</code>:
 
<console>
# ##i##gdisk /dev/sda
</console>
 
Innerhalb <tt>gdisk</tt>, folge diesen Schritten:
 
'''Erstelle eine neue leere Partitionstabelle''' (Dies wird alle Daten löschen!):
 
<console>
Command: ##i##o ↵
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): ##i##y ↵
</console>
 
'''Erstelle Partition 1''' (boot):
 
<console>
Command: ##i##n ↵
Partition Number: ##i##1 ↵
First sector: ##i##↵
Last sector: ##i##+500M ↵
Hex Code: ##i##↵
</console>
 
''Erstelle Partition 2''' (swap):
 
<console>
Command: ##i##n ↵
Partition Number: ##i##2 ↵
First sector: ##i##↵
Last sector: ##i##+4G ↵
Hex Code: ##i##8200 ↵
</console>
 
'''Erstelle Partition 3''' (root):


; You're already running it :If you check, you'll probably find that you are running bash right now. Even if you changed your default shell, bash is probably still running somewhere on your system, because it's the standard Linux shell and is used for a variety of purposes. Because bash is already running, any additional bash scripts that you run are inherently memory-efficient because they share memory with any already-running bash processes. Why load a 500K interpreter if you already are running something that will do the job, and do it well?
<console>
Command: ##i##n ↵
Partition Number: ##i##3 ↵
First sector: ##i##↵
Last sector: ##i##↵##!i## (for rest of disk)
Hex Code: ##i##↵
</console>


; You're already using it:Not only are you already running bash, but you're actually interacting with bash on a daily basis. It's always there, so it makes sense to learn how to use it to its fullest potential. Doing so will make your bash experience more fun and productive. But why should you learn bash programming? Easy, because you already think in terms of running commands, CPing files, and piping and redirecting output. Shouldn't you learn a language that allows you to use and build upon these powerful time-saving constructs you already know how to use? Command shells unlock the potential of a UNIX system, and bash is the Linux shell. It's the high-level glue between you and the machine. Grow in your knowledge of bash, and you'll automatically increase your productivity under Linux and UNIX -- it's that simple.
Während des Prozesses kannst du mit "<tt>p</tt>" und Enter die aktuelle Partitionstabelle anschauen. Falls du einen Fehler machst "<tt>d</tt>" eine existierende Partition löschen.
Wenn du fertig bist mit deiner Partitionierung kannst du mit "<tt>w</tt>" die Konfiguration auf die Festplatte schreiben lassen.


=== Bash confusion ===
'''Schreibe Partitionstabelle auf die Festplatte''':
Learning bash the wrong way can be a very confusing process. Many newbies type <span style="color:green;">man bash</span> to view the bash man page, only to be confronted with a very terse and technical description of shell functionality. Others type <span style="color:green;">info bash</span> (to view the GNU info documentation), causing either the man page to be redisplayed, or (if they are lucky) only slightly more friendly info documentation to appear.


While this may be somewhat disappointing to novices, the standard bash documentation can't be all things to all people, and caters towards those already familiar with shell programming in general. There's definitely a lot of excellent technical information in the man page, but its helpfulness to beginners is limited.
<console>
Command: ##i##w ↵
Do you want to proceed? (Y/N): ##i##Y ↵
</console>


That's where this series comes in. In it, I'll show you how to actually use bash programming constructs, so that you will be able to write your own scripts. Instead of technical descriptions, I'll provide you with explanations in plain English, so that you will know not only what something does, but when you should actually use it. By the end of this three-part series, you'll be able to write your own intricate bash scripts, and be at the level where you can comfortably use bash and supplement your knowledge by reading (and understanding!) the standard bash documentation. Let's begin.
Die Partitionstabelle wird nun auf die Festplatte geschrieben und <tt>gdisk</tt> wird beendet.


=== Environment variables ===
Nun, wurden deine GTP/GUID Partitionen erstellt und sie werden gezeigt wie folgt under ''block devices'' unter Linux:
Under bash and almost all other shells, the user can define environment variables, which are stored internally as ASCII strings. One of the handiest things about environment variables is that they are a standard part of the UNIX process model. This means that environment variables not only are exclusive to shell scripts, but can be used by standard compiled programs as well. When we "export" an environment variable under bash, any subsequent program that we run can read our setting, whether it is a shell script or not. A good example is the <span style="color:green">vipw</span> command, which normally allows root to edit the system password file. By setting the <span style="color:green">EDITOR</span> environment variable to the name of your favorite text editor, you can configure vipw to use it instead of vi, a handy thing if you are used to xemacs and really dislike vi.


The standard way to define an environment variable under bash is:
<pre>
$ myvar='This is my environment variable!'
</pre>
The above command defined an environment variable called "myvar" and contains the string "This is my environment variable!". There are several things to notice above: first, there is no space on either side of the "=" sign; any space will result in an error (try it and see). The second thing to notice is that while we could have done away with the quotes if we were defining a single word, they are necessary when the value of the environment variable is more than a single word (contains spaces or tabs).


{{fancynote|For extremely detailed information on how quotes should be used in bash, you may  want to look at the "QUOTING" section in the bash man page. The existence of special character sequences that get "expanded" (replaced) with other values does complicate how strings are handled in bash. We will just cover the most often-used quoting functionality in this series.}}
* <tt>/dev/sda1</tt>, wird für das <tt>/boot</tt> Dateisystem genutzt,
* <tt>/dev/sda2</tt>, wird für den swap Speicher genutzt, und
* <tt>/dev/sda3</tt>, wird für das root Dateisystem verwendet.


Thirdly, while we can normally use double quotes instead of single quotes, doing so in the above example would have caused an error. Why? Because using single quotes disables a bash feature called expansion, where special characters and sequences of characters are replaced with values. For example, the "!" character is the history expansion character, which bash normally replaces with a previously-typed command. (We won't be covering history expansion in this series of articles, because it is not frequently used in bash programming. For more information on it, see the "HISTORY EXPANSION" section in the bash man page.) While this macro-like functionality can come in handy, right now we want a literal exclamation point at the end of our environment variable, rather than a macro.
==== Erstelle Dateisysteme ====


Now, let's take a look at how one actually uses environment variables. Here's an example:
{{Note|Dieser Abschnitt behandelt BIOS ''und'' UEFI Installationen. Nicht Überspringen!}}
<pre>
$ echo $myvar
This is my environment variable!
</pre>
By preceding the name of our environment variable with a $, we can cause bash to replace it with the value of myvar. In bash terminology, this is called "variable expansion". But, what if we try the following:
<pre>
$ echo foo$myvarbar
foo
</pre>
We wanted this to echo "fooThis is my environment variable!bar", but it didn't work. What went wrong? In a nutshell, bash's variable expansion facility in got confused. It couldn't tell whether we wanted to expand the variable $m, $my, $myvar, $myvarbar, etc. How can we be more explicit and clearly tell bash what variable we are referring to? Try this:
<pre>
$ echo foo${myvar}bar
fooThis is my environment variable!bar
</pre>
As you can see, we can enclose the environment variable name in curly braces when it is not clearly separated from the surrounding text. While $myvar is faster to type and will work most of the time, ${myvar} can be parsed correctly in almost any situation. Other than that, they both do the same thing, and you will see both forms of variable expansion in the rest of this series. You'll want to remember to use the more explicit curly-brace form when your environment variable is not isolated from the surrounding text by whitespace (spaces or tabs).


Recall that we also mentioned that we can "export" variables. When we export an environment variable, it's automatically available in the environment of any subsequently-run script or executable. Shell scripts can "get to" the environment variable using that shell's built-in environment-variable support, while C programs can use the getenv() function call. Here's some example C code that you should type in and compile -- it'll allow us to understand environment variables from the perspective of C:
Bevor du die neuen Partitionen nutzen kannst, müssen die Partitionen mit Dateisystem ''metadata'' initialisiert werden.
<syntaxhighlight lang="c">
Dieser Prozess ist bekannt als "Erstellen des Dateisystems".
#include <stdio.h>
Nachdem die Dateisysteme erstellt wurden kann die Festplatte eingebunden werden und Daten speichern.
#include <stdlib.h>


int main(void) {
Lass es uns simple halten. Nutzt du old-school MBR Partitionen? Falls ja, erstellen wir ein ext2 Dateisystem unter /dev/sda1:
  char *myenvvar=getenv("EDITOR");
  printf("The editor environment variable is set to %s\n",myenvvar);
}
</syntaxhighlight>
Save the above source into a file called '''myenv.c''', and then compile it by issuing the command:
<pre>
$ gcc myenv.c -o myenv
</pre>
Now, there will be an executable program in your directory that, when run, will print the value of the <span style="color:green">EDITOR</span> environment variable, if any. This is what happens when I run it on my machine:
<pre>
$ ./myenv
The editor environment variable is set to (null)
</pre>
Hmmm... because the <span style="color:green">EDITOR</span> environment variable was not set to anything, the C program gets a null string. Let's try setting it to a specific value:
<pre>
$ EDITOR=xemacs
$ ./myenv
The editor environment variable is set to (null)
</pre>
While you might have expected myenv to print the value "xemacs", it didn't quite work, because we didn't export the EDITOR environment variable. This time, we'll get it working:
<pre>
$ export EDITOR
$ ./myenv
The editor environment variable is set to xemacs
</pre>
So, you have seen with your very own eyes that another process (in this case our example C program) cannot see the environment variable until it is exported. Incidentally, if you want, you can define and export an environment variable using one line, as follows:
<pre>
$ export EDITOR=xemacs
</pre>
It works identically to the two-line version. This would be a good time to show how to erase an environment variable by using <span style="color:green">unset</span>:
<pre>
$ unset EDITOR
$ ./myenv
The editor environment variable is set to (null)
</pre>


=== Chopping strings overview ===
<console>
Chopping strings -- that is, splitting an original string into smaller, separate chunk(s) -- is one of those tasks that is performed daily by your average shell script. Many times, shell scripts need to take a fully-qualified path, and find the terminating file or directory. While it's possible (and fun!) to code this in bash, the standard <span style="color:green">basename</span> UNIX executable performs this extremely well:
# ##i##mkfs.ext2 /dev/sda1
<pre>
</console>
$ basename /usr/local/share/doc/foo/foo.txt
foo.txt
$ basename /usr/home/drobbins
drobbins
</pre>
<span style="color:green">basename</span> is quite a handy tool for chopping up strings. It's companion, called <span style="color:green">dirname</span>, returns the "other" part of the path that <span style="color:green">basename</span> throws away:
<pre>
$ dirname /usr/local/share/doc/foo/foo.txt
/usr/local/share/doc/foo
$ dirname /usr/home/drobbins/
/usr/home
</pre>
{{fancynote|Both dirname and basename do not look at any files or directories on disk; they are purely string manipulation commands.}}


=== Command substitution ===
Falls du new-school GPT Partitionen für UEFI nutzt, erstellst du ein vfat Dateisystem unter /dev/sda1, da UEFI dieses lesen kann:
One very handy thing to know is how to create an environment variable that contains the result of an executable command. This is very easy to do:
<pre>
$ MYDIR=$(dirname /usr/local/share/doc/foo/foo.txt)
$ echo $MYDIR
/usr/local/share/doc/foo
</pre>
What we did above is called ''command substitution''. Several things are worth noticing in this example. On the first line, we simply enclosed the command we wanted to execute with ''$( )''.


Note that it is also possible to do the same thing using backquotes, the keyboard key that normally sits above the Tab key:
<pre>
$ MYDIR=`dirname /usr/local/share/doc/foo/foo.txt`
$ echo $MYDIR
/usr/local/share/doc/foo
</pre>
As you can see, bash provides multiple ways to perform exactly the same thing. Using command substitution, we can place any command or pipeline of commands in between ''` `'' or ''$( )'' and assign it to an environment variable. Handy stuff! Here's an example of how to use a pipeline with command substitution:


<pre>
<console>
$ MYFILES=$(ls /etc | grep pa)
# ##i##mkfs.vfat -F 32 /dev/sda1
$ echo $MYFILES
</console>
pam.d passwd
</pre>


It's also worth pointing out that ''$( )'' is generally preferred over ''` `'' in shell scripts because it is more universally supported across different shells, is easier to type and read, and is less complicated to use in a nested form, as follows:
Nun, lass uns eine swap Partition erstellen. Die Partition wird genutzt als Überlauf (virtueller Zwischenspeicher) , wenn dein RAM ausgelastet ist oder wenn du dein System in den Ruhezustand versetzen willst.
<pre>
$ MYFILES=$(ls $(dirname foo/bar/oni))
</pre>


=== Chopping strings like a pro ===
Du wirst kein Dateisystem auf der swap Partition erstellen, denn swap wird nicht zum Speichern von Datein genutzt, aber es ist notwendig eine swap Partition zu initialisieren via dem  <code>mkswap</code> Befehl. Dann werden wir den Befehl <code>swapon</code> ausführen, damit ist die swap Partition sofort aktiv innerhalb deiner aktuellem (live CD) Umgebung.
While <span style="color:green">basename</span> and <span style="color:green">dirname</span> are great tools, there are times where we may need to perform more advanced string "chopping" operations than just standard pathname manipulations. When we need more punch, we can take advantage of bash's advanced built-in variable expansion functionality. We've already used the standard kind of variable expansion, which looks like this: ${MYVAR}. But bash can also perform some handy string chopping on its own. Take a look at these examples:
Für den Fall das der swap im späterem Installationsprozess benötigt wird:
<pre>
<console>
$ MYVAR=foodforthought.jpg
# ##i##mkswap /dev/sda2
$ echo ${MYVAR##*fo}
# ##i##swapon /dev/sda2
rthought.jpg
</console>
$ echo ${MYVAR#*fo}
odforthought.jpg
</pre>
In the first example, we typed ${MYVAR##*fo}. What exactly does this mean? Basically, inside the ''${ }'', we typed the name of the environment variable, two ##s, and a wildcard ("*fo"). Then, bash took <span style="color:green">MYVAR</span>, found the longest substring from the beginning of the string "foodforthought.jpg" that matched the wildcard "*fo", and chopped it off the beginning of the string. That's a bit hard to grasp at first, so to get a feel for how this special "##" option works, let's step through how bash completed this expansion. First, it began searching for substrings at the beginning of "foodforthought.jpg" that matched the "*fo" wildcard. Here are the substrings that it checked:
<pre>
f     
fo              MATCHES *fo
foo   
food
foodf         
foodfo          MATCHES *fo
foodfor
foodfort       
foodforth
foodfortho     
foodforthou
foodforthoug
foodforthought
foodforthought.j
foodforthought.jp
foodforthought.jpg
</pre>
After searching the string for matches, you can see that bash found two. It selects the longest match, removes it from the beginning of the original string, and returns the result.


The second form of variable expansion shown above appears identical to the first, except it uses only one "#" -- and bash performs an almost identical process. It checks the same set of substrings as our first example did, except that bash removes the shortest match from our original string, and returns the result. So, as soon as it checks the "fo" substring, it removes "fo" from our string and returns "odforthought.jpg".
Nun, müssen wir das root Dateisystem erstellen. Dort wird das Wurzelverzeichnis von Funtoo Linux leben.
Wir empfehlen im allgemeinen ext4 oder XFS als root Dateisystem. Bist du nicht sicher wähle ext4.  
Hier steht wie man ein ext4 root Dateisystem erstellt:


This may seem extremely cryptic, so I'll show you an easy way to remember this functionality. When searching for the longest match, use ## (because ## is longer than #). When searching for the shortest match, use #. See, not that hard to remember at all! Wait, how do you remember that we are supposed to use the '#' character to remove from the *beginning* of a string? Simple! You will notice that on a US keyboard, shift-4 is "$", which is the bash variable expansion character. On the keyboard, immediately to the left of "$" is "#". So, you can see that "#" is "at the beginning" of "$", and thus (according to our mnemonic), "#" removes characters from the beginning of the string. You may wonder how we remove characters from the end of the string. If you guessed that we use the character immediately to the right of "$" on the US keyboard ("%"), you're right! Here are some quick examples of how to chop off trailing portions of strings:
<console>
<pre>
# ##i##mkfs.ext4 /dev/sda3
$ MYFOO="chickensoup.tar.gz"
</console>
$ echo ${MYFOO%%.*}
chickensoup
$ echo ${MYFOO%.*}
chickensoup.tar
</pre>
As you can see, the % and %% variable expansion options work identically to # and ##, except they remove the matching wildcard from the end of the string. Note that you don't have to use the "*" character if you wish to remove a specific substring from the end:
<pre>
MYFOOD="chickensoup"
$ echo ${MYFOOD%%soup}
chicken
</pre>
In this example, it doesn't matter whether we use "%%" or "%", since only one match is possible. And remember, if you forget whether to use "#" or "%", look at the 3, 4, and 5 keys on your keyboard and figure it out.


We can use another form of variable expansion to select a specific substring, based on a specific character offset and length. Try typing in the following lines under bash:
...und hier steht wie du ein XFS root Dateisystem erstellen kannst, falls du XFS wählst:
<pre>
$ EXCLAIM=cowabunga
$ echo ${EXCLAIM:0:3}
cow
$ echo ${EXCLAIM:3:7}
abunga
</pre>
This form of string chopping can come in quite handy; simply specify the character to start from and the length of the substring, all separated by colons.


=== Applying string chopping ===
<console>
Now that we've learned all about chopping strings, let's write a simple little shell script. Our script will accept a single file as an argument, and will print out whether it appears to be a tarball. To determine if it is a tarball, it will look for the pattern ".tar" at the end of the file. Here it is:
# ##i##mkfs.xfs /dev/sda3
<syntaxhighlight lang="bash">
</console>
#!/bin/bash


if [ "${1##*.}" = "tar" ]
Deine Dateisysteme (und swap) wurden nun initialisiert, so dass sie nun eingehängt werden können (angehängt an deine existierende Verzeichnisstruktur) und zum Speichern genutzt werden können. Jetzt sind wir fertig um mit der Funtoo Linux Installation zu beginnen, auf unseren brand neuen Dateisystemen.
then
      echo This appears to be a tarball.
else
      echo At first glance, this does not appear to be a tarball.
fi
</syntaxhighlight>
To run this script, enter it into a file called '''mytar.sh''', and type <span style="color:green">chmod 755 mytar.sh</span> to make it executable. Then, give it a try on a tarball, as follows:
<pre>
$ ./mytar.sh thisfile.tar
This appears to be a tarball.
$ ./mytar.sh thatfile.gz
At first glance, this does not appear to be a tarball.
</pre>
OK, it works, but it's not very functional. Before we make it more useful, let's take a look at the "if" statement used above. In it, we have a boolean expression. In bash, the "=" comparison operator checks for string equality. In bash, all boolean expressions are enclosed in square brackets. But what does the boolean expression actually test for? Let's take a look at the left side. According to what we've learned about string chopping, "${1##*.}" will remove the longest match of "*." from the beginning of the string contained in the environment variable "1", returning the result. This will cause everything after the last "." in the file to be returned. Obviously, if the file ends in ".tar", we will get "tar" as a result, and the condition will be true.


You may be wondering what the "1" environment variable is in the first place. Very simple -- $1 is the first command-line argument to the script, $2 is the second, etc. OK, now that we've reviewed the function, we can take our first look at "if" statements.
{{fancywarning|1=
Wenn du mit einen OpenVZ Host arbeitest, nutze bitte nur ext4. Das Parallels Entwicklerteam testet ausschließlich mit ext4 und moderne Versionen des  <code>openvz-rhel6-stable</code> sind '''nicht''' kompatible mit XFS. Es könnten Kernel Bugs auftreten.
}}


=== If statements ===
==== Einhängen der Dateisystemen ====
Like most languages, bash has its own form of conditional. When using them, stick to the format above; that is, keep the "if" and the "then" on separate lines, and keep the "else" and the terminating and required "fi" in horizontal alignment with them. This makes the code easier to read and debug. In addition to the "if,else" form, there are several other forms of "if" statements:
Hänge die neuen Dateisysteme wie folgt ein, erstelle <code>/mnt/funtoo</code> als Einhängepunkt zur Installation:
<syntaxhighlight lang="bash">
if      [ condition ]
then
        action
fi
</syntaxhighlight>
This one performs an action only if condition is true, otherwise it performs no action and continues executing any lines following the "fi".
<syntaxhighlight lang="bash">
if [ condition ]
then
        action
elif [ condition2 ]
then
        action2
.
.
.
elif [ condition3 ]
then


else
<console>
        actionx
# ##i##mkdir /mnt/funtoo
fi
# ##i##mount /dev/sda3 /mnt/funtoo
</syntaxhighlight>
# ##i##mkdir /mnt/funtoo/boot
The above "elif" form will consecutively test each condition and execute the action corresponding to the first true condition. If none of the conditions are true, it will execute the "else" action, if one is present, and then continue executing lines following the entire "if,elif,else" statement.
# ##i##mount /dev/sda1 /mnt/funtoo/boot
</console>
Optional, falls du  separate Dateisysteme für  <code>/home</code> oder alle andere erstellt hast:


=== Next time ===
<console>
Now that we've covered the most basic bash functionality, it's time to pick up the pace and get ready to write some real scripts. In the next article, I'll cover looping constructs, functions, namespace, and other essential topics. Then, we'll be ready to write some more complicated scripts. In the third article, we'll focus almost exclusively on very complex scripts and functions, as well as several bash script design options. See you then!
# ##i##mkdir /mnt/funtoo/home
# ##i##mount /dev/sda4 /mnt/funtoo/home
</console>


== Resources ==
Falls du eine separate Partition für <code>/tmp</code> oder <code>/var/tmp</code> auf einem anderem Dateisystem hast, stelle sicher, dass die Zugriffsrechte des  Einhängepunkt stimmen und er global beschreibbar ist, wie folgt:
* Read [[Bash by Example, Part 2]]
* Read [[Bash by Example, Part 3]]
* Visit [http://www.gnu.org/software/bash/bash.html GNU's bash home page]


__NOTOC__
<console>
[[Category:Linux Core Concepts]]
# ##i##chmod 1777 /mnt/funtoo/tmp
[[Category:Articles]]
</console>
{{ArticleFooter}}

Revision as of 19:23, January 27, 2015


   Note

This is a template that is used as part of the Installation instructions which covers: the process of partitioning and filesystem creation. Templates are being used to allow multiple variant install guides that use most of the same re-usable parts.


Vorbereiten der Festplatte

Diese Sektion handelt über die verschiedenen Möglichkeiten Funtoo Linux auf einer Festplatte zu installieren und zu booten.

Einleitung

Früher gab es nur eine Variante einen PC zu booten, alle Desktop- und Servercomputer hatten einen voreingestellten PC BIOS, alle Festplatten nutzten den Master Boot Record (MBR) um das System zu booten und unsere Festplatten waren mit dem MBR Partitionsschema in verschiedene Regionen partitioniert. Das war einfach wie's gemacht wurde. Und uns gefiel es!

Dann kamen EFI und UEFI, neue firmware designt das System zu booten, gemeinsam mit GTP Partitionstabellen um Partitionen auf Festplatten größer als 2.2TB zu definieren. Plötzlich haben wir eine breite Wahl von Optionen, Linux Systeme zu installieren und zu booten. Damit haben wir nun eine komplexere Situation als damals.

Nehmen wir einen Moment um die verfügbaren Optionen, zur Konfiguration der Festplatte um Linux zu booten, zu besprechen. Diese Installationsanleitung nutzt und empfiehlt die old-school Methode des BIOS bootens mit hilfe des MBR. Es funktioniert und (außer in seltenen Fällen) ist universal unterstützt. Mit dieser Methode ist nichts falsch, solange deine Systemfestplatte nur bis zu 2TB groß ist. Solange wird diese Methode die volle Kapazität deiner Festplatte nutzen.

Es gibt aber einige Situationen, in denen diese old-school Methode nicht optimal ist. Falls du eine Systemfestplatte >2TB hast, dann erlauben dir MBR Partitionen keinen Zugang zum gesamten Speicher. Das ist also ein Grund gegen diese Methode. Ein Weiterer ist, dass es "PC" Systeme gibt, welche das booten via BIOS nicht mehr unterstützen und dich zwingen via UEFI zu booten. Aus Mitleid für die PC-Nutzer, die in diese Zwickmühle geraten, decken wir das Booten via UEFI zusätzlich in dieser Installationsanleitung ab .

Unsere empfehlung ist immer noch die old-school Methode, es seiden du hast Gründe dagegen. Der Bootloader, den wir nutzen um den Linux Kernel zu laden, heißt GRUB. Also nennen wir die Methode BIOS + GRUB(MBR) Methode. Es ist die traditionelle Methode um ein Linux System bootbar zu machen.

Falls du via UEFI booten willst, empfehlen wir dir nicht den MBR zum booten zu nutzen, was nur manche Systeme unterstützen, sondern wir empfehlen UEFI zu nutzen um GRUB zu laden. GRUB wird dann das Linux System booten. Wir referenzieren zu dieser Methode mit UEFI + GRUB (GPT).

Und ja, es gibt noch weitere Methoden, von denen einige auf der Boot Methods Seite dokumentiert sind. Unsere Empfehlung war immer die 'BIOS + GRUB (GPT) Methode, welche allerdings nun nicht mehr konsistent und hardwareübergreifend unterstützt wird.

Die größte Frage ist immer -- Welche Bootmethode sollst du nutzen? Hier ist mein Gedankengang.

Grundsatz 1 - Old School
Falls du verlässlich via System Rescue CD booten kannst und dir ein leicht blaues Menü angezeigt wird, dann bootet die CD via BIOS und es ist sehr wahrscheinlich, das du auch Funtoo Linux via BIOS booten kannst. Also gehe old-school und nutze diese Methode, es sei denn du hast Gründe via UEFI zu booten. Zum Beispiel eine Systemfestplatte >2.2TB In diesem Fall beachte Grundsatz 2, wenn dein System UEFI unterstützt.
Grundsatz 2 - New School
Falls du verlässlich via System Rescue CD booten kannst und dir ein schwarz und weißes Menü, --Glückwunsch, dein System ist konfiguriert UEFI zu unterstützen. Das bedeutet das du bereit bist Funtoo Linux einzurichten um via UEFI zu booten. Dein System könnte immer noch das Booten übers BIOS unterstützen, aber versuch es einfach mal mit UEFI als erstes. Du kannst in deiner BIOS Konfiguration herum stochern und damit spielen.
Was ist der große Unterschied zwischen Old School und New School?
Hier ist der Deal. Falls du mit old-school MBR Partitionen gehst, deine /boot Partition wird ein ext2 Dateisystem haben, und du wirst fdisknutzen um MBR Partitionen zu erstellen. Fallse du mit new-school GPT Partitionen und booten via UEFI gehst, wird deine /boot Partition ein vfat Dateisystem haben, da UEFI dies lesen kann,außerdem wirst du gdisk nutzen um GPT Partitionen zu erstellen. Und du wirst GRUB ein wenig anders installieren. Das ist alles was es zu wissen gibt, für den Fall das du neugierig warst.
Notiere auch
Um Funtoo Linux via new-school UEFI Methode bootbar zu machen ist es notwendig, dass die System Rescue CD via UEFI gebooted wurde -- und du den ursprünglichen schwarz und weißen Bildschirm siehst. Ansonsten wird UEFI nicht aktiv sein und du wirst nicht in der Lage sein Funtoo Linux korrekt zu installieren.
   Note

Einige Motherboards unterstützen UEFI nicht richtig. Informiere dich. Zum Beispiel, das Award BIOS in meinem Gigabyte GA-990FXA-UD7 rev 1.1 hat eine Option das Booten via UEFI für CD/DVD zu aktivieren. Das ist aber nicht ausreichend um UEFI für Festplatten zu nutzen und Funtoo Linux zu installieren. UEFI muss für entfernbare Datenträger und fixierte Datenträger unterstützt werden. (Damit du deine neue Funtoo Installation booten kannst) Tatsächlich haben die neueren Revisionen des Boards(rev 3.0) volle UEFI unterstützung. Das könnte der dritte Grundsatze sein -- kenne die Hardware.

Old-School (BIOS/MBR) Methode

   Note

Falls du via System Rescue CD booten kannst und dir ein leicht blaues Menü angezeigt wird nutze diese Methode. Falls du die new-school Methode nutzen wirst, Klicke hier um direkt zu UEFI/GPT zu springen.

Vorbereitung

Erstens, es ist ein gute Idee sicherzustellen, dass du die richtige Festplatte zum Partitionieren gefunden hast. Versuche es mit diesem Befehl und verifiziere, dass /dev/sda die Festplatte zum Partitionieren ist:

root # fdisk -l /dev/sda

Disk /dev/sda: 640.1 GB, 640135028736 bytes, 1250263728 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt


root #         Start          End    Size  Type            Name
 1         2048   1250263694  596.2G  Linux filesyste Linux filesystem

Nun, ist es empfohlen, das du alle existierende MBR oder GTP Partitionstabellen auf der Festplatte, welche das System beim Booten verwirren könnten löschst. Wir machen dies mit sgdisk:

   Warning

Dies wird deine existierende Partitionen unerreichbar machen! Du bist "stark" gewarnt und geraten alle wichtigen/kritischen Daten vorher zu sichern.

root # sgdisk --zap-all /dev/sda

Creating new GPT entries.
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.

Diese Ausgabe ist nichts beängstigendes, da der Befehl trotzdem erfolgreich war:

***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. 
***************************************************************
Partitionierung

Nun werden wir fdisk nutzen um die MBR Partitionstabelle und die Partitionen zu erstellen:

root # fdisk /dev/sda

Innerhalb fdisk, folge diesen Schritten:

Leere die Partitionstabelle:

Command (m for help): o ↵

Erstelle Partition 1 (boot):

Command (m for help): n ↵
Partition type (default p): 
Partition number (1-4, default 1): 
First sector: 
Last sector: +128M ↵

Erstelle Partition 2 (swap):

Command (m for help): n ↵
Partition type (default p): 
Partition number (2-4, default 2): 
First sector: 
Last sector: +2G ↵
Command (m for help): t ↵ 
Partition number (1,2, default 2): 
Hex code (type L to list all codes): 82 ↵

Erstelle root Partition:

Command (m for help): n ↵
Partition type (default p): 
Partition number (3,4, default 3): 
First sector: 
Last sector: 

Verifiziere die Partitionstabelle:

Command (m for help): p

Disk /dev/sda: 298.1 GiB, 320072933376 bytes, 625142448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x82abc9a6

Device    Boot     Start       End    Blocks  Id System
/dev/sda1           2048    264191    131072  83 Linux
/dev/sda2         264192   4458495   2097152  82 Linux swap / Solaris
/dev/sda3        4458496 625142447 310341976  83 Linux

Schreibe die Partitionstabelle auf die Festplatte:

Command (m for help): w

Deine neue MBR Partitionstabelle wird nun auf die Systemfestplatte geschrieben.

   Note

Du bist noch nicht fertig mit der Partitionierung! Jetzt, springe zu Creating filesystems.

New-School (UEFI/GPT) Methode

   Note

Falls du verlässlich via System Rescue CD booten kannst und dir ein schwarz und weißes Menü angezeigt wurde, verwende diese Methode. Ansonsten diese Methode wird nicht funktionieren!

Die gdiskBefehle um GTP Partitionstabellen zu erstellen lauten wie folgt. Passe die Große jeweils an, falls notwendig, diese Voreinstellungen werden für die meisten Nutzer funktionieren.Beginne gdisk:

root # gdisk /dev/sda

Innerhalb gdisk, folge diesen Schritten:

Erstelle eine neue leere Partitionstabelle (Dies wird alle Daten löschen!):

Command: o ↵
This option deletes all partitions and creates a new protective MBR.
Proceed? (Y/N): y ↵

Erstelle Partition 1 (boot):

Command: n ↵
Partition Number: 1 ↵
First sector: 
Last sector: +500M ↵
Hex Code: 

Erstelle Partition 2' (swap):

Command: n ↵
Partition Number: 2 ↵
First sector: 
Last sector: +4G ↵
Hex Code: 8200 ↵

Erstelle Partition 3 (root):

Command: n ↵
Partition Number: 3 ↵
First sector: 
Last sector:  (for rest of disk)
Hex Code: 

Während des Prozesses kannst du mit "p" und Enter die aktuelle Partitionstabelle anschauen. Falls du einen Fehler machst "d" eine existierende Partition löschen. Wenn du fertig bist mit deiner Partitionierung kannst du mit "w" die Konfiguration auf die Festplatte schreiben lassen.

Schreibe Partitionstabelle auf die Festplatte:

Command: w ↵
Do you want to proceed? (Y/N): Y ↵

Die Partitionstabelle wird nun auf die Festplatte geschrieben und gdisk wird beendet.

Nun, wurden deine GTP/GUID Partitionen erstellt und sie werden gezeigt wie folgt under block devices unter Linux:


  • /dev/sda1, wird für das /boot Dateisystem genutzt,
  • /dev/sda2, wird für den swap Speicher genutzt, und
  • /dev/sda3, wird für das root Dateisystem verwendet.

Erstelle Dateisysteme

   Note

Dieser Abschnitt behandelt BIOS und UEFI Installationen. Nicht Überspringen!

Bevor du die neuen Partitionen nutzen kannst, müssen die Partitionen mit Dateisystem metadata initialisiert werden. Dieser Prozess ist bekannt als "Erstellen des Dateisystems". Nachdem die Dateisysteme erstellt wurden kann die Festplatte eingebunden werden und Daten speichern.

Lass es uns simple halten. Nutzt du old-school MBR Partitionen? Falls ja, erstellen wir ein ext2 Dateisystem unter /dev/sda1:

root # mkfs.ext2 /dev/sda1

Falls du new-school GPT Partitionen für UEFI nutzt, erstellst du ein vfat Dateisystem unter /dev/sda1, da UEFI dieses lesen kann:


root # mkfs.vfat -F 32 /dev/sda1

Nun, lass uns eine swap Partition erstellen. Die Partition wird genutzt als Überlauf (virtueller Zwischenspeicher) , wenn dein RAM ausgelastet ist oder wenn du dein System in den Ruhezustand versetzen willst.

Du wirst kein Dateisystem auf der swap Partition erstellen, denn swap wird nicht zum Speichern von Datein genutzt, aber es ist notwendig eine swap Partition zu initialisieren via dem mkswap Befehl. Dann werden wir den Befehl swapon ausführen, damit ist die swap Partition sofort aktiv innerhalb deiner aktuellem (live CD) Umgebung. Für den Fall das der swap im späterem Installationsprozess benötigt wird:

root # mkswap /dev/sda2
root # swapon /dev/sda2

Nun, müssen wir das root Dateisystem erstellen. Dort wird das Wurzelverzeichnis von Funtoo Linux leben. Wir empfehlen im allgemeinen ext4 oder XFS als root Dateisystem. Bist du nicht sicher wähle ext4. Hier steht wie man ein ext4 root Dateisystem erstellt:

root # mkfs.ext4 /dev/sda3

...und hier steht wie du ein XFS root Dateisystem erstellen kannst, falls du XFS wählst:

root # mkfs.xfs /dev/sda3

Deine Dateisysteme (und swap) wurden nun initialisiert, so dass sie nun eingehängt werden können (angehängt an deine existierende Verzeichnisstruktur) und zum Speichern genutzt werden können. Jetzt sind wir fertig um mit der Funtoo Linux Installation zu beginnen, auf unseren brand neuen Dateisystemen.

   Warning

Wenn du mit einen OpenVZ Host arbeitest, nutze bitte nur ext4. Das Parallels Entwicklerteam testet ausschließlich mit ext4 und moderne Versionen des openvz-rhel6-stable sind nicht kompatible mit XFS. Es könnten Kernel Bugs auftreten.

Einhängen der Dateisystemen

Hänge die neuen Dateisysteme wie folgt ein, erstelle /mnt/funtoo als Einhängepunkt zur Installation:

root # mkdir /mnt/funtoo
root # mount /dev/sda3 /mnt/funtoo
root # mkdir /mnt/funtoo/boot
root # mount /dev/sda1 /mnt/funtoo/boot

Optional, falls du separate Dateisysteme für /home oder alle andere erstellt hast:

root # mkdir /mnt/funtoo/home
root # mount /dev/sda4 /mnt/funtoo/home

Falls du eine separate Partition für /tmp oder /var/tmp auf einem anderem Dateisystem hast, stelle sicher, dass die Zugriffsrechte des Einhängepunkt stimmen und er global beschreibbar ist, wie folgt:

root # chmod 1777 /mnt/funtoo/tmp