Project

General

Profile

Dynamic disk partitioning » History » Version 10

Han Boetes, 03/22/2017 07:51 AM

1 2 Paul Kelly
h1. Dynamic disk partitioning
2 1 Paul Kelly
3 1 Paul Kelly
h2. Overview
4 1 Paul Kelly
5 5 Corey Osman
When configuring a host for an unattended kickstart build.  It is possible to specify the host's disk layout as an explicit sequence of partions or use a pre-exec dynamic disk layout script. In either case enter the explict partition list or the pre-exec script into the host's disk layout text area, (or choose a predefined configuration.) 
6 1 Paul Kelly
7 1 Paul Kelly
If the text is to be interpreted as a dynamic disk layout then please ensure that the pre-exec shell script contains a comment line starting with "#Dynamic" and that your script writes a static partition table into /tmp/diskpart.cfg. 
8 1 Paul Kelly
9 1 Paul Kelly
If a disk partition table contains /^#Dynamic/ then the kickstart process will execute the text as a bourne shell script as its very first step and will then include the file /tmp/diskpart.cfg into the downloaded kickstart file.
10 1 Paul Kelly
11 1 Paul Kelly
h2. Examples
12 1 Paul Kelly
13 4 Ohad Levy
<pre><code class="c">
14 3 Ohad Levy
#Dynamic - this line tells Foreman this is a script rather then a static layout
15 3 Ohad Levy
#This snippets define the swap partition size, it would generate a partition twice the size of the memory if your physical memory is up to 2GB
16 3 Ohad Levy
#or will create a swap partition with your memory size + 2GB.
17 3 Ohad Levy
18 3 Ohad Levy
#get the actual memory installed on the system and divide by 1024 to get it in MB
19 3 Ohad Levy
act_mem=$((`grep MemTotal: /proc/meminfo | sed 's/^MemTotal: *//'|sed 's/ .*//'` / 1024))
20 3 Ohad Levy
21 3 Ohad Levy
#check if the memory is less than 2GB then swap is double the memory else it is memory plus 2 GB
22 3 Ohad Levy
if [ "$act_mem" -gt 2048 ]; then
23 3 Ohad Levy
    vir_mem=$(($act_mem + 2048))
24 3 Ohad Levy
else
25 3 Ohad Levy
    vir_mem=$(($act_mem * 2))
26 3 Ohad Levy
fi
27 3 Ohad Levy
28 3 Ohad Levy
#copy all the HDD partitions to the temp file for execution
29 3 Ohad Levy
cat <<EOF > /tmp/diskpart.cfg
30 3 Ohad Levy
zerombr yes
31 3 Ohad Levy
clearpart --all --initlabel
32 3 Ohad Levy
part swap --size "$vir_mem"
33 3 Ohad Levy
part /boot --fstype ext3 --size 100 --asprimary
34 3 Ohad Levy
part / --fstype ext3 --size 1024 --grow
35 3 Ohad Levy
EOF
36 7 Charles Gomes
</code></pre>
37 6 Charles Gomes
38 8 Charles Gomes
This next example uses LVM and 4GB swap for real hardware and direct disk and no swap for virtual machines. Virtual machines should use thin-provisioning leaving LVM unnecessary.
39 6 Charles Gomes
<pre><code class="c">
40 6 Charles Gomes
#Dynamic - this line tells Foreman this is a script rather then a static layout
41 6 Charles Gomes
#This snippets define the swap partition size and if a /boot should be used.
42 6 Charles Gomes
#4GB for real hardware or nothing for VM's
43 6 Charles Gomes
virtual=0
44 6 Charles Gomes
45 6 Charles Gomes
#test if it's KVM or QEMU
46 6 Charles Gomes
dmidecode | egrep -i 'vendor' |grep QEMU && virtual=1
47 6 Charles Gomes
cat /proc/cpuinfo |grep QEMU && virtual=1
48 6 Charles Gomes
dmesg |grep "Booting paravirtualized kernel on KVM" && virtual=1
49 6 Charles Gomes
#test if it's vmware
50 6 Charles Gomes
dmidecode | grep -i 'manufacturer' |grep VMware && virtual=1
51 1 Paul Kelly
52 1 Paul Kelly
if [ "$virtual" -eq 1 ]; then
53 8 Charles Gomes
	cat <<EOF > /tmp/diskpart.cfg
54 8 Charles Gomes
zerombr yes
55 8 Charles Gomes
clearpart --all --initlabel
56 8 Charles Gomes
part / --fstype ext4 --size 2048 --grow
57 8 Charles Gomes
EOF
58 6 Charles Gomes
else
59 8 Charles Gomes
	cat <<EOF > /tmp/diskpart.cfg
60 6 Charles Gomes
zerombr yes
61 6 Charles Gomes
clearpart --all --initlabel
62 8 Charles Gomes
part /boot --fstype ext4 --size 200 --asprimary
63 8 Charles Gomes
part pv.01 --ondisk sda --grow --size=40000
64 8 Charles Gomes
volgroup os pv.01
65 8 Charles Gomes
logvol / --fstype=ext4 --name=root --vgname=os --size=40000
66 8 Charles Gomes
logvol swap --name=swap --vgname=os --size=4096
67 6 Charles Gomes
EOF
68 8 Charles Gomes
fi
69 1 Paul Kelly
</pre>
70 9 Han Boetes
71 9 Han Boetes
For Suse Linux this ran into some problems for which are described in this pull request: https://github.com/theforeman/community-templates/pull/321
72 9 Han Boetes
73 9 Han Boetes
Here is an example which implements this solution:
74 9 Han Boetes
<pre>
75 9 Han Boetes
#Dynamic - this line tells Foreman this is a script rather then a static layout
76 9 Han Boetes
<%#
77 9 Han Boetes
kind: ptable
78 9 Han Boetes
name: AutoYaST LVM modified
79 9 Han Boetes
oses:
80 9 Han Boetes
- SLES
81 9 Han Boetes
- OpenSUSE
82 9 Han Boetes
%>
83 9 Han Boetes
84 10 Han Boetes
# All sizes are in Megabytes
85 10 Han Boetes
# Set the static size for /boot
86 10 Han Boetes
BOOT=256
87 10 Han Boetes
88 9 Han Boetes
# Calculate the swap size depending on the memory and the recommendations from redhat.
89 9 Han Boetes
MEM=$(($(sed -n 's/^MemTotal: \+\([0-9]*\) kB/\1/p' /proc/meminfo) / 1024))
90 9 Han Boetes
if [ "$MEM" -lt "2048" ]; then
91 9 Han Boetes
    SWAP=$((MEM * 2))
92 9 Han Boetes
elif [ "$MEM" -gt "2048" ] || [ "$MEM" -le "8192" ]; then
93 9 Han Boetes
    SWAP=$MEM
94 9 Han Boetes
elif [ "$MEM" -ge "8192" ]; then
95 9 Han Boetes
    SWAP=4096
96 9 Han Boetes
fi
97 9 Han Boetes
98 1 Paul Kelly
# Get the whole disksize
99 1 Paul Kelly
DSK=$(($(blockdev --getsize64 /dev/sda)/1048576))
100 10 Han Boetes
# Remove the size reserved for SWAP and /boot from that.
101 10 Han Boetes
DSK=$((DSK - (SWAP + BOOT)))
102 9 Han Boetes
103 9 Han Boetes
cat << EOF > /tmp/diskpart.cfg
104 9 Han Boetes
  <partitioning config:type="list">
105 9 Han Boetes
    <drive>
106 9 Han Boetes
      <device>/dev/sda</device>
107 9 Han Boetes
      <initialize config:type="boolean">true</initialize>
108 9 Han Boetes
      <partitions config:type="list">
109 9 Han Boetes
        <partition>
110 9 Han Boetes
          <create config:type="boolean">true</create>
111 9 Han Boetes
          <filesystem config:type="symbol">ext4</filesystem>
112 9 Han Boetes
          <format config:type="boolean">true</format>
113 1 Paul Kelly
          <mount>/boot</mount>
114 9 Han Boetes
          <partition_id config:type="integer">131</partition_id>
115 9 Han Boetes
          <partition_nr config:type="integer">1</partition_nr>
116 10 Han Boetes
          <size>${BOOT}M</size>
117 9 Han Boetes
          <stripes config:type="integer">1</stripes>
118 9 Han Boetes
          <stripesize config:type="integer">4</stripesize>
119 9 Han Boetes
          <subvolumes config:type="list"/>
120 9 Han Boetes
        </partition>
121 9 Han Boetes
        <partition>
122 9 Han Boetes
          <create config:type="boolean">true</create>
123 9 Han Boetes
          <format config:type="boolean">false</format>
124 9 Han Boetes
          <lvm_group>vg00</lvm_group>
125 9 Han Boetes
          <partition_id config:type="integer">142</partition_id>
126 9 Han Boetes
          <partition_nr config:type="integer">2</partition_nr>
127 9 Han Boetes
          <size>max</size>
128 9 Han Boetes
        </partition>
129 9 Han Boetes
      </partitions>
130 9 Han Boetes
      <pesize></pesize>
131 9 Han Boetes
      <type config:type="symbol">CT_DISK</type>
132 9 Han Boetes
      <use>all</use>
133 9 Han Boetes
    </drive>
134 9 Han Boetes
    <drive>
135 9 Han Boetes
      <device>/dev/vg00</device>
136 9 Han Boetes
      <initialize config:type="boolean">true</initialize>
137 9 Han Boetes
      <partitions config:type="list">
138 9 Han Boetes
        <partition>
139 9 Han Boetes
          <create config:type="boolean">true</create>
140 9 Han Boetes
          <filesystem config:type="symbol">ext4</filesystem>
141 9 Han Boetes
          <format config:type="boolean">true</format>
142 9 Han Boetes
          <lv_name>root</lv_name>
143 9 Han Boetes
          <mount>/</mount>
144 9 Han Boetes
          <size>$((DSK/4))M</size>
145 9 Han Boetes
        </partition>
146 9 Han Boetes
        <partition>
147 9 Han Boetes
          <create config:type="boolean">true</create>
148 9 Han Boetes
          <filesystem config:type="symbol">swap</filesystem>
149 9 Han Boetes
          <format config:type="boolean">true</format>
150 9 Han Boetes
          <lv_name>swap</lv_name>
151 9 Han Boetes
          <mount>swap</mount>
152 9 Han Boetes
          <size>${SWAP}M</size>
153 9 Han Boetes
        </partition>
154 9 Han Boetes
        <partition>
155 9 Han Boetes
          <create config:type="boolean">true</create>
156 9 Han Boetes
          <filesystem config:type="symbol">ext4</filesystem>
157 9 Han Boetes
          <format config:type="boolean">true</format>
158 9 Han Boetes
          <lv_name>usr</lv_name>
159 9 Han Boetes
          <mount>/usr</mount>
160 9 Han Boetes
          <size>$((DSK/4))M</size>
161 9 Han Boetes
        </partition>
162 9 Han Boetes
        <partition>
163 9 Han Boetes
          <create config:type="boolean">true</create>
164 9 Han Boetes
          <filesystem config:type="symbol">ext4</filesystem>
165 9 Han Boetes
          <format config:type="boolean">true</format>
166 9 Han Boetes
          <lv_name>tmp</lv_name>
167 9 Han Boetes
          <mount>/tmp</mount>
168 9 Han Boetes
          <size>$((DSK/16))M</size>
169 9 Han Boetes
        </partition>
170 9 Han Boetes
        <partition>
171 9 Han Boetes
          <create config:type="boolean">true</create>
172 9 Han Boetes
          <filesystem config:type="symbol">ext4</filesystem>
173 9 Han Boetes
          <format config:type="boolean">true</format>
174 9 Han Boetes
          <lv_name>var</lv_name>
175 9 Han Boetes
          <mount>/var</mount>
176 9 Han Boetes
          <size>$((DSK/4))M</size>
177 9 Han Boetes
        </partition>
178 9 Han Boetes
      </partitions>
179 9 Han Boetes
      <type config:type="symbol">CT_LVM</type>
180 9 Han Boetes
      <use>all</use>
181 9 Han Boetes
    </drive>
182 1 Paul Kelly
  </partitioning>
183 9 Han Boetes
EOF
184 9 Han Boetes
185 10 Han Boetes
# See https://github.com/theforeman/foreman/pull/4076 for this little piece of magic.
186 9 Han Boetes
sed '/<\/ntp-client>/ r /tmp/diskpart.cfg' /tmp/profile/autoinst.xml > /tmp/profile/modified.xml
187 9 Han Boetes
</pre>