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