MigrationForCentOS7

目的

使用磁盘克隆的方式快速安装、部署系统。

验证环境

Virt-manager, CentOS 7 ISO安装盘

安装注意事项

分区时选择xfs(CentOS默认), 选择LVM分区。

/images/2017_05_24_15_16_47_460x471.jpg

The disk partition should be Automatically create them.

/images/2017_05_24_15_17_58_595x456.jpg

安装时选择minimum installation. 安装完毕后,关闭虚拟机

安装后调整

复制安装好的硬盘:

$ ls
CentOS5G.qcow2
$ cp CentOS5G.qcow2 Duplicated.qcow2
$ ls
CentOS5G.qcow2  Duplicated.qcow2

创建一个中转盘,一个大小为50G的目标盘, 中转盘用于存放克隆文件,而目标盘则是我们将克隆文件拷贝过去的盘。

$ qemu-img create -f qcow2 Middle.qcow2 16G
Formatting 'Middle.qcow2', fmt=qcow2 size=17179869184 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16
$ qemu-img create -f qcow2 Dest.qcow2 50G
Formatting 'Dest.qcow2', fmt=qcow2 size=53687091200 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16

在virt-manager中,依次添加剩余的三块硬盘.

/images/2017_05_24_14_49_04_614x394.jpg

查看磁盘格局为, 可以看到,vdb和vda其实一样,但因为vdb没有被挂载,所以可以直接从其上往目标盘使用dd进行拷贝:

# fdisk -l

Disk /dev/vda: 8589 MB, 8589934592 bytes, 16777216 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
Disk label type: dos
Disk identifier: 0x000c7c16

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1026047      512000   83  Linux
/dev/vda2         1026048    16777215     7875584   8e  Linux LVM

Disk /dev/vdb: 8589 MB, 8589934592 bytes, 16777216 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
Disk label type: dos
Disk identifier: 0x000c7c16

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1   *        2048     1026047      512000   83  Linux
/dev/vdb2         1026048    16777215     7875584   8e  Linux LVM

Disk /dev/vdc: 17.2 GB, 17179869184 bytes, 33554432 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


Disk /dev/vdd: 53.7 GB, 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

中转盘可以用于制作img文件, 这里我们先不制作img文件,我们直接从复制的盘往目标盘写入:

[root@localhost ~]# dd if=/dev/vdb of=/dev/vdd bs=1M
8192+0 records in
8192+0 records out
8589934592 bytes (8.6 GB) copied, 150.443 s, 57.1 MB/s

现在查看/dev/vdd的磁盘结构,可以看到与/dev/vda/dev/vdb是一样的.

扩展/dev/vdd的分区,从8G 扩展到50G:

# fdisk /dev/vdd
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1,2, default 2): 
Partition 2 is deleted

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (1026048-104857599, default 1026048): 
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-104857599, default 104857599): 
Using default value 104857599
Partition 2 of type Linux and of size 49.5 GiB is set

Command (m for help): 
Command (m for help): p

Disk /dev/vdd: 53.7 GB, 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
Disk label type: dos
Disk identifier: 0x000c7c16

   Device Boot      Start         End      Blocks   Id  System
/dev/vdd1   *        2048     1026047      512000   83  Linux
/dev/vdd2         1026048   104857599    51915776   83  Linux

Command (m for help): t
Partition number (1,2, default 2): 2
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.

现在关闭虚拟机,使用Dest.qcow2文件创建一台新的虚拟机,

/images/2017_05_24_15_00_08_582x284.jpg

扩展分区

进入系统后,看到磁盘分区依然保持为8G下的磁盘格局

# df -HT
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos00-root xfs       7.2G  897M  6.3G  13% /
devtmpfs                  devtmpfs  510M     0  510M   0% /dev
tmpfs                     tmpfs     521M     0  521M   0% /dev/shm
tmpfs                     tmpfs     521M  7.0M  514M   2% /run
tmpfs                     tmpfs     521M     0  521M   0% /sys/fs/cgroup
/dev/vda1                 xfs       521M  131M  391M  26% /boot
tmpfs                     tmpfs     105M     0  105M   0% /run/user/0

使用以下命令,扩展pv(physical volumn):

[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize PFree 
  /dev/vda2  centos00 lvm2 a--  7.51g 40.00m
[root@localhost ~]# pvresize /dev/vda2
  Physical volume "/dev/vda2" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized
[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize  PFree 
  /dev/vda2  centos00 lvm2 a--  49.51g 42.04g

扩展根分区, 注意mapper的名称:

[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize  VFree 
  centos00   1   2   0 wz--n- 49.51g 42.04g
[root@localhost ~]# ls /dev/centos00/
root  swap
[root@localhost ~]# lvextend -l +100%FREE /dev/centos00/root 
  Size of logical volume centos00/root changed from 6.67 GiB (1707 extents) to 48.71 GiB (12469 extents).
  Logical volume root successfully resized.

扩展xfs:

[root@localhost ~]# xfs_growfs /dev/centos00/root
meta-data=/dev/mapper/centos00-root isize=256    agcount=4, agsize=436992 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1747968, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1747968 to 12768256
[root@localhost ~]# df -HT
Filesystem                Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos00-root xfs        53G  897M   52G   2% /
devtmpfs                  devtmpfs  510M     0  510M   0% /dev
tmpfs                     tmpfs     521M     0  521M   0% /dev/shm
tmpfs                     tmpfs     521M  7.0M  514M   2% /run
tmpfs                     tmpfs     521M     0  521M   0% /sys/fs/cgroup
/dev/vda1                 xfs       521M  131M  391M  26% /boot
tmpfs                     tmpfs     105M     0  105M   0% /run/user/0

现在重新启动系统,发现根分区已经扩展到了50G的空间。

vagrant-libvirt issue on ArchLinux

Previously install vagrant-libvirt is a tough task on ArchLinux, thus I do following steps to let my vagrant-libvirt running again on archlinux, steps are listed as following:

Remove the installed vagrant and backup the configuration files.

$ sudo pacman -Rsn vagrant
$ sudo mv ~/.vagrant.d ~/vagrant.d.back
$ sudo rm -rf /opt/vagrant

Install the vagrant-libvirt in AUR repository:

$ yaourt vagrant-libvirt
$ tsocks vagrant plugin install vagrant-libvirt
$ vagrant plugin list
vagrant-libvirt (0.0.40)

Please notice the plugin should also be installed after you have installed the vagrant-libvirt via yaourt.

AddSecondDiskForVagrant

Purpose

For adding second disk to vagrant machine, while vagrant machine is running under virtualbox hypervisor. Following are the steps for doing this.

Steps

Vagrantfile definition:

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|

  config.vm.provider "virtualbox" do |vb|
    # Display the VirtualBox GUI when booting the machine
    #vb.gui = true
      unless File.exist?('./secondDisk.vdi')
        vb.customize ['createhd', '--filename', './secondDisk.vdi', '--variant', 'Fixed', '--size', 10 * 1024]
      end
  
    # Customize the amount of memory on the VM:
    vb.memory = "1024"
    vb.customize ['storageattach', :id,  '--storagectl', 'SATA', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', './secondDisk.vdi']

  end
end

The initial.sh should be run under the vagrant machine has been created:

cat initial.sh
set -e
set -x
echo 'Acquire::http::Proxy "http://192.168.0.121:3142";'>/etc/apt/apt.conf.d/01proxy
# Added the primary repository
apt-get update
apt-get -y install vim lvm2

if [ -f /etc/disk_added_date ]
then
   echo "disk already added so exiting."
   exit 0
fi


sudo fdisk -u /dev/sdb <<EOF
n
p
1


t
8e
w
EOF

pvcreate /dev/sdb1
vgextend vagrant-vg /dev/sdb1
lvextend -l +100%FREE /dev/vagrant-vg/root
resize2fs /dev/vagrant-vg/root

date > /etc/disk_added_date

!!!Notice!!!

You should especially notice the lvextend’s configuration, for every vagrant box has the different naming mechanism.

Ansible Tips on register and so on

  1. 处理用户输入
vars_prompt, 设置KVMPassword的值为多少

- name: apply CloudStack configuration to all nodes
  hosts: cloudstackmanagement
  sudo: yes

  #########################################################################################
  # Vars and vars_prompt
  #
  vars_prompt:

    - name: "KVMPassword"
      prompt: "KVM host root password"
      private: yes
  1. 可以使用when来界定用户输入值的有效
    - name: Validate input - KVMServer host password
      fail: msg="Missing or incorrect KVM Host password."
      when: KVMPassword is not defined or ( KVMPassword is defined and KVMPassword  == "" )

when 针对不同的返回值采取不同的行动(举某个安装命令yum/apt差别为例)

- name: Install the required  packages in Redhat derivatives
  yum: name={{ item }} state=installed
  with_items: network_pkgs
  when: ansible_os_family == 'RedHat'

- name: Install the required packages in Debian derivatives
  apt: name={{ item }} state=installed update_cache=yes
  with_items: network_pkgs
  environment: env
  when: ansible_os_family == 'Debian'
  1. 某节点输出的值可以被写入到register中,譬如以下命令根据预定义参数,创建zone,管道输出后结果到ZoneID变量中。
    - name: Configure zone and add resources
      shell: cloudmonkey create zone name={{ CMConfig.ZoneName }} dns1={{ CMConfig.PublicDNS1 }} dns2={{ CMConfig.PublicDNS2 }} internaldns1={{ CMConfig.InternalDNS1 }} internaldns2={{ CMConfig.InternalDNS2 }} guestcidraddress={{ CMConfig.GuestCIDR }} networktype={{ CMConfig.NetworkType }} localstorageenabled=false | grep ^id | awk '{print $3}'
      register: ZoneID
  1. 对ZoneID变量的使用: {{ ZoneID.stdout }}
    - name: Create physical network 1
      shell:  cloudmonkey create physicalnetwork name={{ CMConfig.Phys1Name }} zoneid={{ ZoneID.stdout }} isolationmethods={{ CMConfig.Phys1Isolation }} vlan={{ CMConfig.Phys1VLANs }} | grep ^id | awk '{print $3}'
      register: Phys1ID

OpenWRT MR11u

Upgrade kernel

On OpenWRT:

$ cd /tmp
$ wget http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/openwrt-ar71xx-generic-tl-mr11u-v2-squashfs-sysupgrade.bin
$ sysupgrade -v openwrt-ar71xx-generic-tl-mr11u-v2-squashfs-sysupgrade.bin

Make sure your configuration is listed as following:

# cat /etc/opkg/distfeeds.conf 
src/gz designated_driver_base http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/packages/base
src/gz designated_driver_kernel http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/packages/kernel
src/gz designated_driver_telephony http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/packages/telephony
src/gz designated_driver_packages http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/packages/packages
src/gz designated_driver_routing http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/packages/routing
src/gz designated_driver_luci http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/packages/luci
src/gz designated_driver_management http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/packages/management
# src/gz designated_driver_targets http://openwrt.proxy.ustclug.org/snapshots/trunk/ar71xx/generic/packages/targets

Then opkg update, and install following packages:

$ opkg install block-mount kmod-fs-ext4 kmod-usb-ohci kmod-usb-storage

But the storage is too slow.