1 | #!/bin/bash |
---|
2 | #/** |
---|
3 | #@file Disk.lib |
---|
4 | #@brief Librería o clase Disk |
---|
5 | #@class Disk |
---|
6 | #@brief Funciones para gestión de discos y particiones. |
---|
7 | #@version 0.9 |
---|
8 | #@warning License: GNU GPLv3+ |
---|
9 | #*/ |
---|
10 | |
---|
11 | |
---|
12 | #/** |
---|
13 | # ogCreateCache int_partsize |
---|
14 | #@brief Define la caché local en la partición 4 del disco 1 |
---|
15 | #@param int_partsize tamaño de la partición (en KB) |
---|
16 | #@return (nada, por determinar) |
---|
17 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
18 | #@note Requisitos: sfdisk, parted, awk, sed |
---|
19 | #@version 0.91 - Definición de caché enp OpenGnSys |
---|
20 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
21 | #@date 2010/03/09 |
---|
22 | #*/ ## |
---|
23 | function ogCreateCache () |
---|
24 | { |
---|
25 | # Variables locales. |
---|
26 | local DISK SECTORS CYLS START END SIZE |
---|
27 | # Error si no se recibe 1 parámetro. |
---|
28 | [ $# -eq 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
29 | |
---|
30 | DISK=$(ogDiskToDev 1) || return $? |
---|
31 | PART="${DISK}4" |
---|
32 | SECTORS=$(awk -v D=${DISK#/dev/} '{if ($4==D) {print $3*2}}' /proc/partitions) |
---|
33 | CYLS=$(sfdisk -g $DISK | cut -f2 -d" ") |
---|
34 | END=$[SECTORS/CYLS*CYLS-1] |
---|
35 | SIZE=$(echo $1|awk '{print $0*2}') # En sectores de 512 B. |
---|
36 | START=$[END-SIZE+1] |
---|
37 | ENDPART3=$(sfdisk -us -l $DISK | awk -v P="${DISK}3" '{if ($1==P) print $3}') |
---|
38 | # Error si tamaño 0 o si mayor que la mitad del disco o si pisa la partición 3. |
---|
39 | if [ $SIZE -eq 0 -o $SIZE -ge $[END/2] -o $START -le $ENDPART3 ]; then |
---|
40 | ogRaiseError $OG_ERR_FORMAT "$1" || return $? |
---|
41 | fi |
---|
42 | |
---|
43 | # Crear plantilla de particionado, modificando partición 4 para caché local. |
---|
44 | tmpsfdisk=/tmp/sfdiskout$$ |
---|
45 | trap "rm -f $tmpsfdisk" 1 2 3 9 15 |
---|
46 | sfdisk -d $DISK | sed "s;$PART.*$;$PART : start=$START, size=$SIZE, Id=ca;" >$tmpsfdisk |
---|
47 | |
---|
48 | # Desmontar todos los sistemas de archivos del disco. |
---|
49 | ogUnmountAll 1 2>/dev/null |
---|
50 | # Si la tabla de particiones no es valida, volver a generarla. |
---|
51 | [ $(parted -s $DISK print >/dev/null) ] || fdisk $DISK <<< "w" |
---|
52 | # Definir particiones y notificar al kernel. |
---|
53 | sfdisk -f $DISK < $tmpsfdisk 2>/dev/null && sfdisk -R $DISK |
---|
54 | rm -f $tmpsfdisk |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | #/** |
---|
59 | # ogCreatePartitions int_ndisk str_parttype:int_partsize ... |
---|
60 | #@brief Define el conjunto de particiones de un disco. |
---|
61 | #@param int_ndisk nº de orden del disco |
---|
62 | #@param str_parttype mnemónico del tipo de partición |
---|
63 | #@param int_partsize tamaño de la partición (en KB) |
---|
64 | #@return (nada, por determinar) |
---|
65 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
66 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
67 | #@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize |
---|
68 | #@attention Pueden definirse particiones vacías de tipo \c EMPTY |
---|
69 | #@note Requisitos: sfdisk, parted, partprobe, awk |
---|
70 | #@todo Definir atributos (arranque, oculta) y tamaños en MB, GB, etc. |
---|
71 | #@version 0.9 - Primera versión para OpenGNSys |
---|
72 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
73 | #@date 2009/09/09 |
---|
74 | #@version 0.91 - Corrección del redondeo del tamaño del disco. |
---|
75 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
76 | #@date 2010/03/09 |
---|
77 | #*/ ## |
---|
78 | function ogCreatePartitions () |
---|
79 | { |
---|
80 | # Variables locales. |
---|
81 | local DISK PART SECTORS CYLS START SIZE TYPE EXTSTART EXTSIZE tmpsfdisk |
---|
82 | # Si se solicita, mostrar ayuda. |
---|
83 | if [ "$*" == "help" ]; then |
---|
84 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk str_parttype:int_partsize ..." \ |
---|
85 | "$FUNCNAME 1 NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000" |
---|
86 | return |
---|
87 | fi |
---|
88 | # Error si no se reciben menos de 2 parámetros. |
---|
89 | [ $# -ge 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
90 | |
---|
91 | DISK="$(ogDiskToDev $1)" || return $? |
---|
92 | shift |
---|
93 | |
---|
94 | # Nº total de sectores, para evitar desbordamiento (evitar redondeo). |
---|
95 | SECTORS=$(awk -v D=${DISK#/dev/} '{if ($4==D) {print $3*2}}' /proc/partitions) |
---|
96 | CYLS=$(sfdisk -g $DISK | cut -f2 -d" ") |
---|
97 | SECTORS=$[SECTORS/CYLS*CYLS-1] |
---|
98 | # Sector de inicio de la particon (la 1ª empieza en el sector 63). |
---|
99 | START=63 |
---|
100 | PART=1 |
---|
101 | |
---|
102 | # Fichero temporal de entrada para "sfdisk" |
---|
103 | tmpsfdisk=/tmp/sfdisk$$ |
---|
104 | trap "rm -f $tmpsfdisk" 1 2 3 9 15 |
---|
105 | |
---|
106 | echo "unit: sectors" >$tmpsfdisk |
---|
107 | echo >>$tmpsfdisk |
---|
108 | |
---|
109 | # Generar fichero de entrada para "sfdisk" con las particiones. |
---|
110 | while [ $# -gt 0 ]; do |
---|
111 | # Leer formato de cada parámetro - Tipo:Tamaño |
---|
112 | TYPE="${1%%:*}" |
---|
113 | SIZE="${1#*:}" |
---|
114 | [ -z "$SIZE" ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
115 | # Convertir en sectores de 512 B. |
---|
116 | SIZE=$[SIZE*2] |
---|
117 | [ $SIZE -eq 0 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
118 | # Obtener identificador de tipo de partición. |
---|
119 | ID=$(ogFsToId "$TYPE") |
---|
120 | [ -n "$ID" ] || ogRaiseError $OG_ERR_PARTITION "$TYPE" || return $? |
---|
121 | # Comprobar si la partición es extendida. |
---|
122 | if [ $ID = 5 ]; then |
---|
123 | [ $PART -gt 4 ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
124 | EXTSTART=$START |
---|
125 | EXTSIZE=$SIZE |
---|
126 | fi |
---|
127 | # Incluir particiones lógicas dentro de la partición extendida. |
---|
128 | if [ $PART = 5 ]; then |
---|
129 | [ -z "$EXTSTART" ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
130 | START=$EXTSTART |
---|
131 | SECTORS=$[EXTSTART+EXTSIZE] |
---|
132 | fi |
---|
133 | # Generar datos para la partición. |
---|
134 | echo "$DISK$PART : start=$START, size=$SIZE, Id=$ID" >>$tmpsfdisk |
---|
135 | # Error si se supera el nº total de sectores. |
---|
136 | START=$[START+SIZE] |
---|
137 | [ $START -gt $SECTORS ] && ogRaiseError $OG_ERR_FORMAT && return $? |
---|
138 | PART=$[PART+1] |
---|
139 | shift |
---|
140 | done |
---|
141 | # Si no se indican las 4 particiones primarias, definirlas como vacías. |
---|
142 | while [ $PART -le 4 ]; do |
---|
143 | echo "$DISK$PART : start=0, size=0, Id=0" >>$tmpsfdisk |
---|
144 | PART=$[PART+1] |
---|
145 | done |
---|
146 | # Si se define partición extendida sin lógicas, crear particion 5 vacía. |
---|
147 | if [ $PART = 5 -a -n "$EXTSTART" ]; then |
---|
148 | echo "${DISK}5 : start=$EXTSTART, size=$EXTSIZE, Id=0" >>$tmpsfdisk |
---|
149 | fi |
---|
150 | |
---|
151 | # Desmontar todos los sistemas de archivos del disco. |
---|
152 | ogUnmountAll $1 |
---|
153 | # Si la tabla de particiones no es valida, volver a generarla. |
---|
154 | [ $(parted -s $DISK print >/dev/null) ] || fdisk $DISK <<< "w" |
---|
155 | # Definir particiones y notificar al kernel. |
---|
156 | sfdisk -f $DISK < $tmpsfdisk 2>/dev/null && sfdisk -R $DISK |
---|
157 | rm -f $tmpsfdisk |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | #/** |
---|
162 | # ogDevToDisk path_device |
---|
163 | #@brief Devuelve el nº de orden de dicso (y partición) correspondiente al nombre de fichero de dispositivo. |
---|
164 | #@param path_device Camino del fichero de dispositivo. |
---|
165 | #@return int_ndisk (para dispositivo de disco) |
---|
166 | #@return int_ndisk int_npartition (para dispositivo de partición). |
---|
167 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
168 | #@exception OG_ERR_NOTFOUND Dispositivo no detectado. |
---|
169 | #@note Requisitos: awk |
---|
170 | #@version 0.9 - Primera versión para OpenGNSys |
---|
171 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
172 | #@date 2009-07-20 |
---|
173 | #*/ ## |
---|
174 | function ogDevToDisk () |
---|
175 | { |
---|
176 | # Variables locales. |
---|
177 | local d n |
---|
178 | # Si se solicita, mostrar ayuda. |
---|
179 | if [ "$*" == "help" ]; then |
---|
180 | ogHelp "$FUNCNAME" "$FUNCNAME path_device" \ |
---|
181 | "$FUNCNAME /dev/sda => 1 1" |
---|
182 | return |
---|
183 | fi |
---|
184 | |
---|
185 | # Error si no se recibe 1 parámetro. |
---|
186 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
187 | # Error si no es fichero de bloques. |
---|
188 | [ -b "$1" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
---|
189 | |
---|
190 | # Procesa todos los discos para devolver su nº de orden y de partición. |
---|
191 | n=1 |
---|
192 | for d in $(ogDiskToDev); do |
---|
193 | [ -n "$(echo $1 | grep $d)" ] && echo "$n ${1#$d}" && return |
---|
194 | n=$[n+1] |
---|
195 | done |
---|
196 | ogRaiseError $OG_ERR_NOTFOUND "$1" |
---|
197 | return $OG_ERR_NOTFOUND |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | #/** |
---|
202 | # ogDiskToDev [int_ndisk [int_npartition]] |
---|
203 | #@brief Devuelve la equivalencia entre el nº de orden del dispositivo (dicso o partición) y el nombre de fichero de dispositivo correspondiente. |
---|
204 | #@param int_ndisk nº de orden del disco |
---|
205 | #@param int_npartition nº de orden de la partición |
---|
206 | #@return Para 0 parametros: Devuelve los nombres de ficheros de los dispositivos sata/ata/usb linux encontrados. |
---|
207 | #@return Para 1 parametros: Devuelve la ruta del disco duro indicado. |
---|
208 | #@return Para 2 parametros: Devuelve la ruta de la particion indicada. |
---|
209 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
210 | #@exception OG_ERR_NOTFOUND Dispositivo no detectado. |
---|
211 | #@note Requisitos: awk, lvm |
---|
212 | #@version 0.9 - Primera versión para OpenGNSys |
---|
213 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
214 | #@date 2009-07-20 |
---|
215 | #*/ ## |
---|
216 | function ogDiskToDev () |
---|
217 | { |
---|
218 | # Variables locales |
---|
219 | local ALLDISKS VOLGROUPS DISK PART |
---|
220 | |
---|
221 | # Si se solicita, mostrar ayuda. |
---|
222 | if [ "$*" == "help" ]; then |
---|
223 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk [int_npartition]" \ |
---|
224 | "$FUNCNAME => /dev/sda /dev/sdb" \ |
---|
225 | "$FUNCNAME 1 => /dev/sda" \ |
---|
226 | "$FUNCNAME 1 1 => /dev/sda1" |
---|
227 | return |
---|
228 | fi |
---|
229 | |
---|
230 | # Listar dispositivo para los discos duros (tipos: 3=hd, 8=sd). |
---|
231 | ALLDISKS=$(awk '($1==3 || $1==8) && $4!~/[0-9]/ {printf "/dev/%s ",$4}' /proc/partitions) |
---|
232 | VOLGROUPS=$(vgs -a --noheadings 2>/dev/null | awk '{printf "/dev/%s ",$1}') |
---|
233 | ALLDISKS="$ALLDISKS $VOLGROUPS" |
---|
234 | |
---|
235 | # Mostrar salidas segun el número de parametros. |
---|
236 | case $# in |
---|
237 | 0) # Muestra todos los discos, separados por espacios. |
---|
238 | echo $ALLDISKS |
---|
239 | ;; |
---|
240 | 1) # Error si el parámetro no es un digito. |
---|
241 | [ -z "${1/[1-9]/}" ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
242 | DISK=$(echo "$ALLDISKS" | awk -v n=$1 '{print $n}') |
---|
243 | # Error si el fichero no existe. |
---|
244 | [ -e "$DISK" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
---|
245 | echo "$DISK" |
---|
246 | ;; |
---|
247 | 2) # Error si los 2 parámetros no son digitos. |
---|
248 | [ -z "${1/[1-9]/}" -a -z "${2/[1-9]/}" ] || ogRaiseError $OG_ERR_FORMAT|| return $? |
---|
249 | DISK=$(echo "$ALLDISKS" | awk -v n=$1 '{print $n}') |
---|
250 | [ -e "$DISK" ] || ogRaiseError $OG_ERR_NOTFOUND "$1" || return $? |
---|
251 | PART="$DISK$2" |
---|
252 | # Comprobar si es partición. |
---|
253 | if [ -b "$PART" ]; then |
---|
254 | echo "$PART" |
---|
255 | elif [ -n "$VOLGROUPS" ]; then |
---|
256 | # Comprobar si volumen lógico. /* (comentario Doxygen) |
---|
257 | PART=$(lvscan -a 2>/dev/null | grep "'$DISK/" | awk -v n=$2 -F\' '{if (NR==n) print $2}') |
---|
258 | [ -e "$PART" ] || ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $? |
---|
259 | # (comentario Doxygen) */ |
---|
260 | echo "$PART" |
---|
261 | else |
---|
262 | ogRaiseError $OG_ERR_NOTFOUND "$1 $2" || return $? |
---|
263 | fi |
---|
264 | ;; |
---|
265 | *) # Formato erroneo. |
---|
266 | ogRaiseError $OG_ERR_FORMAT |
---|
267 | return $OG_ERR_FORMAT |
---|
268 | ;; |
---|
269 | esac |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | #/** |
---|
274 | # ogFsToId str_fstype |
---|
275 | #@brief Devuelve el identificador de partición correspondiente a un tipo de sistema de archivos. |
---|
276 | #@param str_fstype mnemónico de tipo de sistema de archivos |
---|
277 | #@return int_idpart nº identificador de tipo de partición. |
---|
278 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
279 | #@version 0.9 - Primera versión para OpenGNSys |
---|
280 | #@author Ramon Gomez, ETSII Universidad Sevilla |
---|
281 | #@date 2009-12-14 |
---|
282 | #*/ ## |
---|
283 | function ogFsToId () |
---|
284 | { |
---|
285 | # Variables locales |
---|
286 | local ID |
---|
287 | |
---|
288 | # Si se solicita, mostrar ayuda. |
---|
289 | if [ "$*" == "help" ]; then |
---|
290 | ogHelp "$FUNCNAME" "$FUNCNAME str_fstype" "$FUNCNAME EXT3 => 83" |
---|
291 | return |
---|
292 | fi |
---|
293 | # Error si no se recibe 1 parámetro. |
---|
294 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
295 | |
---|
296 | # Asociar id. de partición para su mnemónico de sistema de archivos. |
---|
297 | case "$1" in |
---|
298 | EMPTY) ID=0 ;; |
---|
299 | FAT12) ID=1 ;; |
---|
300 | EXTENDED) ID=5 ;; |
---|
301 | FAT16) ID=6 ;; |
---|
302 | NTFS|EXFAT) ID=7 ;; |
---|
303 | FAT32) ID=b ;; |
---|
304 | HFAT12) ID=11 ;; |
---|
305 | HFAT16) ID=16 ;; |
---|
306 | HNTFS) ID=17 ;; |
---|
307 | HFAT32) ID=1b ;; |
---|
308 | LINUX-SWAP) ID=82 ;; |
---|
309 | EXT[234]|REISERFS|REISER4|XFS|JFS) |
---|
310 | ID=83 ;; |
---|
311 | LINUX-LVM) ID=8e ;; |
---|
312 | SOLARIS) ID=bf ;; |
---|
313 | CACHE) ID=ca ;; |
---|
314 | LINUX-RAID) ID=fd ;; |
---|
315 | *) ID="" ;; |
---|
316 | esac |
---|
317 | echo $ID |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | #/** |
---|
322 | # ogGetPartitionActive int_ndisk |
---|
323 | #@brief Muestra que particion de un disco esta marcada como de activa. |
---|
324 | #@param int_ndisk nº de orden del disco |
---|
325 | #@return Nº de partición activa |
---|
326 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
327 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
328 | #@note Requisitos: parted |
---|
329 | #@todo Queda definir formato para atributos (arranque, oculta, ...). |
---|
330 | #@version 0.9 - Primera versión compatible con OpenGNSys. |
---|
331 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
332 | #@date 2009/07/24 |
---|
333 | #*/ ## |
---|
334 | function ogGetPartitionActive () |
---|
335 | { |
---|
336 | # Variables locales |
---|
337 | local DISK |
---|
338 | |
---|
339 | # Si se solicita, mostrar ayuda. |
---|
340 | if [ "$*" == "help" ]; then |
---|
341 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" "$FUNCNAME 1 => 1" |
---|
342 | return |
---|
343 | fi |
---|
344 | # Error si no se recibe 1 parámetro. |
---|
345 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
346 | |
---|
347 | # Comprobar que el disco existe y listar su partición activa. |
---|
348 | DISK="$(ogDiskToDev $1)" || return $? |
---|
349 | parted $DISK print 2>/dev/null | awk '/boot/ {print $1}' |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | #/** |
---|
354 | # ogGetPartitionId int_ndisk int_npartition |
---|
355 | #@brief Devuelve el mnemonico con el tipo de sistema de archivos. |
---|
356 | #@param int_ndisk nº de orden del disco |
---|
357 | #@param int_npartition nº de orden de la partición |
---|
358 | #@return Identificador de tipo de partición. |
---|
359 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
360 | #@exception OG_ERR_NOTFOUND Disco o particion no corresponden con un dispositivo. |
---|
361 | #@note Requisitos: sfdisk |
---|
362 | #@version 0.9 - Primera versión compatible con OpenGNSys. |
---|
363 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
364 | #@date 25/03/2009 |
---|
365 | #*/ ## |
---|
366 | function ogGetPartitionId () |
---|
367 | { |
---|
368 | # Variables locales. |
---|
369 | local DISK PART |
---|
370 | |
---|
371 | # Si se solicita, mostrar ayuda. |
---|
372 | if [ "$*" == "help" ]; then |
---|
373 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
374 | "$FUNCNAME 1 1 => 7" |
---|
375 | return |
---|
376 | fi |
---|
377 | # Error si no se reciben 2 parámetros. |
---|
378 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
379 | |
---|
380 | # Detectar id. de tipo de particion y codificar al mnemonico. |
---|
381 | DISK=$(ogDiskToDev $1) || return $? |
---|
382 | PART=$(ogDiskToDev $1 $2) || return $? |
---|
383 | echo $(sfdisk --id $DISK $2 2>/dev/null) |
---|
384 | } |
---|
385 | |
---|
386 | |
---|
387 | #/** |
---|
388 | # ogGetPartitionSize int_ndisk int_npartition |
---|
389 | #@brief Muestra el tamano en KB de una particion determinada. |
---|
390 | #@param int_ndisk nº de orden del disco |
---|
391 | #@param int_npartition nº de orden de la partición |
---|
392 | #@return int_partsize - Tamaño en KB de la partición. |
---|
393 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
394 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
395 | #@note Requisitos: sfdisk, awk |
---|
396 | #@version 0.9 - Primera versión para OpenGNSys |
---|
397 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
398 | #@date 2009/07/24 |
---|
399 | #*/ ## |
---|
400 | function ogGetPartitionSize () |
---|
401 | { |
---|
402 | # Variables locales. |
---|
403 | local PART |
---|
404 | |
---|
405 | # Si se solicita, mostrar ayuda. |
---|
406 | if [ "$*" == "help" ]; then |
---|
407 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
408 | "$FUNCNAME 1 1 => 10000000" |
---|
409 | return |
---|
410 | fi |
---|
411 | # Error si no se reciben 2 parámetros. |
---|
412 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
413 | |
---|
414 | # Obtener el tamaño de la partición. |
---|
415 | DISK="$(ogDiskToDev $1)" || return $? |
---|
416 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
417 | case "$(ogGetPartitionId $1 $2)" in |
---|
418 | 5|f) # Procesar detección de tamaño de partición Extendida. |
---|
419 | sfdisk -l $DISK 2>/dev/null | \ |
---|
420 | awk -v p=$PART '{if ($1==p) {sub (/\*/,""); print $5} }' |
---|
421 | ;; |
---|
422 | *) sfdisk -s $PART |
---|
423 | ;; |
---|
424 | esac |
---|
425 | } |
---|
426 | |
---|
427 | |
---|
428 | #/** |
---|
429 | # ogListPartitions int_ndisk |
---|
430 | #@brief Lista las particiones definidas en un disco. |
---|
431 | #@param int_ndisk nº de orden del disco |
---|
432 | #@return str_parttype:int_partsize ... |
---|
433 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
434 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
435 | #@note Requisitos: \c parted \c awk |
---|
436 | #@attention El nº de partición se indica por el orden de los párametros \c parttype:partsize |
---|
437 | #@attention Las tuplas de valores están separadas por espacios. |
---|
438 | #@version 0.9 - Primera versión para OpenGNSys |
---|
439 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
440 | #@date 2009/07/24 |
---|
441 | #*/ ## |
---|
442 | function ogListPartitions () |
---|
443 | { |
---|
444 | # Variables locales. |
---|
445 | local DISK PART NPARTS TYPE SIZE |
---|
446 | |
---|
447 | # Si se solicita, mostrar ayuda. |
---|
448 | if [ "$*" == "help" ]; then |
---|
449 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk" \ |
---|
450 | "$FUNCNAME 1 => NTFS:10000000 EXT3:5000000 LINUX-SWAP:1000000" |
---|
451 | return |
---|
452 | fi |
---|
453 | # Error si no se recibe 1 parámetro. |
---|
454 | [ $# == 1 ] || ogRaiseError $OG_ERR_FORMAT "$FORMAT" || return $? |
---|
455 | |
---|
456 | # Procesar la salida de \c parted . |
---|
457 | DISK="$(ogDiskToDev $1)" || return $? |
---|
458 | NPARTS=$(ogGetPartitionsNumber $1) |
---|
459 | for (( PART = 1; PART <= NPARTS; PART++ )); do |
---|
460 | TYPE=$(ogGetFsType $1 $PART 2>/dev/null) |
---|
461 | if [ $? -eq 0 ]; then |
---|
462 | SIZE=$(ogGetPartitionSize $1 $PART 2>/dev/null) |
---|
463 | echo -n "$TYPE:$SIZE " |
---|
464 | else |
---|
465 | echo -n "EMPTY:0 " |
---|
466 | fi |
---|
467 | done |
---|
468 | echo |
---|
469 | } |
---|
470 | |
---|
471 | |
---|
472 | #/** |
---|
473 | # ogListPrimaryPartitions int_ndisk |
---|
474 | #@brief Metafunción que lista las particiones primarias no vacías definidas en un disco. |
---|
475 | #@param int_ndisk nº de orden del disco |
---|
476 | #@see ogListPartitions |
---|
477 | #*/ ## |
---|
478 | function ogListPrimaryPartitions () |
---|
479 | { |
---|
480 | # Variables locales. |
---|
481 | local PARTS |
---|
482 | |
---|
483 | PARTS=$(ogListPartitions "$@") || return $? |
---|
484 | echo $PARTS | cut -sf1-4 -d" " | sed 's/\( EMPTY:0\)*$//' |
---|
485 | } |
---|
486 | |
---|
487 | |
---|
488 | #/** |
---|
489 | # ogListLogicalPartitions int_ndisk |
---|
490 | #@brief Metafunción que lista las particiones lógicas definidas en un disco. |
---|
491 | #@param int_ndisk nº de orden del disco |
---|
492 | #@see ogListPartitions |
---|
493 | #*/ ## |
---|
494 | function ogListLogicalPartitions () |
---|
495 | { |
---|
496 | # Variables locales. |
---|
497 | local PARTS |
---|
498 | |
---|
499 | PARTS=$(ogListPartitions "$@") || return $? |
---|
500 | echo $PARTS | cut -sf5- -d" " |
---|
501 | } |
---|
502 | |
---|
503 | |
---|
504 | #/** |
---|
505 | # ogSetPartitionActive int_ndisk int_npartition |
---|
506 | #@brief Establece cual es la partición activa de un disco. |
---|
507 | #@param int_ndisk nº de orden del disco |
---|
508 | #@param int_npartition nº de orden de la partición |
---|
509 | #@return (nada). |
---|
510 | #@exception OG_ERR_FORMAT Formato incorrecto. |
---|
511 | #@exception OG_ERR_NOTFOUND Disco o partición no corresponden con un dispositivo. |
---|
512 | #@note Requisitos: parted |
---|
513 | #@version 0.9 - Primera versión compatible con OpenGNSys. |
---|
514 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
515 | #@date 2009/09/17 |
---|
516 | #*/ ## |
---|
517 | function ogSetPartitionActive () |
---|
518 | { |
---|
519 | # Variables locales |
---|
520 | local DISK PART |
---|
521 | |
---|
522 | # Si se solicita, mostrar ayuda. |
---|
523 | if [ "$*" == "help" ]; then |
---|
524 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
525 | "$FUNCNAME 1 1" |
---|
526 | return |
---|
527 | fi |
---|
528 | # Error si no se reciben 2 parámetros. |
---|
529 | [ $# == 2 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
530 | |
---|
531 | # Comprobar que el disco existe y activar la partición indicada. |
---|
532 | DISK="$(ogDiskToDev $1)" || return $? |
---|
533 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
534 | parted -s $DISK set $2 boot on 2>/dev/null |
---|
535 | } |
---|
536 | |
---|
537 | |
---|
538 | #/** |
---|
539 | # ogSetPartitionSize int_ndisk int_npartition int_size |
---|
540 | #@brief Muestra el tamano en KB de una particion determinada. |
---|
541 | #@param int_ndisk nº de orden del disco |
---|
542 | #@param int_npartition nº de orden de la partición |
---|
543 | #@param int_size tamaño de la partición (en KB) |
---|
544 | #@return (nada) |
---|
545 | #@exception OG_ERR_FORMAT formato incorrecto. |
---|
546 | #@exception OG_ERR_NOTFOUND disco o particion no detectado (no es un dispositivo). |
---|
547 | #@note Requisitos: sfdisk, awk |
---|
548 | #@todo Compruebar que el tamaño sea numérico positivo y evitar que pueda solaparse con la siguiente partición. |
---|
549 | #@version 0.9 - Primera versión para OpenGNSys |
---|
550 | #@author Ramon Gomez, ETSII Universidad de Sevilla |
---|
551 | #@date 2009/07/24 |
---|
552 | #*/ ## |
---|
553 | function ogSetPartitionSize () |
---|
554 | { |
---|
555 | # Variables locales. |
---|
556 | local DISK PART SIZE |
---|
557 | |
---|
558 | # Si se solicita, mostrar ayuda. |
---|
559 | if [ "$*" == "help" ]; then |
---|
560 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
561 | "$FUNCNAME 1 1 10000000" |
---|
562 | return |
---|
563 | fi |
---|
564 | # Error si no se reciben 3 parámetros. |
---|
565 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
566 | |
---|
567 | # Obtener el tamaño de la partición. |
---|
568 | DISK="$(ogDiskToDev $1)" || return $? |
---|
569 | PART="$(ogDiskToDev $1 $2)" || return $? |
---|
570 | # Convertir tamaño en KB a sectores de 512 B. |
---|
571 | SIZE=$[$3*2] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
572 | # Usar \c sfdisk para redefinir el tamaño. |
---|
573 | sfdisk -f -uS -N$2 $DISK <<< ",$SIZE" &>/dev/null || ogRaiseError $OG_ERR_PARTITION "$1,$2" || return $? |
---|
574 | } |
---|
575 | |
---|
576 | |
---|
577 | #/** |
---|
578 | # ogUpdatePartitionTable |
---|
579 | #@brief Fuerza al kernel releer la tabla de particiones de los discos duros |
---|
580 | #@param no requiere |
---|
581 | #@return informacion propia de la herramienta |
---|
582 | #@note Requisitos: \c partprobe |
---|
583 | #@warning pendiente estructurar la funcion a opengnsys |
---|
584 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
585 | #@note funcion importada de EAC |
---|
586 | #*/ |
---|
587 | |
---|
588 | function ogUpdatePartitionTable () |
---|
589 | { |
---|
590 | echo "Forzando al kernel la lectura de la tabla de particiones" |
---|
591 | list=`partprobe -s | cut -f1 -d: ` 2>/dev/null |
---|
592 | echo $list > /tmp/disk |
---|
593 | } |
---|
594 | |
---|
595 | |
---|
596 | |
---|
597 | |
---|
598 | function ogGetPartitionsNumber () { |
---|
599 | #/** @function ogGetPartitionsNumber: @brief detecta el numero de particiones del disco duro indicado. |
---|
600 | #@param int_numdisk (indentificado EAC del disco) |
---|
601 | #@return devuelve el numero paritiones del disco duro indicado |
---|
602 | #@warning Salidas de errores no determinada |
---|
603 | #@attention Requisitos: parted |
---|
604 | #@note Notas sin especificar |
---|
605 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
606 | #*/ |
---|
607 | #local disco totalpart |
---|
608 | #disco=`ogDiskToDev $1` |
---|
609 | #totalpart=`parted $disco print | egrep ^" [0123456789] " -c` |
---|
610 | #echo $totalpart |
---|
611 | local DISK |
---|
612 | #/// Contar el nº de veces que aparece el disco en su lista de particiones. |
---|
613 | DISK=$(ogDiskToDev $1) 2>/dev/null |
---|
614 | sfdisk -l $DISK 2>/dev/null | grep -c "^$DISK" |
---|
615 | } |
---|
616 | |
---|
617 | |
---|
618 | function ogDiskToRelativeDev () { |
---|
619 | #/** @function ogDiskToRelativeDev: @brief Traduce los ID de discos o particiones EAC a ID Linux relativos, es decir 1 1 => sda1 |
---|
620 | #@param Admite 1 parametro: $1 int_numdisk |
---|
621 | #@param Admite 2 parametro: $1 int_numdisk $2 int_partition |
---|
622 | #@return Para 1 parametros traduce Discos Duros: Devuelve la ruta relativa linux del disco duro indicado con nomenclatura EAC.........ejemplo: IdPartition 1 => sda |
---|
623 | #@return Para 2 parametros traduce Particiones: Devuelve la ruta relativa linux de la particion indicado con nomenclatura EAC........... ejemplo: IdPartition 2 1 => sdb1 |
---|
624 | #@warning No definidas |
---|
625 | #@attention |
---|
626 | #@note Notas sin especificar |
---|
627 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
628 | #*/ |
---|
629 | |
---|
630 | if [ $# = 0 ] |
---|
631 | then |
---|
632 | Msg "Info: Traduce el identificador del dispositivo EAC a dispositivo linux \n" info |
---|
633 | Msg "Sintaxis1: IdPartition int_disk -----------------Ejemplo1: IdPartition 1 -> sda " example |
---|
634 | Msg "Sintaxis2: IdPartition int_disk int_partition --Ejemplo2: IdPartition 1 2 -> sda2 " example |
---|
635 | |
---|
636 | return |
---|
637 | fi |
---|
638 | #PART="$(Disk|cut -f$1 -d' ')$2" # se comenta esta linea porque doxygen no reconoce la funcion disk y no crea los enlaces y referencias correctas. |
---|
639 | PART=$(ogDiskToDev|cut -f$1 -d' ')$2 |
---|
640 | echo $PART | cut -f3 -d \/ |
---|
641 | } |
---|
642 | |
---|
643 | |
---|
644 | function ogDeletePartitionTable () { |
---|
645 | #/** @function ogDeletePartitionTable: @brief Borra la tabla de particiones del disco. |
---|
646 | #@param $1 opcion A (identificador LINUX) str_ID_linux (/dev/sda) |
---|
647 | #@param $1 opcion B (Identifiador EAC) int_numdiskEAC(1) |
---|
648 | #@return la informacion propia del fdisk |
---|
649 | #@warning no definidos |
---|
650 | #@attention |
---|
651 | #@note |
---|
652 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
653 | #*/ |
---|
654 | if [ $# = 0 ] |
---|
655 | then |
---|
656 | Msg "sintaxis1: ogDeletePartitionTable int_disk" red |
---|
657 | Msg "sintaxis2: ogDeletePartitionTable str_/dev/sdX" red |
---|
658 | return |
---|
659 | fi |
---|
660 | if [ -n "${1%/dev/*}" ] |
---|
661 | then |
---|
662 | dev=`DiskToDev $1` |
---|
663 | else |
---|
664 | dev=$1 |
---|
665 | fi |
---|
666 | echo -ne "o\nw" | fdisk $dev |
---|
667 | } |
---|
668 | |
---|
669 | |
---|
670 | |
---|
671 | function ogSetPartitionId() { |
---|
672 | #/** @function ogSetPartitionId: @brief Cambia el identificador de la particion, pero no su sistema de archivos. |
---|
673 | #@param $1 int_numdiskEAC |
---|
674 | #@param $2 int_numpartitionEAC |
---|
675 | #@param $3 str_tipoPartition admite EXT2 EXT3 NTFS FAT32 SWAP CACHE |
---|
676 | #@return la propia del fdisk |
---|
677 | #@warning no controla los parametros, si se introducen mal o simplemente no se introducen no muestra mensaje |
---|
678 | #@warning Identifica por nombre del sistema de archivos no por número |
---|
679 | #@attention Requisitos: fdisk |
---|
680 | #@note |
---|
681 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
682 | #*/ |
---|
683 | |
---|
684 | # Variables locales |
---|
685 | local DISK PART ID |
---|
686 | |
---|
687 | # Si se solicita, mostrar ayuda. |
---|
688 | if [ "$*" == "help" ]; then |
---|
689 | ogHelp "$FUNCNAME" "$FUNCNAME int_ndisk int_npartition" \ |
---|
690 | "$FUNCNAME 1 1 10000000" |
---|
691 | return |
---|
692 | fi |
---|
693 | # Error si no se reciben 3 parámetros. |
---|
694 | [ $# == 3 ] || ogRaiseError $OG_ERR_FORMAT || return $? |
---|
695 | |
---|
696 | # Sustituye nº de disco por su dispositivo. |
---|
697 | DISK=`ogDiskToDev $1` || return $? |
---|
698 | PART=`ogDiskToDev $1 $2` || return $? |
---|
699 | |
---|
700 | # Elección del tipo de partición. |
---|
701 | ID=$(ogFsToId "$3") |
---|
702 | [ -n "$ID" ] || ogRaiseError $OG_ERR_PARTITION "$TYPE" || return $? |
---|
703 | |
---|
704 | echo -ne "t\n$2\n${ID}\nw\n" | fdisk $DISK 1>/dev/null 2>&1 |
---|
705 | } |
---|
706 | |
---|
707 | |
---|
708 | function ogDeletePartitionsLabels () { |
---|
709 | #/** @function ogDeletePartitionsLabels: @brief Elimina la informacion que tiene el kernel del cliente og sobre los labels de los sistemas de archivos |
---|
710 | #@param No requiere |
---|
711 | #@return Nada |
---|
712 | #@warning |
---|
713 | #@attention Requisitos: comando interno linux rm |
---|
714 | #@note |
---|
715 | #@version 0.1 Date: 27/10/2008 Author Antonio J. Doblas Viso. Universidad de Malaga |
---|
716 | #*/ |
---|
717 | rm /dev/disk/by-label/* # */ COMENTARIO OBLIGATORIO PARA DOXYGEN |
---|
718 | } |
---|
719 | |
---|