Linux 學習筆記(硬盤設定,程式後台運行)

前言:

話說最近有興趣去考LPIC 1 的認証,所以在讀課程NDG-Linux 1,以下將記錄一些工作經常用到的命令。

硬碟設定:

新增硬碟:

1. 把新的hard disk 連入linux 系統

2. 查看hard disk資料
fdisk -l & parted -l
=> 2.1 Parted命令
parted /dev/sdb
(parted)mklabel msdos
(parted) mkpart
Partition type? primary/extended? primary 
File system type? [ext2]? ext4 
Start? 1 
End? 20190
(parted) print

3. 格式化hard disk 成為ext4格式
mkfs.ext4 /dev/sdb1
Or
mke4fs -t ext4 /dev/sdb1

4. Mounting New Ext4 Parition in File System

df -hT  #顯示出是否有mount新的盤
=> 新的disk 入/etc/fstab . 下次開機會自動mount新的disk
/dev/sdb1   /mnt/disk2-part1  ext4   defaults    0   0

新增硬碟到Home:

最近在裝Anthos Bare Metal時,發現新版的Admin node需要110GB,但我自己原本的Template虛擬機只有100GB的空間。該死的preflight check一直提醒我Disk大小不夠。很久以前曾經做過,透過延伸LVM的空間來擴大磁碟空間。

找了很久,發現坊間文件有許多的怪方法,好不容易找到一個跟記憶中的方法類似的,但卻是Ubuntu 16.04(我的OS是18.04啊)。結合了其他幾篇方法,終於成功擴充了我的磁碟。因此,快速把實作的步驟記錄在這裡,分享給大家。

1. 先把新的硬碟裝上

2. 重新開機該台VM,重開後,確認已經可以看得到/dev/sdb 


3. 通過fdisk /dev/sdb 切出一顆partition


shawnho@hpe-tpe-admin1:~$ sudo fdisk /dev/sdbWelcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xc8266e6c.Command (m for help): p
Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc8266e6cCommand (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599):Created a new partition 1 of type 'Linux' and of size 50 GiB.Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

 

4. 再tag這個partition為Linux LVM

 

shawnho@hpe-tpe-admin1:~$ sudo fdisk /dev/sdbWelcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
成功後,就可以看到/dev/sdb之外,又多了一顆/dev/sdb1

 

5. 在剛剛partitioned的磁碟,建立physical volume,並且延伸我們的volume group到這個volume上。

 

shawnho@hpe-tpe-admin1:~$ sudo pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
shawnho@hpe-tpe-admin1:~$ sudo vgextend  ubuntu-vg /dev/sdb1
  Volume group "ubuntu-vg" successfully extended

volume group的名稱,可通過vgdisplay的命令查到。

 

shawnho@hpe-tpe-admin1:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <99.00 GiB
  PE Size               4.00 MiB
  Total PE              25343
  Alloc PE / Size       25343 / <99.00 GiB
  Free  PE / Size       0 / 0
  VG UUID               qG2kZl-uNmM-vZrQ-rx3Z-NFm1-bdrH-OemTYG

 

6. 確認目前的磁區與檔案系統,我們決定將新劃分出的空間,掛載給我們的根目錄(/)

 

shawnho@hpe-tpe-admin1:~$ df -m
Filesystem                          1M-blocks  Used Available Use% Mounted on
udev                                  3958     0      3958   0% /dev
tmpfs                                  798     1       797   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv    99269  5801     88384   7% /
tmpfs                                 3989     0      3989   0% /dev/shm
tmpfs                                    5     0         5   0% /run/lock
tmpfs                                 3989     0      3989   0% /sys/fs/cgroup
/dev/sda2                              976    78       832   9% /boot
tmpfs                                  798     0       798   0% /run/user/1000shawnho@hpe-tpe-admin1:~$ sudo lvextend /dev/mapper/ubuntu-vg/ubuntu-lv /dev/sdb1
  skip_dev_dir: Couldn't split up device name ubuntu-vg/ubuntu-lv.
  Size of logical volume ubuntu-vg/ubuntu-lv changed from least than 99.00 GiB (25343 extents) to 148.99 GiB (38142 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

7. 最後再resize 為ext4格式,我們就可以看到,根目錄磁區已經增加了約50GB

shawnho@hpe-tpe-admin1:~$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 13, new_desc_blocks = 19
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 39057408 (4k) blocks long.shawnho@hpe-tpe-admin1:~$ df
Filesystem                        1K-blocks    Used Available Use% Mounted on
udev                                4052356       0   4052356   0% /dev
tmpfs                                816808     804    816004   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 153253392 5939628 140010368   5% /
tmpfs                               4084036       0   4084036   0% /dev/shm
tmpfs                                  5120       0      5120   0% /run/lock
tmpfs                               4084036       0   4084036   0% /sys/fs/cgroup
/dev/sda2                            999320   79192    851316   9% /boot
tmpfs    

Comments