Thursday 4 March 2010

เปลี่ยน repo มาใช้ในไทย + วิธีสร้าง CentOS Repository ไว้ในองค์กร

ยุคนี้หลายๆคนใช้ CentOS กันเยอะ เวลา yum มันก็จะไปดึงไฟล์จาก repository ตามที่กำหนดไว้
ซึ่งหากติดตั้งแบบปกติไม่ได้มีการแก้ไขค่าใดๆก็จะเป็นการใช้ค่า repo มาตรฐาน คือดึงไฟล์จากต่างประเทศ ซึ่ง IDC บ้านเราก็รู้ๆกันอยู่ว่า bw ต่างประเทศแรงแบบไม่เกรงใจใคร  :dry:

วิธีที่หลายๆคนใช้กันอยู่ก็คือการเปลี่ยน repo มาใช้ในไทยซึ่งที่นิยมก็จะเป็นของ มหาลัยต่างๆ เช่น ku psu กับของ ISP เช่น issp ซึ่งเป็นวิธีที่ง่ายที่สุดเพราะไม่ต้องสร้าง repo เอง วิธีการก็คือแก้ไขไฟล์ /etc/yum.repo.d/CentOS-Base.repo ใหม่ดังนี้

ku mirror: http://mirror1.ku.ac.th
โหลดไฟล์ repo ได้จาก
CentOS 4.x http://mirror1.ku.ac.th/yum-centos/4/CentOS-Base.repo
CentOS 5.x http://mirror1.ku.ac.th/yum-centos/5/CentOS-Base.repo

Code: [Select]
cd /etc/yum.repo.d
mv CentOS-Base.repo CentOS-Base.repo.bak
wget http://mirror1.ku.ac.th/yum-centos/5/CentOS-Base.repo

psu mirror: http://ftp.psu.ac.th/pub/centos
โหลดไฟล์ repo ได้จาก
CentOS 4.x ftp://ftp.psu.ac.th/pub/centosrepo/CentOS4-psu.repo
CentOS 5.x ftp://ftp.psu.ac.th/pub/centosrepo/CentOS5-psu.repo

Code: [Select]
cd /etc/yum.repo.d
mv CentOS-Base.repo CentOS-Base.repo.bak
wget -o CentOS-Base.repo ftp.psu.ac.th/pub/centosrepo/CentOS5-psu.repo

เพียงเท่านี้ก็จะเป็นการเปลี่ยนมาใช้ repo ซึ่งอยู่ในประเทศทำให้ download package ต่างๆได้รวดเร็วขึ้นมาก

จากการใช้งาน repo ในไทย ปัญหาที่พบบ่อยๆคือบางครั้ง repo ก็ down ไม่สามารถโหลดไฟล์ได้
ซึ่งจะนานๆเป็นทีนึง แต่ถ้าเกิดเป็นในช่วงเวลาเร่งด่วนที่ต้องการใช้งาน ก็อาจจะหงุดหงิดเล็กน้อย แต่ก็เป็นที่ยอมรับได้


วิธีการสร้าง Local Repository

สำหรับท่านที่มีเครื่อง CentOS ภายในองค์กรจำนวนมาก การสร้าง local repo server เหมือนกับของ ku หรือ psu ที่แนะนำไปจะช่วยลดการใช้ BW ไปได้มาก เนื่องจากถึงจะเปลี่ยนมาใช้ repo ในไทยแล้วก็ตามแต่ก็อาจต้องโหลดไฟล์ผ่าน lease line หรือ adsl ซึ่งความเร็วไม่มากเท่ากับเครื่องที่วางไว้ที่ IDC นั่นเอง การสร้าง local repo ก็ง่ายๆหลักการก็คือ copy ไฟล์มาให้ครบตามที่เขามีนั้่นเอง แล้วก็แก้ไขไฟล์ CentOS-Base.repo ของเครื่องในองค์กรมาใช้ url เครื่อง local repo ของเราเอง

ซึ่งก่อนอื่นก็ต้องมีเครื่องที่จะทำหน้าที่นี้โดยเป็นเครื่องที่มี service web server เช่น apache
เมื่อได้เครื่องแล้วก็กำหนด virtual host ขึ้นมาสำหรับเก็บไฟล์ mirror ในที่นี้จะยกตัวอย่างเป็น /var/www/html และเรียกผ่านทาง ip 192.168.1.100

ตัวอย่างต่อไปนี้เป็น CentOS 5.x บน x86_64 หากเป็นเวอร์ชั่นอื่นก็ให้เปลี่ยน 5 กับ x86_64 ให้ตรงกับที่ต้องการเช่น 4 กับ i386 เป็นต้น

1. สร้าง folder สำหรับเก็บไฟล์
Code: [Select]
mkdir -p /var/www/html/centos/5/os/x86_64/CentOS
mkdir -p /var/www/html/centos/5/updates/x86_64

2. ไฟล์ใน folder /var/www/html/centos/5/os/x86_64/CentOS คือไฟล์ RPMs จาก CD/DVD นั่นเอง ให้ copy ไฟล์ใน CD/DVD มาใส่ให้ครบ หรือจะโหลดผ่าน internet ก็ได้เช่นกัน แต่การ copy จากแผ่น cd/dvd ที่มีอยู่แล้วจะช่วยลดเวลาไปได้มาก ในกรณีที่มีไฟล์ iso อยู่ในเครื่องก็สามารถ mount ไฟล์ iso เพื่อ copy ไฟล์ได้ด้วยคำสั่ง
Code: [Select]
mkdir /iso
mount -o loop /path/to/centos-cd.iso /iso
ls /iso

จากนั้นสร้าง repo index ด้วยคำสั่ง
Code: [Select]
createrepo /var/www/html/centos/5/os/x86_64* หากไม่มีคำสั่งนี้ก็ติดตั้งด้วยคำสั่ง yum install createrepo ซะก่อน
** เมื่อมี CentOS เวอร์ชั่นใหม่ออกมาก็ให้เอาไฟล์ RPMs ในแผ่น CD/DVD เวอร์ชั่นใหม่มาทับของเดิม

3. Download ไฟล์ updates มาจาก mirror ที่ใดที่หนึ่งตาม list ต่อไปนี้ http://www.centos.org/modules/tinycontent/index.php?id=30 (เลือกที่มี rsync)
Code: [Select]
rsync -avrt rsync://mirrors.kernel.org/centos/5/updates/x86_64 /var/www/html/centos/5/updates/* ดูเรื่องการใส่ / ปิดท้ายให้ดี อันใหนมี / อันใหนไม่มี / จะมีผลกับไฟล์ที่โหลดมาถูกตำแหน่ง

4. เพิ่มคำสั่ง rsync ตามข้อ 3 ใน crontab เพื่อให้ update เรื่อยๆตามต้องการ เช่นวันละ 1 ครั้ง
Code: [Select]
#crontab -e
0 4 * * * rsync -avrt rsync://mirrors.kernel.org/centos/5/updates/x86_64 /var/www/html/centos/5/updates/ > /dev/null 2>&1

5. ทำเช่นเดียวกันกับข้อ 3-4 สำหรับ folder: extras centosplus addons fasttrack ตามต้องการ (ข้อ 5 นี้ หากไม่ได้ใช้ก็ไม่ต้องโหลดก็ได้ เลือกเฉพาะที่ต้องการใช้พอ แล้วที่เหลือก็กำหนดไปใช้ repo ในไทยแทน)
Code: [Select]
rsync -avrt rsync://mirror1.ku.ac.th/centos/5/extras/x86_64 --exclude=debug/ /var/www/html/centos/5/extras/
rsync -avrt rsync://mirror1.ku.ac.th/centos/5/centosplus/x86_64 --exclude=debug/ /var/www/html/centos/5/centosplus/

6. แก้ไขไฟล์ CentOS-Base.repo เพื่อใช้งาน repo ของเราเอง (ไฟล์นี้ก็ทำใส่ไว้ใน local repo เพื่อให้โหลดไปใช้ได้เลย เหมือนกับของ ku กับ psu นั่นแหละ)

Code: [Select]
# CentOS-Base.repo

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
baseurl=http://192.168.1.100/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://192.168.1.100/centos/RPM-GPG-KEY-CentOS-5
# Rpmforge (yum-priority)
# protect=1

#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
baseurl=http://192.168.1.100/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://192.168.1.100/centos/RPM-GPG-KEY-CentOS-5
# Rpmforge (yum-priority)
# protect=1

[addons]
name=CentOS-$releasever - Addons
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=addons
baseurl=http://192.168.1.100/centos/$releasever/addons/$basearch/
gpgcheck=1
gpgkey=http://192.168.1.100/centos/RPM-GPG-KEY-CentOS-5
# Rpmforge (yum-priority)
# protect=0

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
baseurl=http://192.168.1.100/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://192.168.1.100/centos/RPM-GPG-KEY-CentOS-5
# Rpmforge (yum-priority)
# protect=0

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
baseurl=http://192.168.1.100/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://192.168.1.100/centos/RPM-GPG-KEY-CentOS-5
# Rpmforge (yum-priority)
# protect=1

#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
baseurl=http://192.168.1.100/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://192.168.1.100/centos/RPM-GPG-KEY-CentOS-5
# Rpmforge (yum-priority)
# protect=0

เพียงเท่านี้เราก็มี local repo ของเราเอง เวลาติดตั้ง CentOS ใหม่ก็เรียก yum update ได้อย่างรวดเร็ว ไม่เชื่อลองดู

ของผมก็มีสร้างไว้ที่ http://centos.as.in.th (มีเฉพาะ CentOS 5.3 x86_64 ไม่ครบเหมือนของ ku/psu)
จริงๆเครื่องใครวางที่ IDC ก็ rsync มาจาก ku หรือ psu ได้เลย เร็วดีแป๊บเดียวเสร็จ แต่ของผมจะ rsync updates มาจาก ตปท ที่เหลือเอาจาก KU

No comments:

Post a Comment