quinta-feira, 10 de maio de 2012

Instalando novo HD SATA no Linux pela linha de comando.

Introdução:

Este é um tutorial que resolvi postar pois necessitei recentemente de efetuar uma manobra parecida e não me lembrava dos comandos necessários.

O tutorial está baseado no meu ambiente, porém os comandos são genéricos e acredito funcionar em qualquer distro.

Problema:

Comprei um novo HD de 1 TB e preciso instalar em meu servidor linux. Nele eu rodo um CentOS 5.7 32bits.

Solução:

O primeiro passo é verificar se o CentOS reconheceu os novos HDs, vamos fazer isso utilizando o comando fdisk -l :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@mytracelog ~]# fdisk -l
Disk /dev/sda: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14       13054   104751832+  8e  Linux LVM
Disk /dev/sdb: 137.4 GB, 137438953472 bytes
255 heads, 63 sectors/track, 16709 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
Disk /dev/sdc: 137.4 GB, 137438953472 bytes
255 heads, 63 sectors/track, 16709 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdc doesn't contain a valid partition table


Analisando o resultado, podemos ver que os 2 HDs foram reconhecidos com sucesso. O hd1 foi reconhecido como /dev/sdb e o hd2 como /dev/sdc. Outra informação que pode ser vista é que ainda não foram particionados.

Criando a partição

A criação da partição também é feita pelo fdisk, repare que o fdisk é uma ferramenta interativa, dentro dele será utilizado os comando n e w.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@mytracelog ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
The number of cylinders for this disk is set to 16709.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-16709, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-16709, default 16709): 16709
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.


Para verificar se realmente a partição foi criada:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@mytracelog ~]# fdisk -l
Disk /dev/sda: 107.3 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14       13054   104751832+  8e  Linux LVM
Disk /dev/sdb: 137.4 GB, 137438953472 bytes
255 heads, 63 sectors/track, 16709 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1       16709   134215011   83  Linux
Disk /dev/sdc: 137.4 GB, 137438953472 bytes
255 heads, 63 sectors/track, 16709 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdc doesn't contain a valid partition table


Formatando a partição em ext3

Com a partição criada, é hora de formatar.

Para formatar utilize o mkfs.ext3:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@mytracelog ~]# mkfs.ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
16777216 inodes, 33553752 blocks
1677687 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
1024 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872
Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.


Criando o ponto de montagem

Para criar o ponto de montagem:


Montando a partição

Para montar a partição:
1
[root@mytracelog ~]# mount -t ext3 /dev/sdb1 /hd1

Verificando se a partição foi montada com sucesso:

1
2
3
4
5
6
7
8
9
[root@mytracelog ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       92G   14G   73G  17% /
/dev/sda1              99M   26M   68M  28% /boot
tmpfs                 2.0G  640M  1.4G  33% /dev/shm
D_DRIVE               800G  618G  183G  78% /media/sf_D_DRIVE
/dev/hdc               43M   43M     0 100% /media/VBOXADDITIONS_4.1.6_74713
/dev/sdb1             126G  188M  120G   1% /hd1

Veja nossa nova partição /dev/sdb1 montada em /hd1.

Configurando a montagem automática

Agora vamos automatizar a montagem, não queremos montar essa partição toda vez que iniciarmos o CentOS. Para isso temos que editar o /etc/fstab:
1
[root@mytracelog ~]# vi /etc/fstab

Adicione a seguinte linha:
1
/dev/sdb1   /hd1   ext3   defaults   1   2

Conclusão

Como visto, a instalação de um novo HD SATA no CentOS  envolve diversos passos. Foi apresentado um contexto de instalação de 2 HDs, o hd1 foi utilizado como exemplo, para o hd2 basta seguir os mesmos passos com algumas pequenas modificações. O gerenciamento de partição ficou a cargo do fdisk, a formatação foi feito pelo mkfs.ext3, a criação do ponto de montagem pelo mkdir e a montagem pelo mount.