Resize root disk

GCE預設給定的linux root disk大小為10GB,我們可以透過image或snapshot的方式產生大於10G的disk,最後透過resize動作把實際空間擴大。操作的原理是透過fdisk與resize2fs兩個指令來改變磁碟起始與結束位置,然後再做個resize生效的動作...

STEP1: 
使用image或snapshot來建立OS root disk


peixindeMacBook-Pro in ~

○ → gcutil adddisk disk-50g --source_image=projects/centos-cloud/global/images/centos-6-v20131120 --size_gb=50

Select a zone:

1: europe-west1-a  (maintenance starts in 94 days)

2: europe-west1-b  (maintenance starts in 3 days)

3: us-central1-a

4: us-central1-b

>>> 3

INFO: Waiting for insert of disk disk-50g. Sleeping for 3s.

INFO: Waiting for insert of disk disk-50g. Sleeping for 3s.

INFO: Waiting for insert of disk disk-50g. Sleeping for 3s.

WARNING: DISK_SIZE_LARGER_THAN_IMAGE_SIZE: Disk size: '50 GB' is larger than image size: '10 GB'. The primary root persistent disk partition size will be equal to the image size. The disk needs to be re-partitioned from within an instance before the additional space can be used.


Table of resources:


+----------+---------------+--------+---------+

| name     | zone          | status | size-gb |

+----------+---------------+--------+---------+

| disk-50g | us-central1-a | READY  |      50 |

+----------+---------------+--------+---------+


Table of operations:


+----------------------------------------+--------+-------------------------------+----------------+

| name                                   | status | insert-time                   | operation-type |

+----------------------------------------+--------+-------------------------------+----------------+

| operation-1394509449338-4f44c885b4bb9- | DONE   | 2014-03-10T20:44:09.338-07:00 | insert         |

| c371e085                               |        |                               |                |

+----------------------------------------+--------+-------------------------------+----------------+


peixindeMacBook-Pro in ~

○ → gcutil listdisks

+-----------------------+---------------+--------+---------+

| name                  | zone          | status | size-gb |

+-----------------------+---------------+--------+---------+

| disk-50g              | us-central1-a | READY  |      50 |

+-----------------------+---------------+--------+---------+


STEP2:使用生成的硬碟建立主機,並指定為boot磁區

peixindeMacBook-Pro in ~

○ → gcutil addinstance server-50g --disk=disk-50g,boot

Select a zone:

1: europe-west1-a  (maintenance starts in 94 days)

2: europe-west1-b  (maintenance starts in 3 days)

3: us-central1-a

4: us-central1-b

>>> 3

Select a machine type:

1: n1-standard-1 1 vCPU, 3.75 GB RAM

2: n1-standard-2 2 vCPUs, 7.5 GB RAM

3: n1-standard-4 4 vCPUs, 15 GB RAM

4: n1-standard-8 8 vCPUs, 30 GB RAM

5: n1-highcpu-2 2 vCPUs, 1.8 GB RAM

6: n1-highcpu-4 4 vCPUs, 3.6 GB RAM

7: n1-highcpu-8 8 vCPUs, 7.2 GB RAM

8: n1-highmem-2 2 vCPUs, 13 GB RAM

9: n1-highmem-4 4 vCPUs, 26 GB RAM

10: n1-highmem-8 8 vCPUs, 52 GB RAM

11: f1-micro 1 vCPU (shared physical core) and 0.6 GB RAM

12: g1-small 1 vCPU (shared physical core) and 1.7 GB RAM

>>> 11

INFO: Waiting for insert of instance server-50g. Sleeping for 3s.

INFO: Waiting for insert of instance server-50g. Sleeping for 3s.


Table of resources:


+------------+---------------+----------------+---------------+---------+

| name       | network-ip    | external-ip    | zone          | status  |

+------------+---------------+----------------+---------------+---------+

| server-50g | 10.240.156.30 | 23.251.144.204 | us-central1-a | RUNNING |

+------------+---------------+----------------+---------------+---------+


Table of operations:


+----------------------------------------+--------+-------------------------------+----------------+

| name                                   | status | insert-time                   | operation-type |

+----------------------------------------+--------+-------------------------------+----------------+

| operation-1394509557116-4f44c8ec76659- | DONE   | 2014-03-10T20:45:57.116-07:00 | insert         |

| 12bc8caf                               |        |                               |                |

+----------------------------------------+--------+-------------------------------+----------------+


peixindeMacBook-Pro in ~

○ → gcutil listinstances

+---------------------+---------------+---------+----------------+-----------------+

| name                | zone          | status  | network-ip     | external-ip     |

+---------------------+---------------+---------+----------------+-----------------+

| server-50g          | us-central1-a | RUNNING | 10.240.156.30  | 23.251.144.204  |

+---------------------+---------------+---------+----------------+-----------------+


STEP3:連進主機進行fdisk

peixindeMacBook-Pro in ~

○ → gcutil ssh server-50g

INFO: Zone for server-50g detected as us-central1-a.

INFO: Running command line: ssh -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i /Users/simonsu/.ssh/google_compute_engine -A -p 22 [email protected] --

Warning: Permanently added '23.251.144.204' (RSA) to the list of known hosts.

-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

[[email protected] ~]$ df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1             9.9G  898M  8.5G  10% /

tmpfs                 296M     0  296M   0% /dev/shm

[[email protected] ~]$ sudo fdisk /dev/sda


The device presents a logical sector size that is smaller than

the physical sector size. Aligning to a physical sector (or optimal

I/O) size boundary is recommended, or performance may be impacted.


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

        switch off the mode (command 'c') and change display units to

        sectors (command 'u').


Command (m for help): p


Disk /dev/sda: 53.7 GB, 53687091200 bytes

4 heads, 32 sectors/track, 819200 cylinders

Units = cylinders of 128 * 512 = 65536 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk identifier: 0x0004a990


  Device Boot      Start         End      Blocks   Id  System

/dev/sda1              17      163825    10483712+  83  Linux


Command (m for help): c

DOS Compatibility flag is not set


Command (m for help): u

Changing display/entry units to sectors


Command (m for help): p


Disk /dev/sda: 53.7 GB, 53687091200 bytes

4 heads, 32 sectors/track, 819200 cylinders, total 104857600 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk identifier: 0x0004a990


  Device Boot      Start         End      Blocks   Id  System

/dev/sda1            2048    20969472    10483712+  83  Linux


Command (m for help): p


Disk /dev/sda: 53.7 GB, 53687091200 bytes

4 heads, 32 sectors/track, 819200 cylinders, total 104857600 sectors

Units = sectors of 1 * 512 = 512 bytes

Sector size (logical/physical): 512 bytes / 4096 bytes

I/O size (minimum/optimal): 4096 bytes / 4096 bytes

Disk identifier: 0x0004a990


  Device Boot      Start         End      Blocks   Id  System

/dev/sda1            2048    20969472    10483712+  83  Linux


Command (m for help): n

Command action

  e   extended

  p   primary partition (1-4)

p

Partition number (1-4): 1

Partition 1 is already defined.  Delete it before re-adding it.


Command (m for help): d

Selected partition 1


Command (m for help): n

Command action

  e   extended

  p   primary partition (1-4)

p

Partition number (1-4): 1

First sector (2048-104857599, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):

Using default value 104857599


Command (m for help): w

The partition table has been altered!


Calling ioctl() to re-read partition table.


WARNING: Re-reading the partition table failed with error 16: Device or resource busy.

The kernel still uses the old table. The new table will be used at

the next reboot or after you run partprobe(8) or kpartx(8)

Syncing disks.

[[email protected] ~]$ df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1             9.9G  898M  8.5G  10% /

tmpfs                 296M     0  296M   0% /dev/shm

[[email protected] ~]$ sudo reboot


Broadcast message from [email protected]

(/dev/pts/0) at 3:48 ...


The system is going down for reboot NOW!

[[email protected] ~]$ Connection to 23.251.144.204 closed by remote host.

Connection to 23.251.144.204 closed.


STEP4:Reboot後,重新連入執行resize2fs指令

peixindeMacBook-Pro in ~

○ → gcutil listinstances

^[[A+---------------------+---------------+---------+----------------+-----------------+

| name                | zone          | status  | network-ip     | external-ip     |

+---------------------+---------------+---------+----------------+-----------------+

| ewant2-server-01    | us-central1-a | RUNNING | 10.240.133.2   | 199.223.235.168 |

+---------------------+---------------+---------+----------------+-----------------+

| iii-ap-server-01    | us-central1-a | RUNNING | 10.240.60.232  | 23.236.60.234   |

+---------------------+---------------+---------+----------------+-----------------+

| iii-ap-server-02    | us-central1-a | RUNNING | 10.240.93.106  | 23.236.60.206   |

+---------------------+---------------+---------+----------------+-----------------+

| jameichan-server-01 | us-central1-a | RUNNING | 10.240.122.253 | 23.251.150.253  |

+---------------------+---------------+---------+----------------+-----------------+

| server-50g          | us-central1-a | RUNNING | 10.240.156.30  | 23.251.144.204  |

+---------------------+---------------+---------+----------------+-----------------+


peixindeMacBook-Pro in ~

○ → gcutil ssh server-50g

INFO: Zone for server-50g detected as us-central1-a.

INFO: Running command line: ssh -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no -i /Users/simonsu/.ssh/google_compute_engine -A -p 22 [email protected] --

Warning: Permanently added '23.251.144.204' (RSA) to the list of known hosts.

-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

[[email protected] ~]$

[[email protected] ~]$

[[email protected] ~]$ df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1             9.9G  898M  8.5G  10% /

tmpfs                 296M     0  296M   0% /dev/shm


[[email protected] ~]$ sudo resize2fs /dev/sda1

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/sda1 is mounted on /; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 4

Performing an on-line resize of /dev/sda1 to 13106944 (4k) blocks.

The filesystem on /dev/sda1 is now 13106944 blocks long.


[[email protected] ~]$ df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda1              50G  908M   46G   2% /

tmpfs                 296M     0  296M   0% /dev/shm

[[email protected] ~]$ exit

logout

Connection to 23.251.144.204 closed.



參考:https://developers.google.com/compute/docs/disks#repartitionrootpd

Comments